| Author |
Topic |
|
proser
Starting Member
5 Posts |
Posted - 2008-11-14 : 12:53:14
|
Hello, I'm trying to figure this out but it doesn't seem to work.I'm trying to find all the PID's account that matches each Scheduled Accounts:Table PID:ID PID ACCTKY ----------- ----------- ----------- 1 1 12 1 23 1 34 2 45 2 56 3 4 Table Scheduled:ID SCHD ACCTKY ----------- ----------- ----------- 1 1 12 1 23 1 34 2 15 3 46 5 5 I'd like to see:When SCHD = 1PID MATCH------- --------1 True2 False3 False Thanks alot! |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-11-14 : 12:59:00
|
| [code]SELECT p.PID,CASE WHEN COUNT(s.ID) = 0 THEN 'False'ELSE 'True'ENDFROM PID pLEFT JOIN scheduled sON p.ACCTKY =s.ACCTKYAND s.SCHD=1GROUP BY p.PID[/code] |
 |
|
|
proser
Starting Member
5 Posts |
Posted - 2008-11-14 : 13:17:12
|
Sorry if I wasn't being clear:If I change Table PID:ID PID ACCTKY ----------- ----------- ----------- 1 1 12 1 23 1 34 2 15 2 56 3 4 I get PID 2 TRUE. I want PID table to match ONLY if all the ACCTKY in Scheduled exists in PID table. |
 |
|
|
lionofdezert
Aged Yak Warrior
885 Posts |
Posted - 2008-11-14 : 13:45:14
|
| i think visakh guided you in right way. Read his reply carefully. |
 |
|
|
proser
Starting Member
5 Posts |
Posted - 2008-11-14 : 14:03:59
|
Again, sorry for not being clear about the question.I want to show PID Only if its ACCTKY matches ALL the ACCTKY in Scheduled table for each Schedule ID.Visakh's solution matches even if one of the acctky not exists in the schedule table:Here is a better sample set:PIDID PID ACCTKY ----------- ----------- ----------- 1 1 12 1 23 1 34 2 15 2 26 2 37 3 4 SCHDID SCHD ACCTKY ----------- ----------- ----------- 1 1 12 1 23 1 34 2 15 2 26 2 37 2 48 3 49 5 5 My expected result when SCHD = 1:PID MATCH----- --------1 TRUE2 TRUE3 FALSE Thanks again |
 |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2008-11-14 : 14:24:36
|
| I am getting correct output with visakh's solution.Declare @PID table(ID int, PID int, ACCTKY int)insert @PIDselect 1,1,1 union all select 2,1,2 union all select 3,1,3 union all select 4,2,1 union allselect 5,2,2 union allselect 6,2,3 union allselect 7,3,4 Declare @SCHD table( ID int,SCH int,ACCTKY int)insert @SCHDselect 1 ,1 ,1 union allselect 2 ,1 ,2 union all select 3 ,1 ,3 union allselect 4 ,2 , 1 union allselect 5 ,2 ,2 union allselect 6 ,2 ,3 union allselect 7 ,2 ,4 union allselect 8 ,3 ,4 union allselect 9 ,5 ,5 SELECT p.PID,CASE WHEN COUNT(s.ID) = 0 THEN 'False'ELSE 'True'END as MatchFROM @PID pLEFT JOIN @SCHD sON p.ACCTKY =s.ACCTKYAND s.SCH=1GROUP BY p.PIDOutputPID MATCH1 True2 True3 False |
 |
|
|
proser
Starting Member
5 Posts |
Posted - 2008-11-14 : 14:37:39
|
| [code]Declare @PID table(ID int, PID int, ACCTKY int)insert @PIDselect 1,1,1 union allselect 2,1,2 union allselect 3,1,3 union allselect 4,2,1 union allselect 5,2,2 union allselect 6,2,5 union allselect 7,3,4Declare @SCHD table( ID int,SCH int,ACCTKY int)insert @SCHDselect 1 ,1 ,1 union allselect 2 ,1 ,2 union allselect 3 ,1 ,3 union allselect 4 ,2 ,1 union allselect 5 ,2 ,2 union allselect 6 ,2 ,3 union allselect 7 ,2 ,4 union allselect 8 ,3 ,4 union allselect 9 ,5 ,5SELECT p.PID,CASE WHEN COUNT(s.ID) = 0 THEN 'False'ELSE 'True'END as MatchFROM @PID pLEFT JOIN @SCHD sON p.ACCTKY =s.ACCTKYAND s.SCH=1GROUP BY p.PIDAlso returns:PID Match ----------- ----- 1 True2 True3 False[/code] |
 |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2008-11-14 : 14:56:41
|
quote: Originally posted by proser
Declare @PID table(ID int, PID int, ACCTKY int)insert @PIDselect 1,1,1 union allselect 2,1,2 union allselect 3,1,3 union allselect 4,2,1 union allselect 5,2,2 union allselect 6,2,5 union allselect 7,3,4Declare @SCHD table( ID int,SCH int,ACCTKY int)insert @SCHDselect 1 ,1 ,1 union allselect 2 ,1 ,2 union allselect 3 ,1 ,3 union allselect 4 ,2 ,1 union allselect 5 ,2 ,2 union allselect 6 ,2 ,3 union allselect 7 ,2 ,4 union allselect 8 ,3 ,4 union allselect 9 ,5 ,5SELECT p.PID,CASE WHEN COUNT(s.ID) = 0 or Count(s.ID) <> Count(p.PID) THEN 'False'ELSE 'True'END as MatchFROM @PID pLEFT JOIN @SCHD sON p.ACCTKY =s.ACCTKYAND s.SCH=1GROUP BY p.PIDAlso returns:PID Match ----------- ----- 1 True2 False3 False
|
 |
|
|
proser
Starting Member
5 Posts |
Posted - 2008-11-14 : 15:45:38
|
| Not exactly, it still returns true when counts match but acctky not match. |
 |
|
|
hanbingl
Aged Yak Warrior
652 Posts |
Posted - 2008-11-14 : 15:46:32
|
| [code]select PIDfrom(select *, (select count(distinct acctky) from @PID where PID = p.PID) cnt from @PID P) aleft join(select *, (select count(distinct acctky) from @SCHD where SCH = s.SCH) cnt from @SCHD S where sch = 1) bon a.acctky = b.acctkyand b.sch = 1 and a.cnt = b.cntgroup by PIDhaving count(a.cnt) = count(b.cnt)[/code] |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-11-14 : 23:34:30
|
| [code]SELECT p.PID,CASE WHEN Count(DISTINCT s.ACCTKY) <> Count(DISTINCT p.ACCTKY) THEN 'False'ELSE 'True'END as MatchFROM @PID pLEFT JOIN @SCHD sON p.ACCTKY =s.ACCTKYAND s.SCH=1GROUP BY p.PID[/code] |
 |
|
|
|
|
|