Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
a2l2en
Starting Member
11 Posts |
Posted - 2007-06-27 : 18:13:54
|
| I have a table with customer information.CustID, Name, email, contract_id222, Joy, joy@mail.com, Cash222, Joy, joy@mail.com, Credit444, Bob, bob@mail.com, Cash123, Jon, jon@mio.com, Cash123, Jon, jon@mio.com, Credit123, Jon, jon@mio.com, CheckThere can only be three values in the contract_id column - Cash, Credit or Check. All customers have at least "Cash" for contract_id, however, they can have additional records for Credit or Check. I don't have write access to the database so I have to write a query that separates them out. I've tried to create table aliases and do outer joins but it results in only giving me back the customers that have cash, credit and check. I miss all other combinations...Any suggestions?Results need to look like this:CustID| Cash| Credit| Check******************************123 cash credit check444 cash null null222 cash credit nullI only have read access and need a query to do this. Help :-) |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2007-06-27 : 18:30:08
|
| Here is one way: [CODE]SELECT CustID, CASE WHEN EXISTS (SELECT 1 FROM @T WHERE Contract_ID = 'Cash' AND CustID = t.CustID) THEN 'Cash' END AS Cash, CASE WHEN EXISTS (SELECT 1 FROM @T WHERE Contract_ID = 'Credit' AND CustID = t.CustID) THEN 'Credit' END AS Credit, CASE WHEN EXISTS (SELECT 1 FROM @T WHERE Contract_ID = 'Check' AND CustID = t.CustID) THEN 'Check' END AS "Check"FROM @T tGROUP BY CustID [/CODE] |
 |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2007-06-27 : 18:39:12
|
Here is another way with joins (hehe, got bored wating on a job to complete):SELECT DISTINCT(t.CustID), CASE WHEN t.Contract_ID = 'Cash' THEN 'Cash' END AS Cash, CASE WHEN t1.Contract_ID = 'Credit' THEN 'Credit' END AS Credit, CASE WHEN t2.Contract_ID = 'Check' THEN 'Check' END AS "Check"FROM @T tLEFT OUTER JOIN @T t1 ON t.CustID = t1.CustID AND t.Contract_ID = 'cash' AND t1.Contract_ID = 'credit'LEFT OUTER JOIN @T t2 ON t.CustID = t2.CustID AND t.Contract_ID = 'cash' AND t2.Contract_ID = 'check'WHERE t.Contract_ID = 'Cash' |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-06-28 : 02:20:14
|
| [code]SELECT CustID, MAX(CASE WHEN Contract_ID = 'Cash' THEN 'Cash' END) AS Cash, MAX(CASE WHEN Contract_ID = 'Credit' THEN 'Credit' END) AS Credit, MAX(CASE WHEN Contract_ID = 'Check' THEN 'Check' END) AS [Check]FROM CustomerInformationTableGROUP BY CustIDORDER BY CustID[/code]Peter LarssonHelsingborg, Sweden |
 |
|
|
Koji Matsumura
Posting Yak Master
141 Posts |
Posted - 2007-06-28 : 02:58:28
|
| We know that 'Cash' exists for each cutomer.SELECT A.CustID, 'Cash',CASE WHEN EXISTS (SELECT COUNT(*) FROM CustomerInformationTable Z WHERE Z.CustID = A.CustID AND Z.Contract_ID = 'Credit')THEN 'Credit' END,CASE WHEN EXISTS (SELECT COUNT(*) FROM CustomerInformationTable Z WHERE Z.CustID = A.CustID AND Z.Contract_ID = 'Check')THEN 'Check' ENDFROM CustomerInformationTable AWHERE A.Contract_ID = 'Cash'ORDER BY A.CustID |
 |
|
|
|
|
|
|
|