简体   繁体   中英

SQL - Splitting a column based on the values

I'm trying to split a column from a result set into 2 columns based on the values from the column. So a user can subscribe to multiple items and the user can have 2 email addresses which can receive this subscription. The result set gives a list of subscriptions and their corresponding entries for subscribed email ids.

DB details

Table 1 - user_subscriptions

user_id 
email_id - 1 for email id 1 and 2 for email id 2
subscription_id

Table 2 - subscriptions 

subscription_id
subscription_name

Now I need all the subscriptions for the user whether subscribed by either of the email ids or not. So I get a result set something like this

+----------------------+----------+
| subscription_name    | email_id |
+----------------------+----------+
| item1                | 1        |
| item1                | 2        |
| item2                | null     |
| item3                | 1        |
| item4                | null     |
| item5                | 2        |
+----------------------+----------+

So I'm looking to split the above result set into something like below

+-------------------+---------+---------+
| subscription_name | email_1 | email_2 |

+-------------------+---------+---------+
| item1             | 1 or Y  | 1 or Y  |
| item2             | 0 or N  | 0       |
| item3             | 1       | 0       |
| item4             | 0       | 0       |
| item5             | 0       | 1       |
+-------------------+---------+---------+

Hope this question makes sense. Any help would be appreciated!

Updated -----------

Sample Data:

subscriptions - +-----------------+-------------------+ | subscription_id | subscription_name | +-----------------+-------------------+ | 1 | item1 | | 2 | item2 | | 3 | item3 | | 4 | item4 | | 5 | item5 | +-----------------+-------------------+

user_subscriptions

+---------+----------+-----------------+ | user_id | email_id | subscription_id | +---------+----------+-----------------+ | 101 | 1 | 1 | | 101 | 2 | 1 | | 101 | 1 | 3 | | 101 | 2 | 5 | | 102 | 1 | 1 | | 102 | 2 | 1 | +---------+----------+-----------------+

Expected Result:

For user_id = 101

+-----------------+-------------------+--------+--------+ | subscription_id | subscription_name | mail_1 | mail_2 | +-----------------+-------------------+--------+--------+ | 1 | item1 | Y | Y | | 2 | item2 | N | N | | 3 | item3 | Y | N | | 4 | item4 | N | N | | 5 | item5 | N | Y | +-----------------+-------------------+--------+--------+

You need a conditional aggregate:

select us.subscription_name,
     -- there's at least one email
   CASE WHEN MIN(us.email_id) IS NOT NULL THEN 'Y' ELSE 'N' END as email_1,
     -- there's more than one email
   CASE WHEN MIN(us.email_id) <> MAX(us.email_id) THEN 'Y' ELSE 'N' END as email_2
from subscriptions as s
left join user_subscriptions as us
on s.subscription_id = us.subscription_id
where us.user_id = ...  
group by us.subscription_name
SELECT 
     S.subscription_id, 
     S.subscription_name,
     CASE 
         WHEN US1.mail_ID IS NULL THEN 'N'
         ELSE 'Y'
     END mail_1,
     CASE 
         WHEN US2.mail_ID IS NULL THEN 'N'
         ELSE 'Y'
     END mail_2
FROM subscriptions S
LEFT JOIN user_subscriptions US1
  ON S.subscription_id = US1.subscription_id
 AND US1.mail_id = 1
LEFT JOIN user_subscriptions US2
  ON S.subscription_id = US2.subscription_id
 AND US2.mail_id = 2
WHERE us1.user_id = 5 -- or use a variable @user_ID
  OR  us2.user_id = 5 

I've not worked in sybase before, but I'm fairly sure the following SQL will translate easily (or even run directly):

SELECT
    s.subscription_name,
    COUNT(email_1.subscription_id) AS email_1,
    COUNT(email_2.subscription_id) AS email_2
FROM subscriptions AS s
LEFT JOIN user_subscriptions AS email_1 ON (
    s.subscription_id = email_1.subscription_id AND
    email_1.email_id = 1
)
LEFT JOIN user_subscriptions AS email_2 ON (
    s.subscription_id = email_2.subscription_id AND
    email_2.email_id = 2
)
;

You could also say IF(email_1.subscription_id IS NOT NULL, 'Y', 'N') etc in the SELECT to return a straight-forward yes/no rather than a count etc.

It works on the principle that the list of LEFT JOIN statements will match any "user subscription" record with email_id=1 and email_id=2 etc.

My lack of sybase knowledge disclaimer: ANSI SQL is can't perform PIVOT - if sybase does, you could do this far more elegantly I'm sure. There's another question+answer which hints that sybase can do such things; it'd be worth your while looking there: https://stackoverflow.com/a/8114446/817132

Hope it helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM