SQL - 根据值拆分列

时间:2015-10-12 13:06:42

标签: sql sybase

我正在尝试根据列中的值将结果集中的列拆分为2列。 因此,用户可以订阅多个项目,并且用户可以拥有2个可以接收此订阅的电子邮件地址。 结果集提供订阅列表以及订阅的电子邮件ID的相应条目。

数据库详细信息

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

现在我需要用户的所有订阅,无论是否通过任何一个电子邮件ID订阅。 所以我得到了一个像这样的结果集

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

所以我想把上面的结果集分成如下所示

+-------------------+---------+---------+
| 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       |
+-------------------+---------+---------+

希望这个问题有道理。任何帮助将不胜感激!

更新了-----------

示例数据:

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 | +---------+----------+-----------------+

预期结果:

对于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 | +-----------------+-------------------+--------+--------+

3 个答案:

答案 0 :(得分:1)

您需要条件聚合:

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

答案 1 :(得分:1)

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 

答案 2 :(得分:0)

之前我没有在sybase工作,但我很确定以下SQL会轻松翻译(甚至直接运行):

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
)
;

你也可以在IF(email_1.subscription_id IS NOT NULL, 'Y', 'N')中说SELECT等,以便直接返回是/否而非计数等。

它的工作原则是LEFT JOIN语句列表与email_id=1email_id=2等任何“用户订阅”记录相匹配。

我缺乏sybase知识免责声明: ANSI SQL无法执行PIVOT - 如果是sybase,你可以更优雅地做到这一点我敢肯定。还有一个问题+答案提示sybase可以做这些事情;在那里寻找它是值得的:https://stackoverflow.com/a/8114446/817132

希望它有所帮助!