| Author |
Topic |
|
cindylee
Yak Posting Veteran
55 Posts |
Posted - 2005-01-27 : 01:45:53
|
| Hello PPl,I have 2 tablestable A--------CustomerID (PK)120024003444Table B--------CustomerId (FK) itemID Date status1200 30200 20/11/04 checked out1200 30200 30/12/04 Due Date1200 30200 1/01/05 Billsent1200 30201 20/11/04 checked out1200 30201 30/12/04 Due Date1200 30201 1/01/05 BillsentI need a output like thisCustomerID itemId CheckedoutDate DueDate BillsentDate1200 30200 20/11/04 30/12/05 1/01/051200 30201 20/11/04 30/12/05 1/01/05any help greatly appreciatedCindy |
|
|
cindylee
Yak Posting Veteran
55 Posts |
Posted - 2005-01-27 : 01:50:32
|
| Do i need to use a CASE |
 |
|
|
Stoad
Freaky Yak Linguist
1983 Posts |
Posted - 2005-01-27 : 03:25:33
|
| No, Cindy -- CASE is not your case (i'm a famous linguist, btw);select CustomerID, itemId,(select Date from B as t wheret.CustomerID=B.CustomerID and t.itemId=B.itemId and t.status='checked out')AS CheckedoutDate,(select Date from B as t wheret.CustomerID=B.CustomerID and t.itemId=B.itemId and t.status='Due Date')AS DueDate,(select Date from B as t wheret.CustomerID=B.CustomerID and t.itemId=B.itemId and t.status='Billsent')AS BillsentDatefrom B |
 |
|
|
cindylee
Yak Posting Veteran
55 Posts |
Posted - 2005-01-27 : 18:06:17
|
| thanks stoadCindy |
 |
|
|
cindylee
Yak Posting Veteran
55 Posts |
Posted - 2005-01-27 : 19:54:02
|
| Hi Stoad,i tried the query and the ouput is:CustomerID ItemID CheckedOutDate DueDate BillSent---------- -------- ------------- -------- --------- 1200 30200 20/11/04 30/12/05 1/01/051200 30200 20/11/04 30/12/05 1/01/051200 30200 20/11/04 30/12/05 1/01/05It duplicates 3 times, but when i use distinct its ok.Also I only want for the customers who are present in Table A.Tycindy |
 |
|
|
jhermiz
3564 Posts |
Posted - 2005-01-27 : 23:06:52
|
Try this:SELECT B.CustomerID, B.ItemID, (SELECT D FROM B AS t WHERE t.CustomerID=B.CustomerID AND t.ItemID=B.ItemID AND t.Status='Checked Out')AS CheckedOut, (SELECT D FROM B AS t WHERE t.CustomerID=B.CustomerID AND t.ItemID=B.ItemID AND t.Status='Due Date') AS Due, (SELECT D FROM B AS t WHERE t.CustomerID=B.CustomerID AND t.ItemID=B.ItemID AND t.Status='Bill Sent') AS BillSentFROM B INNER JOIN A ON B.CustomerID=A.CustomerIDGROUP BY B.CustomerID, B.ItemID Keeping the web experience alive -- [url]http://www.web-impulse.com[/url]Imperfection living for perfection -- [url]http://jhermiz.blogspot.com/[/url] |
 |
|
|
Merkin
Funky Drop Bear Fearing SQL Dude!
4970 Posts |
Posted - 2005-01-27 : 23:19:33
|
quote: Originally posted by Stoad (i'm a famous linguist, btw);
A cunning one at that!Damian |
 |
|
|
Stoad
Freaky Yak Linguist
1983 Posts |
Posted - 2005-01-28 : 03:02:01
|
| Cindy;oops! Of course, my query triples each record. Try jon's suggestion.lol, Damian... i'm forced to be cunning - being at work, it is not easy,sometimes, to concentrate on (or over?) a forum's question. |
 |
|
|
ehorn
Master Smack Fu Yak Hacker
1632 Posts |
Posted - 2005-01-31 : 19:26:04
|
quote: Originally posted by Stoad No, Cindy -- CASE is not your case (i'm a famous linguist, btw);
Might this be a case for CASE  select b.CustomerID, b.itemid, max(case when b.status = 'checked out' then date end) as CheckedOutDate, max(case when b.status = 'Due Date' then date end) as DueDate, max(case when b.status = 'Billsent' then date end) as BillSentDatefrom tableB binner join tableA a on a.CustomerID = b.CustomerIDgroup by customerid,itemid |
 |
|
|
|