Choose Maximum among Duplicates - sql-server

I am writing a query in SQL to be used for SSRS 2005 which is
select t1.Category, t2.condition,'1' as hasCondition from t1
left outer join t2 on t1.ID = t2.ID
left outer join t3 on t2.cID = t3.cID
where t3.clientID = 6
union
select t1.Category, t2.condition, '0' as hasCondition from t1
left outer join t2 on t1.ID = t2.ID
and it returns the data as:
Category Condition hasCondition
Behavioural Tourette's Syndrome 0
Communications Impaired Speech 0
Dexterity Osteoarthritus 0
Dexterity Osteoporosis 0
Dexterity Other Dexterity 0
Dexterity Rheumatoid Arthritus 0
Emotional Bipolar Disorder 0
Emotional Clinical Depression 0
Emotional Depression 0
Emotional Depression 1
Emotional Gulf War Syndrome 0
Emotional Gulf War Syndrome 1
Now I want to select only one record from duplicates rows/results for example:
Category-Emotional, condition-Depression which hasCondition '1' or 'true'
I hope I made it clear using the example.
Please help me.
Thanks.

It's not 100% clear what you want, but I think you're just looking for the MAX() of the final field...
SELECT
category, condition, MAX(has_condition) AS hasCondition
FROM
(
select t1.Category, t2.condition,'1' as hasCondition from t1
left outer join t2 on t1.ID = t2.ID
left outer join t3 on t2.cID = t3.cID
where t3.clientID = 6
union
select t1.Category, t2.condition, '0' as hasCondition from t1
left outer join t2 on t1.ID = t2.ID
)
AS data
GROUP BY
category, condition
If you wanted to simplify that whole query...
select
t1.Category,
t2.condition,
MAX(CASE WHEN t3.clientID = 6 THEN 1 ELSE 0 END) as hasCondition
from
t1
left outer join
t2
on t2.ID = t1.ID
left outer join
t3
on t3.cID = t2.cID
and t3.clientID = 6
group by
t1.Category,
t2.condition

SELECT category, condition, MAX(hasCondition)
FROM (
select t1.Category, t2.condition,'1' as hasCondition from t1
left outer join t2 on t1.ID = t2.ID
left outer join t3 on t2.cID = t3.cID
where t3.clientID = 6
union
select t1.Category, t2.condition, '0' as hasCondition from t1
left outer join t2 on t1.ID = t2.ID)
GROUP BY category, condition

Related

SQL Case statement with subquery

can somebody tell me what wrong with my case statement?
select
(case
when (select top 1 descr from t1 inner join t2 on t2.id = t1.id where t1.code = '17418')= 'C' Then 'Cancelled'<br/>
when (select top 1 descr from t1 inner join t2 on t2.id = t1.id where t1.code = '100020')= 'CL' Then 'Closed'<br/>
when (select top 1 descr from t1 inner join t2 on t2.id = t1.id where t1.code = '1105')= 'R' Then 'Reserved'<br/>
when (select top 1 descr from t1 inner join t2 on t2.id = t1.id where t1.code = '1106')= 'S' Then 'Scheduled' <br/>
else null end ) <br/>
From table
I think you wanted to do something like this :)
select CASE t1.descr
WHEN 'C' THEN 'Cancelled'
WHEN 'CL' THEN 'Closed'
WHEN 'R' THEN 'Reserved'
WHEN 'S' THEN 'Schedule'
ELSE
END as descr_Text
from t1
inner join t2 on t2.id = t1.id
where t1.code in('17418','100020','1105',....)

how to join 4 table into single table?

table-1
name closedid
rere 4
trtr 5
ewew 6
And
table-2
name openedid
rere 6
trtr 7
ytyt 8
uyuy 5
And
table-3
name assign
rere 6
ytyt 8
uyuy 5
rtyy 9
And
table-4
name unassign
rere 6
trtr 7
errt 5
hdtg 9
I want final output like this:
name closedid opened assign unassign
rere 4 6 6 6
trtr 5 7 null 7
ytyt null 8 8 null
uyuy null 5 5 null
ewew 6 null null null
rtyy null null 9 null
errt null null null 5
hdtg null null null 9
I think, this is what you are looking for:
SELECT coalesce(t1.NAME, t2.NAME, t3.NAME, t4.NAME) NAME
,t1.closedid
,t2.openedid
,t3.assign
,t4.unassign
FROM [table-1] t1
FULL OUTER JOIN [table-2] t2 ON t1.NAME = t2.NAME
FULL OUTER JOIN [table-3] t3 ON isnull(t1.NAME, t2.NAME) = t3.NAME
FULL OUTER JOIN [table-4] t4 ON coalesce(t1.NAME, t2.NAME, t3.NAME) = t4.NAME
I would grab all the distinct names and then do left joins with that list, would make it easier to understand what's going on.
;WITH cte(name)
AS (
SELECT name FROM table1
UNION
SELECT name FROM table2
UNION
SELECT name FROM table3
UNION
SELECT name FROM table4
)
SELECT
cte.name,
table1.closedid,
table2.openid,
table3.assign,
table4.unassign
FROM
cte
LEFT JOIN table1 ON cte.name = table1.name
LEFT JOIN table2 on cte.name = table2.name
LEFT JOIN table3 on cte.name = table3.name
LEFT JOIN table4 on cte.name = table4.name
SQLFiddle
Use UNION to get your distinct list of names
Use LEFT JOIN to retrieve your data
Example:
SELECT list.name, t1.assigned, t2.unsassigned, t3.closed, t4.open
FROM
(select name from table1
union select name from table2
union select name from table3
union select name from table4
) list
LEFT JOIN table1 t1 ON (list.name = t1.name)
LEFT JOIN table2 t2 ON (list.name = t2.name)
LEFT JOIN table3 t3 ON (list.name = t3.name)
LEFT JOIN table4 t4 ON (list.name = t4.name)
select
n.name, t1.closedid, t2.openedid, t3.assign, t4.unassign
from
(select name from [table-1]
union
select name from [table-2]
union
select name from [table-3]
union
select name from [table-4]
) as n
left join [table-1] t1 on n.name = t1.name
left join [table-2] t2 on n.name = t2.name
left join [table-3] t3 on n.name = t3.name
left join [table-4] t4 on n.name = t4.name
select coalesce(t1.name,t2.name,t3.name,t4.name) name, t1.closeid, t2.openid, t3.assign, t4.unassign
from table-1 t1
full outer join table-2 t2 on t1.name = t2.name
full outer join table-3 t3 on t2.name = t3.name
full outer join table-4 t4 on t3.name = t4.name

Missing data from converting OR condtion in Join to Another JOIN

Few days back I asked how to change OR condtion (left join) into another left join. Here is the code below :
SELECT *
FROM Table1 t1
LEFT JOIN Table2 t2
ON t1.ID = t2.ID
OR (
t1.col1 = t2.col1
AND t1.col2 = t2.col2
AND t1.col3 = t2.col3
AND t2.ID IS NULL
)
I converted to OR condition like below:
SELECT *
FROM Table1 t1
LEFT JOIN Table2 t2
ON t1.ID = t2.ID
LEFT JOIN Table2 t3
ON (
t1.col1 = t3.col1
AND t1.col2 = t3.col2
AND t1.col3 = t3.col3
AND t2.ID IS NULL
)
But i am not getting records for second left join in seperate row.
With OR condition result are like :
T1.ID T2.ID
1 a
2 b
3 c
4 d
But with second left join its lile below:
T1.ID T2.ID
1 a
2 b
3 c
4rth record is from second left join. How can i bring that in left join. I dont have option for using UNION ALl as per company standards.
Please help.
Thanks
First of all please notice that you're using t2 instead of t3 in the second left join :
SELECT *
FROM Table1 t1
LEFT JOIN Table2 t2
ON t1.ID = t2.ID
LEFT JOIN Table2 t3
ON (
t1.col1 = t3.col1
AND t1.col2 = t3.col2
AND t1.col3 = t3.col3
AND t3.ID IS NULL
)
Anyhow, without understanding what you'r trying to do and how do you want to see your result, it's hard to answer you.
You're using left join which just gives you everything from left result set. Since, you want data from t3 which is in right, change second left join to be either right join or full join, depending on your requirements. One of the following query can give you correct result:
Editing 14-03-2015
SELECT *
FROM Table1 t1
FULL OUTER JOIN Table2 t2
ON t1.ID = t2.ID
FULL OUTER JOIN Table2 t3
ON (
t1.col1 = t3.col1
AND t1.col2 = t3.col2
AND t1.col3 = t3.col3
AND t2.ID IS NULL
)

How to get Count of Joined Rows

There is this SQL Statement
SELECT t1.Name
,Count(t2.SubID) Totals -- I don't know how to do it.
FROM Table t1
INNER JOIN Table2 t2 ON t1.ID = t2.SubID
Thanks.
Guessing... change the JOIN and add GROUP BY
SELECT t1.Name
,Count(t2.SubID) AS Totals
FROM Table t1
LEFT OUTER JOIN Table2 t2 ON t1.ID = t2.SubID
GROUP BY t1.Name
The LEFT OUTER JOIN allows you to find count zero rows per t1.Name
SELECT t1.Name, Count(t2.SubID) Totals
FROM Table t1
INNER JOIN Table2 t2 ON t1.ID = t2.SubID
GROUP BY t1.Name

Change Sql "Not In" To "Left Outer Join"

I don't want to use "not in" this sql query. How can I do it? thanks
SELECT
T2.Sno,
T2.Name,
T1.description,
T2.UserCode
FROM
Table1 AS T1 (nolock)
INNER JOIN T2 (nolock)
ON T1.UserCode = T2.UserCode
WHERE
g.xid= #p_xid
and T2.Sno not in (select Gid from T3 (nolock))
Assuming there is no row in T2 where Sno is null and in T3 where Gid is null:
SELECT
T2.Sno,
T2.Name,
T1.description,
T2.UserCode
FROM
Table1 AS T1 WITH (nolock)
INNER JOIN T2 WITH (nolock)
LEFT JOIN T3 WITH (NOLOCK)
ON T2.Sno = T3.Gid
ON T1.UserCode = T2.UserCode
WHERE
g.xid= #p_xid
and T3.Gid IS NULL
If you have multiple T3 rows per T2.Sno = T3.Gid, you'll need DISTINCT in a JOIN.
Without DISTINCT, it's a different query
With DISTINCT, it's an extra step.
I'd use NOT EXISTS which avoids this.
SELECT
T2.Sno,
T2.Name,
T1.description,
T2.UserCode
FROM
Table1 AS T1 (nolock)
INNER JOIN T2 (nolock)
ON T1.UserCode = T2.UserCode
WHERE
g.xid= #p_xid
and not exists (select * from T3 (nolock) where T3.Gid = T2.Sno)

Resources