问题
I have a database with 20+ tables which I am trying to join into one big query in order to create a report. I have been successful in creating a basic report through SSMS query builder, however one table is causing trouble, since there is a field that can have two values (in or out) for the same uniqueID, I am getting duplicate rows in my report, where all other data is repeated and then finally the information is displayed containing the in and out related fields.
I guess this is kind of a classical problem, but no idea how to solve it or what this is called.
Considering following table (A):
+------+--------------+--------------+--------+----------+
| type | information1 | information2 | Notes | uniqueID |
+------+--------------+--------------+--------+----------+
| IN | infoA | anotherinfo1 | NotesA | SN1 |
+------+--------------+--------------+--------+----------+
| OUT | infoB | anotherinto2 | NotesB | SN2 |
+------+--------------+--------------+--------+----------+
The result in my report is then that all other information get repeated in each row for both types.
After doing a bit of research here on SO how to join two SQL queries, I found a solution that would allow me to extract the information from this table so that the information gets distributed correctly over columns, not rows.
Table (B):
Type 1 |Info 1 |unique ID type 1 |unique ID type 2 |Type 2 |Info 2 |
-----------|-------------------|------------------|------------------|-----------|-----------------|
IN |Information 1 |SerialNumber1 |SerialNumber1 |OUT |Information2 |
IN |Information 1 |SerialNumber2 |SerialNumber2 |OUT |Information2 |
Related query that works to create table B:
SELECT * FROM
(SELECT
t1.Type as "Type 1", t1.information as "Info 1", t1.uniqueID as "unique ID type 1", t2.uniqueID as "unique ID type 2", t2.Type as "Type 2", t2.Information1 as "Info 2"
FROM
(SELECT uniqueID, type, information1, information2, Notes, from TABLE where type = 'IN') as t1,
(SELECT uniqueID, type, information1, information2, Notes, from TABLE where type = 'OUT') as t2
WHERE
t1.uniqueID = t2.uniqueID) as X1;
Please not that unique ID columns are just included to proof that the query works.
The original table is also a bit bigger, trying to keep it short here.
Now the main problem: How can I incorporate my query into my big query?
As you can see I tried to create the X1 variable, where I though I would then be able to do a join on something like X1.t1.uniqueID. This however, does not work. X1.t1.uniqueID is not even recognised in my editor and then fails for the same reason upon execution. Any ideas how to achieve what I need, preferably through the use of a query builder like the one in SSMS? The query is getting too big for me to be built manually.
Many thanks in advance!
回答1:
your current method is not very efficient. Others usually will use CASE WHEN
to do it.
SELECT t.uniqueID,
IN_Info1 = MAX(case when t.type = 'IN' then t.information1 end),
IN_Info2 = MAX(case when t.type = 'IN' then t.information2 end),
IN_Notes = MAX(case when t.type = 'IN' then t.Notes end),
OUT_Info1 = MAX(case when t.type = 'OUT' then t.information1 end),
OUT_Info2 = MAX(case when t.type = 'OUT' then t.information2 end),
OUT_Notes = MAX(case when t.type = 'OUT' then t.Notes end)
FROM TABLEB t
GROUP BY t.uniqueID
and then to incorporate in your big query, you can either use CTE or DERIVED TABLE
-- CTE
; with Tblb as
(
SELECT t.uniqueID,
IN_Info1 = MAX(case when t.type = 'IN' then t.information1 end),
IN_Info2 = MAX(case when t.type = 'IN' then t.information2 end),
IN_Notes = MAX(case when t.type = 'IN' then t.Notes end),
OUT_Info1 = MAX(case when t.type = 'OUT' then t.information1 end),
OUT_Info2 = MAX(case when t.type = 'OUT' then t.information2 end),
OUT_Notes = MAX(case when t.type = 'OUT' then t.Notes end)
FROM TABLEB t
GROUP BY t.uniqueID
)
select *
from TableA a
inner join Tblb b ON a.uniqueID = b.uniqueID
You can't do this X1.t1.uniqueID.
, it should only be X1.uniqueID
来源:https://stackoverflow.com/questions/49809144/perform-join-on-joined-queries