| Author |
Topic |
|
kashyap_sql
Posting Yak Master
174 Posts |
Posted - 2010-10-27 : 03:08:53
|
| I HAVE THREE TABLES TABLE S(SID, SNAME,ADDRESS)SID IS PRIMARY KEYTABLE P(PID,PNAME,COLOR)PID IS PRIMARY KEYTABLE CAT(SID,PID,COST)PRIMARY KEY: SID+PIDREFERENCE KEY : SID REFERENCES S.SID : PID REFERENCES P.PIDI WANT THE SNAME OF SUPPLIERS WHO SUPPLY EVERY PARTI AM TRYING [CODE]SELECT S.SNAME,S.SID,P.PID FROM S INNER JOIN CAT C ON s.sid=c.sid CROSS JOIN P WHERE P.PID=C.PID[/CODE]AFTER THE WHERE WHICH CONDITION I HAD TO PUT IS A LITTLE BIT CONFUSED PLZ HELPWith RegardsKashyap M |
|
|
Kristen
Test
22859 Posts |
Posted - 2010-10-27 : 03:25:40
|
Something like this perhaps?SELECT SupplierIDFROM SupplierProductLinkGROUP BY SupplierIDHAVING COUNT(*) = (SELECT COUNT(*) FROM Products) orSELECT S.xxxFROM Supplier AS SWHERE NOT EXISTS( SELECT * FROM Products AS P WHERE NOT EXISTS ( SELECT * FROM SupplierProductLink AS L WHERE L.ProductID = P.ProductID AND L.SupplierID = S.SupplierID )) |
 |
|
|
kashyap_sql
Posting Yak Master
174 Posts |
Posted - 2010-10-27 : 03:38:12
|
| THIS IS VERY USEFUL THANKS KRISTEN IS THERE ANY IDEAS TO CHANGE MODIFICATIONS IN MY CODE BY USING JOINSWith RegardsKashyap M |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-10-27 : 03:53:50
|
quote: Originally posted by kashyap_sql THIS IS VERY USEFUL THANKS KRISTEN IS THERE ANY IDEAS TO CHANGE MODIFICATIONS IN MY CODE BY USING JOINSWith RegardsKashyap M
What is wrong with given solutions?MadhivananFailing to plan is Planning to fail |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2010-10-27 : 04:08:30
|
| Why do you want to use JOINs? You are going to select thousands of rows that are not needed in the resultset. |
 |
|
|
kashyap_sql
Posting Yak Master
174 Posts |
Posted - 2010-10-27 : 06:51:15
|
quote: Originally posted by madhivanan
quote: Originally posted by kashyap_sql THIS IS VERY USEFUL THANKS KRISTEN IS THERE ANY IDEAS TO CHANGE MODIFICATIONS IN MY CODE BY USING JOINSWith RegardsKashyap M
What is wrong with given solutions?MadhivananFailing to plan is Planning to fail
there is no wrong with the code it was very simple and very usefuli was just trying to implement my need by using joinWith RegardsKashyap M |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2010-10-27 : 08:09:42
|
quote: Originally posted by kashyap_sql I HAVE THREE TABLES TABLE S(SID, SNAME,ADDRESS)SID IS PRIMARY KEYTABLE P(PID,PNAME,COLOR)PID IS PRIMARY KEYTABLE CAT(SID,PID,COST)PRIMARY KEY: SID+PIDREFERENCE KEY : SID REFERENCES S.SID : PID REFERENCES P.PIDI WANT THE SNAME OF SUPPLIERS WHO SUPPLY EVERY PARTI AM TRYING [CODE]SELECT S.SNAME,S.SID,P.PID FROM S INNER JOIN CAT C ON s.sid=c.sid CROSS JOIN P WHERE P.PID=C.PID[/CODE]AFTER THE WHERE WHICH CONDITION I HAD TO PUT IS A LITTLE BIT CONFUSED PLZ HELPWith RegardsKashyap M
CROSS JOIN means every row in the Left_Table is joined to every row in the Right_Table.There is no need for a condition.So what would you like your WHERE clause to do? No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
kashyap_sql
Posting Yak Master
174 Posts |
Posted - 2010-11-01 : 08:25:21
|
| find the pnames of parts supplied by london supplier and by no one elsewhat it means any suggestions will be very helpful thanks in advanceWith RegardsKashyap M |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2010-11-01 : 11:29:19
|
The best would be if you could provide the DDL, some sample data and the wanted output in relation to the sample data. No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
kashyap_sql
Posting Yak Master
174 Posts |
Posted - 2010-11-02 : 01:37:29
|
TABLE P LOOKS LIKEPID. PNAME. COLOR101 AAA RED102 BBB BLUE103 CCC GREEN104 DDD BLACK105 EEE PINK106 FFF WHITE107 GGG YELLOW TABLE S LOOKS LIKE[CODE]SID. SNAME. ADDRESS1 ABCD LONDON2 EFGH NEWYORK3 IJKL AUSTIN4 MNOP CALIFORNIA5 QSTU LONDON6 VWXY NOIDA7 ZABC SWEDEN[/CODE]TABLE CAT LOOKS LIKE[CODE]SID. PID. COST1 101 781.001 102 61.001 103 15.001 104 401.001 105 841.001 106 689.001 107 581.002 107 67.003 106 154.004 102 5.004 104 15.205 101 75.005 104 92.206 105 81.007 104 15.60[/CODE]I DON'T HAVE ANY IDEA ABOUT THE OUTPUT i want just the PNAMES of parts supplied by LONDON supplier and by no one elseWith RegardsKashyap M |
 |
|
|
kashyap_sql
Posting Yak Master
174 Posts |
Posted - 2010-11-02 : 01:38:40
|
i was trying like SELECT P.PNAME,S.ADDRESS FROM P JOIN CAT C ON P.PID=C.PID INNER JOIN S ON S.SID=C.SID WHERE S.ADDRESS='LONDON' GROUP BY P.PNAME,S.ADDRESS With RegardsKashyap M |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2010-11-02 : 06:51:32
|
[code]declare @P table (PID int, PNAME varchar(255), COLOR varchar(255))declare @S table ([SID] int, SNAME varchar(255), [ADDRESS] varchar(255))declare @CAT table ([SID] int, PID int, COST decimal(12,2))insert @Pselect 101, 'AAA', 'RED' union allselect 102, 'BBB', 'BLUE' union allselect 103, 'CCC', 'GREEN' union allselect 104, 'DDD', 'BLACK' union allselect 105, 'EEE', 'PINK' union allselect 106, 'FFF', 'WHITE' union allselect 107, 'GGG', 'YELLOW' insert @Sselect 1, 'ABCD', 'LONDON' union allselect 2, 'EFGH', 'NEWYORK' union allselect 3, 'IJKL', 'AUSTIN' union allselect 4, 'MNOP', 'CALIFORNIA' union allselect 5, 'QSTU', 'LONDON' union allselect 6, 'VWXY', 'NOIDA' union allselect 7, 'ZABC', 'SWEDEN'insert @CATselect 1, 101, 781.00 union allselect 1, 102, 61.00 union allselect 1, 103, 15.00 union allselect 1, 104, 401.00 union allselect 1, 105, 841.00 union allselect 1, 106, 689.00 union allselect 1, 107, 581.00 union allselect 2, 107, 67.00 union allselect 3, 106, 154.00 union allselect 4, 102, 5.00 union allselect 4, 104, 15.20 union allselect 5, 101, 75.00 union allselect 5, 104, 92.20 union allselect 6, 105, 81.00 union allselect 7, 104, 15.60select DISTINCTp.PNAMEfrom @CAT as cjoin @S as son s.[SID]=c.[SID] and s.[ADDRESS]='LONDON'join @P as pon p.PID = c.PIDand not exists(select * from @CAT c2 where c2.PID=c.PID and c2.[SID] in(select [SID] from @S where [ADDRESS] <> 'LONDON'))[/code] No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2010-11-02 : 06:52:42
|
By the way.Giving testdata in the above format is your job - not mine!! No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2010-11-02 : 07:04:35
|
Wow - YES! No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2010-11-02 : 08:33:33
|
Hello Peso,I found that very interesting.Now I have modified my solution and it would be cool if you could evaluate if it is a right solution regarding "relational division".Many thanks.SELECT p.PNAMEFROM @CAT AS cJOIN @S AS s ON s.[SID] = c.[SID] JOIN @P AS p ON p.PID = c.PIDGROUP BY p.PNAMEHAVING MIN(CASE WHEN s.[ADDRESS] = 'LONDON' THEN 1 ELSE 0 END)=1 AND SUM(CASE WHEN s.[ADDRESS] <> 'LONDON' THEN 1 ELSE 0 END)=0 No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2010-11-02 : 08:42:42
|
Look ok to me. You can even drop the SUM aggregation. N 56°04'39.26"E 12°55'05.63" |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2010-11-02 : 09:11:21
|
Thank you Peso.You are right (ah - yes you know that you are right )It took me a while to get WHY it works.This HAVING MIN(CASE WHEN s.[ADDRESS] = 'LONDON' THEN 1 ELSE 0 END)=1secures that 'LONDON' exists in the grouping and ONLY 'LONDON' because otherwise the value of MIN() would be zero.I've learned so much today - thanks again. No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2010-11-02 : 17:34:48
|
You're welcome. N 56°04'39.26"E 12°55'05.63" |
 |
|
|
|