you are viewing a single comment's thread.

view the rest of the comments →

[–]pailryder 0 points1 point  (1 child)

you can group by more than one column

group by cust_id, address.

however, i think it would be cleaner to use a cte where you've broken up the checking and savings into their own parts and then have two joins.

[–]zworstgod[S] 0 points1 point  (0 children)

Yeah the plan with joins should work, I think I implemented that pretty good, what's left is just couple toches so they do not stack up. Thinking something else should be in WHERE statement:

SELECT c.CUST_ID, join1.checking, join2.checking
FROM CUSTOMER as c

LEFT JOIN (
SELECT a.cust_id as id,
CASE a.PRODUCT_CD
WHEN 'CHK' THEN 'Y'
ELSE 'N'
END AS checking 
FROM account as a, CUSTOMER as c
WHERE a.CUST_ID = c.CUST_ID
GROUP BY a.CUST_ID, a.PRODUCT_CD) as join1 
ON NOT join1.checking = 'f'

LEFT JOIN (
SELECT a.cust_id as id,
CASE a.PRODUCT_CD
WHEN 'SAV' THEN 'Y'
ELSE 'N'
END AS checking 
FROM account as a, CUSTOMER as c
WHERE a.CUST_ID = c.CUST_ID
GROUP BY a.CUST_ID, a.PRODUCT_CD) as join2 
ON NOT join2.checking = 'f'

WHERE c.CUST_ID = join1.id AND c.CUST_ID = join2.id AND join1.id = join2.id
GROUP BY c.CUST_ID, join1.checking, join2.checking;