I have 3 tables. They have 1 to many relations
table1 - mainID, Select00(bit), Select01(bit)
table2 - secID, mainID
table3 - secID, Num00, Num01
SELECT table1.mainID , SUM(table3.Num00) as S00, SUM(table3.Num01) as S01
FROM table1 INNER JOIN
table2 ON table1.mainID = table2.mainID INNER JOIN
table3 ON table2.secID = table3.secID
GROUP BY table1.mainID HAVING table1.mainID =11
The following query works, but gives me all sums.
How do I get the SUMs based on condition? i.e
S00 = SUM(table3.Num00) if table1.Select00 = 1 (true)
S01 = SUM(table3.Num01) if table1.Select01 = 1 (true)
SELECT
t1.mainID,
S00 = SUM(CASE WHEN t1.Select00 = 1 THEN t3.Num00 ELSE 0 END),
S01 = SUM(CASE WHEN t1.Select01 = 1 THEN t3.Num01 ELSE 0 END)
FROM
dbo.table1 AS t1
INNER JOIN dbo.table2 AS t2
ON t1.mainID = t2.mainID
INNER JOIN dbo.table3 AS t3
ON t2.secID = t3.secID
WHERE t1.MainID = 11
GROUP BY table1.mainID;
Related
I have a query as follows:
select --this select should always give me 1 record
tbl1.Id, tbl1.Name, tbl1.Address, tbl2.relNo,
CASE WHEN tbl3.Comments IS NOT NULL THEN 1 ELSE 0 END AS 'Required'
from
table1 tbl1
inner join
table2 tbl2 on tbl2.Id = tbl1.Id
left join -- This left join table gives me 5 records for one instance
(select
R.Id, C.Comments
from
tblC C
inner join
tblR R on R.Id = C.id) tbl3 on tbl3.Id = tbl2.Id
I want to write a CASE statement on the rows my left join is giving to check for null value as above and my final select query always return only 1 row. Is there a way to check if all five Comments Column values from my left join be checked for NULLs in the above query?
I would take a shortcut an use a COUNT() OVER PARTITION
CASE WHEN COUNT(*) OVER (PARTITION BY tbl3.Id) =0 THEN 0 ELSE 1 END AS 'Required'
You would have to DISTINCT your output above. Another option would be to GROUP BY and filter in the HAVING clause.
select --this select should always give me 1 record
tbl1.Id, tbl1.Name, tbl1.Address, tbl2.relNo
From table1 tbl1
inner join table2 tbl2 on tbl2.Id = tbl1.Id
left join (-- This left join table gives me 5 records for one instance
SELECT R.Id,
C.Comments
FROM tblC C
INNER JOIN tblR R on R.Id = C.id
) tbl3 on tbl3.Id = tbl2.Id
GROUP BY
Id, Name, Address, relNo
HAVING
COUNT(*) = 5
Is this what you're looking for?
(CASE WHEN (select count(tbl3.id) FROM tbl3 WHERE tbl3.Comments IS NULL) then 1 else 0 end) as 'RequiredVal'
select --this select should always give me 1 record
tbl1.Id, tbl1.Name, tbl1.Address, tbl2.relNo,
CASE WHEN tbl3.Comments IS NOT NULL THEN 1 ELSE 0 END AS 'Required'
, (CASE WHEN (select count(tbl3.id) FROM tbl3 WHERE tbl3.Comments IS NULL) then 1 else 0 end) as 'RequiredVal'
From table1 tbl1
inner join table2 tbl2 on tbl2.Id = tbl1.Id
left join (-- This left join table gives me 5 records for one instance
SELECT R.Id,
C.Comments
FROM tblC C
INNER JOIN tblR R on R.Id = C.id
) tbl3 on tbl3.Id = tbl2.Id
I want to join five tables in SQL server. The sequence as given below. Logic should be
Table1 >>>>Key : ID >>>> Table_A & Table_B (If Table1.Status = ABC then Table_A else Table_B ) >>> Key : NUMBER >>> Table2 >>> Key : Number + Item_No >>> Table3
Please help if below code could work.
SELECT * FROM
TABLE1
LEFT JOIN (CASE WHEN status = 'ABC' THEN Table_A ELSE Table_B END ) X ON (Table1.ID = X.ID)
LEFT JOIN Table2 ON (X.NUMBER = Table2.NUMBER)
LEFT JOIN Table3 ON (Table3.CONCAT(NUMBER + Item_No) = Table2.CONCAT(NUMBER + Item_No))
SELECT Q.*, T2.ItemNo, T2.Product, T3.Connection
FROM (
SELECT T1.ID, CASE WHEN T1.Status = 'ABC'
THEN TA.Number
ELSE TB.Number
END as Number
FROM Table1 T1
LEFT JOIN TableA TA
ON T1.ID = TA.ID
LEFT JOIN TableB TB
ON T1.ID = TB.ID
) as Q
JOIN Table2 T2
ON Q.Number = T2.Number
JOIN Table3 T3
ON T2.ItemNo = T3.ItemNo
i/p
Id Name InsertBy UpdateBy
1 A 2 2
2 B 1 2
3 C 4 3
4 D 4 5
5 E 1 3
O/P(THE COUNT OF EMPLOYEE ID IN INSERT AND COUNT OF EMPID IN UPDATE)
Name InsertBy UpdateBy
A 2 0
B 1 2
C 0 2
D 2 0
E 0 1
It seems to you would require to do self join with separate query (Inserted, Updated) for safer.
SELECT
t.name, a.insertBy, b.updateBy
FROM table t
INNER JOIN
(
SELECT
t.id, count (t1.insertBy) insertBy
FROM table t
LEFT JOIN table t1 on t1.insertBy = t.id
GROUP BY t.id
)a on a.id = t.id
INNER JOIN
(
SELECT
t.id, count (t2.updateBy) updateBy
FROM table t
LEFT JOIN table t2 on t2.updateBy = t.id
GROUP BY t.id
)b on b.id = t.id
Let me edit with other approach which more efficient with separate join
select t1.name, sum(case when a.Name = 'InsertedBy' then 1 else 0 end) InsertBy,
sum(case when a.Name = 'UpdatedBy' then 1 else 0 end) UpdateBy
from table t
cross apply (
values (InsertBy, 'InsertedBy'), (UpdateBy, 'UpdatedBy')
)a(Types, Name)
LEFT JOIN table t1 on t1.Id = a.Types
group by t1.name
However, these will make use of index on (Id)
I am having a query in which number of execution is equal to the number of rows in the table.
Anyone knows any possible reason for that as a result the clustered index seek %age is very high.
I have update the Stats for the Tables.But still no benefit.
select #AdditionalBillToNumericValue = cast(ab_vnum as int) from #table1
inner join table4 on ab_btid = ay_bt and ab_type = 'SRVGRP'
if(#AdditionalBillToNumericValue is not null or #AdditionalBillToNumericValue <> '')
begin
select distinct
fs.sv_pkey
, fs.sv_id
, fs.sv_desc
,ISNULL( fs.sv_ehr,0) as sv_ehr
,ISNULL( fs.sv_emn,0) as sv_emn
,ISNULL( fs.sv_lhr,0) as sv_lhr
,ISNULL( fs.sv_lmn,0) as sv_lmn
,(case when #table1.fh_sv = fs.sv_id then 1 else 0 end) IsDefault
from table3 fr
inner join table4 ab on ab.ab_vnum = fr.sr_sgpkey
inner join table5 fs on fr.sr_svpkey = fs.sv_pkey or fs.sv_pkey = #ServiceWindowKey
inner join #table1 on ab.ab_btid = #table1.ay_bt
AND ab.ab_type = 'SRVGRP'
end
else
begin
select
fs.sv_pkey
, fs.sv_id
, fs.sv_desc
,ISNULL(sv_ehr,0) as sv_ehr
,ISNULL(sv_emn,0) as sv_emn
,ISNULL(sv_lhr,0) as sv_lhr
,ISNULL(sv_lmn,0) as sv_lmn
,(case when #table1.fh_sv = fs.sv_id then 1 else 0 end) IsDefault
from table3 fr
inner join table4 ab on ab.ab_vnum = fr.sr_sgpkey
inner join table5 fs on fr.sr_svpkey = fs.sv_pkey or fs.sv_pkey = #ServiceWindowKey
LEFT join #table1 with(nolock) on ab.ab_btid = #table1.ay_bt
where ab.ab_type = 'SRVGRP'
Please Verify where we are having the join with table5 it is getting the issue.
In Join Both are having int datatype.
I have this query :
select s.LastState
,count(s.LastState) as sumS
from table1 t1
join table2 t2
on t1.ID = t2.ID
join (select LastState
,count(LastState) as sum
from table1
where ID = X
and LastState = 1
or LastState = 2
group by LastState
) s
on s.LastState = t1.LastState
group by s.LastState
This returns the number of both state and I'd like to have the sum of both my counts.
Currently I see my first line with let's admit
10 state 1 and 5 state 2 for my ID X
and I'd like to see 15 (sum of counts for both states).
select --s.LastState
LastState='LastState1and2'
, count(s.LastState) as sumS
from table1 t1
join table2 t2
on t1.ID = t2.ID
join (select LastState
,count(LastState) as sum
from table1
where ID = X
and LastState = 1
or LastState = 2
group by LastState
) s
on s.LastState = t1.LastState
--group by s.LastState