I would like to get the representation of one record based on the primary key value from multiple tables. As shown below, each table can have multiple values based on this primary key value.
TABLE-1
ID
NAME
1
AA
2
BB
3
CC
4
DD
5
EE
TABLE-2
ID
SCHOOL
AUT
1
11
A
2
11
A
2
12
B
3
11
A
4
12
A
4
13
B
5
13
A
TABLE-3
ID
TC
1
101
2
102
2
103
2
104
3
105
4
106
4
107
5
108
The result below is the value obtained with an OUTER JOIN.
SELECT
T1.ID, T2.SCHOOL, T3.TC, T2.AUT
FROM
T1
LEFT OUTER JOIN
T2 ON T1.ID = T2.ID
LEFT OUTER JOIN
T3 ON T1.ID = T3.ID
ORDER BY
T1.ID ASC
ID
SCHOOL
TC
AUT
1
11
101
A
2
11
102
A
2
12
102
B
2
11
103
A
2
12
103
B
2
11
104
A
2
12
104
B
3
11
105
A
4
12
106
A
4
13
106
B
4
12
107
A
4
13
107
B
5
13
106
A
How can I get the result like below?
ID
SCHOOL
TC1
TC2
TC3
1
11
101
2
11
102
103
104
3
11
105
4
12
106
107
5
13
108
The important thing here is that in the result value, SCHOOL only shows that AUT is 'A'.
I would appreciate it if you let me know your query.
It looks, from your desired results, you just need to use row_number in combination with a conditional aggregate. Your sample data seems a little inadequate, I can't see any requirement for table1 at all.
Try the following:
with t as (
select t2.id,t2.school,t3.tc, Row_Number() over(partition by t2.id order by t3.tc) col
from t2 join t3 on t2.id=t3.id
where aut='A'
)
select id,school,
max(case when col=1 then tc end) TC1,
max(case when col=2 then tc end) TC2,
max(case when col=3 then tc end) TC3
from t
group by id, school
Example SQL Fiddle
SELECT
T1.ID, T2.SCHOOL,
GROUP_CONCAT(T3.TC),
GROUP_CONCAT(T2.AUT)
FROM
T1
LEFT OUTER JOIN
T2 ON T1.ID = T2.ID
LEFT OUTER JOIN
T3 ON T1.ID = T3.ID
GROUP BY
T1.ID, T2.SCHOOL
WHERE
T2.AUT = ‘A’
ORDER BY
T1.ID ASC
Notice that GROUP_CONCAT concatenates the values in the row.
EDIT: oh my, haven't seen that it's a SQL Server question!
Just replace GROUP_CONCAT with STRING_AGG if you’re using SQL Server 2017 or newer.
Related
I have three tables which look like those:
TABLE 1
id j_id
1 1
2 2
3 3
TABLE 2
id j_id table1_id
1 57 1
2 84 1
3 1 1
4 9 2
5 2 2
and every j has a value in a third table
id value
1 1abc
2 2bcd
3 3abc
57 57abc
84 84abc
9 9abc
I am trying to write a query which will join table 1 and table 2 and use the J value from the third table instead of the j_id, but the problem is that I want to use the j value from the second table if it exists and otherwise use the value from the first table.
in order the make it clearer this is my query result without using the third table:
tbl1.j_id tbl2.j_id
1 1
1 84
1 57
2 2
2 9
3 null
I want the end query result to use the second table's j value unless it is null:
tbl1.j_id tbl2.j_id j_id
1 1 1abc
1 84 84abc
1 57 57abc
2 2 2abc
2 9 9abc
3 null 3abc
(Question and title edits are more than welcome, weren't that sure how to phrase them..)
You can simply JOIN to table3 on the COALESCE of table2.j_id and table1.j_id:
SELECT t1.j_id AS t1_j_id, t2.j_id AS t2_j_id, t3.value
FROM table1 t1
LEFT JOIN table2 t2 ON t2.table1_id = t1.id
JOIN table3 t3 ON t3.id = COALESCE(t2.j_id, t1.j_id)
Output:
t1_j_id t2_j_id value
1 1 1abc
1 57 57abc
1 84 84abc
2 2 2bcd
2 9 9abc
3 null 3abc
Demo on dbfiddle
One solution is to left join table3 twice:
select
t1.j_id,
t2.j_id,
coalesce(t31.value, t32.value) j_value
from
table1 t1
left join table2 t2 on t2.table1_id = t1.id
left join table3 t31 on t31.id = t2.j_id
left join table3 t32 on t32.id = t1.j_id
I need to compare and identify all IDs that have a mismatch in the codes between the categories, upper and lower case are only examples and I cannot base the join on the category field as they are different.
e.g: ID 1 - cat A, a have the same code but ID 3, cat E and e have a mismatch, same with ID#4.
Edit: sorry - after posting the question, and looking at the answers I have realized that my question was not conveying what I am looking for, and I have mocked up data incorrectly. The Category doesn't always have the same name as uppercase/lowercase letters, this was only meant to be an example.
T1
ID Cat Code
1 A 100
1 a 100
1 B 101
1 b 101
2 C 102
2 D 103
2 d 103
3 E 104
3 e 107
3 F 105
3 g 106
4 I 109
4 i 110
5 ABC 111
5 XYZ 112
5 KLM 123
6 PQR 113
6 STU 113
Desired output:
ID Cat Code
3 E 104
3 e 107
4 I 109
4 i 110
5 ABC 111
5 XYZ 112
5 KLM 123
This answers the original version of the question.
Use exists:
select t1.*
from t1
where exists (select 1
from t1 tt1
where tt1.id = t1.id and
upper(tt1.cat) = upper(t1.cat) and
tt1.cat <> t1.cat and
tt1.code <> t1.code
);
Oracle is case-sensitive by default.
You can use a self join like below to handle this issue:
select t1.*
from tablename t1
inner join tablename t2 on t2.ID = t1.ID and upper(t1.Cat) = upper(t2.Cat)
where t1.Code <> t2.Code
I have 2 tables:
Table1 - Criteria
Office_ID Bus_Stream Bus_Criteria Crit_Value
1 ABC 0 20
1 ABC 1 21
1 ABC 2 7
2 ABC 0 15
2 ABC 1 12
2 ABC 2 21
3 XYZ 1 17
3 XYZ 2 3
Table2 - Limit
Bus_Stream GroupID TypeID SubgroupID Bus_Limit
ABC 20 21 7 50
ABC 15 12 21 100
XYZ 99 17 3 120
I need to create a join that allows me to pull back:
Result
Bus_Stream Office_ID GroupID TypeID SubgroupID Bus_Limit
ABC 1 20 21 7 50
ABC 2 15 12 21 100
XYZ 3 (null) 17 3 120
Essentially, I need to join Table1.Crit_Value based on the following:
Table1.Bus_Criteria Table2
0 GroupID
1 TypeID
2 SubGroupID
with the added complication that if one or two of the 0/1/2 values from Bus_Criteria is missing, the joins will still occur on the remaining criteria.
I have tried a number of combinations of AND/OR on the join to no avail.
Any ideas folks?
This may be what you're after.. use a case statement on the join.
The problem here is your data in t2 isn't normalized, you could also unpivot the 3 data columns in t2 so the join is more natural.
SELECT T2.Bus_Stream, T1.Office_ID, T2.GroupID, T2.TypeID, T2.SubGroupId, T2.bus_Limit
FROM T1
INNER JOIN T2
on T1.Bus_Stream = T2.Bus_Stream
and T1.Crit_value = case when T1.Bus_Critiera = 0 then T2.GroupID
when T1.Bus_Critiera = 1 then T2.TypeID
when T1.Bus_Critiera = 2 then T2.SubGroupID
end
Did you try something like this?
SELECT
t1.*, t2.*
FROM Table1 t1
INNER JOIN Table2 t2 ON
t1.Bus_Stream = t2.Bus_Stream AND
CASE
WHEN t1.Bus_Criteria = 0
THEN t2.GroupID = t1.Crit_Value
WHEN t1.Bus_Criteria = 1
THEN t2.TypeID = t1.Crit_Value
ELSE
t2.SubGroupID = t1.Crit_Value
END
I have two tables in SQL Server that looks as follows,
Table 1:
Store Type
100 A
101 B
102 B
103 B
104 C
105 C
106 A
107 A
108 D
109 D
110 C
111 B
112 D
and table 2:
Store Units
100 5
101 3
102 10
103 6
104 6
105 8
I want to combine the two tables in a query with a relationship link between the Store column of the two tables. The problem i have is that if i create a query between the two then the stores from 106 to 112 does not appear in the table. The query looks as follows,
SELECT dbo.Table1.Store,
dbo.Table1.Type,
dbo.Table2.Units
FROM Table1 INNER JOIN Table2
ON dbo.Table1.Store=dbo.Table2.Store
What should i do to include the stores that does not appear in Table2 (106-112) but in Table1 and make there Units = 0, such that the query table look as follows,
Store Type Units
100 A 5
101 B 3
102 B 10
103 B 6
104 C 6
105 C 8
106 A 0
107 A 0
108 D 0
109 D 0
110 C 0
111 B 0
112 D 0
You want a left outer join instead of an inner join:
SELECT t1.Store, t1.Type, COALESCE(t1.Units, 0) as Units
FROM Table1 t1 LEFT JOIN
Table2 t2
ON t1.Store = t2.Store;
I also introduced table aliases for your query to make it more readable. Note the use of coalesce() so you get 0 for Units instead of NULL.
SELECT dbo.Table1.Store, dbo.Table1.Type, coalesce(dbo.Table2.Units, 0) As Units
FROM Table1
LEFT JOIN Table2 ON dbo.Table1.Store=dbo.Table2.Store
In sql server 2005, how do I get the result set in table3 below? table3 is created by combining table1 and table2 like this:
table1.empid
table1.ticket
table2.identid
And update against table1 joined to table2 doesn't work because empid isn't unique. If need to increment to the next table2.indentid if the ID is already being used by that employee.
Also, table3 below isn't created yet. It's a generated set from table1 and table2. I'm using table3 as an example of what the generated set should look like.
table1
empid ticketid indentid
1 7 20
1 9 4
2 9 21
table2
indentid empid
90 1
91 1
92 2
table3
empid ticketid table1_indentid table2_identid
1 7 20 90
1 9 4 91
2 9 21 92
The only connection between table1 and table2 is empid.
The lines 1 and 2 of your table3 example have the same empid, so they should have the same table2_identid as well. Your example is not proper, or not possible to get with a query on the existing data.
But maybe this is what you want:
If you join the tables like so
SELECT empid, ticketid, t1.indentid as table1_indentid, t2.identid as table2_identid
FROM table1 AS t1
INNER JOIN table2 AS t2 ON t1.empid = t2.empid
you will get
table3
empid ticketid table1_indentid table2_identid
1 7 20 90
1 7 20 91
1 9 4 90
1 9 4 91
2 9 21 92
This is what you need:
select t1.empid, t1.ticketid,
t1.indentid as table1_indentid,
t2.indentid as table2_identid
from table1 t1
inner join table2 t2 on t1.empid = t2.empid
You should however consider reviewing your data structure.