SQL Server多次从同一列中选择数据

时间:2014-04-09 12:05:16

标签: sql sql-server

我有2个表格,其中包含以下数据和结构:

Companies:                                          Contacts:  

CompanyID | UserDefine4                             CompanyID  | Comment       
-------      --------                                ------    | ------
   1      |  Register 20.03.2014                      1        | Newsletter   
   2      |  Subscribe 21.03.2014                     2        | Newsletter   
   3      |  Register 01.04.2014                      3        | Newsletter
   4      |  Unsubscribe 02.04.2014                   4        | Newsletter
   5      |  Unsubscribe 03.04.2014                   5        | Newsletter
   6      |  Subscribe 05.04.2014                     6        | Newsletter

在这两个表中,我想根据{field}中的UserDefined4 groupby日期部分中的文本(注册,订阅,取消订阅)选择UserDefine4

我正在运行以下查询以单独选择记录:但我不知道如何加入下面提到的3个查询。

第1步:

Select Right(Companies.UserDefined4, 10) As DateSubscribe, Count(Companies.ID) As NumberofSubscribe
    From Companies, Contacts
    Where Companies.ID = Contacts.CompanyID And 
    (Contacts.Comment Like 'Newsletter' And Companies.UserDefined4 Like '%Subscribe%' And
      (Right(Companies.UserDefined4, 10) Like '%.03.2014' Or Right(Companies.UserDefined4, 10) Like '%.04.2014'))
    Group By Right(Companies.UserDefined4, 10)

第2步:

Select Right(Companies.UserDefined4, 10) As DateRegister, Count(Companies.ID) As NumberofRegister
    From Companies, Contacts
    Where Companies.ID = Contacts.CompanyID And 
    (Contacts.Comment Like 'Newsletter' And Companies.UserDefined4 Like '%Register%' And
      (Right(Companies.UserDefined4, 10) Like '%.03.2014' Or Right(Companies.UserDefined4, 10) Like '%.04.2014'))
    Group By Right(Companies.UserDefined4, 10)

第3步:

Select Right(Companies.UserDefined4, 10) As DateUnsubscribe, Count(Companies.ID) As NumberofUnsubscribe
    From Companies, Contacts
    Where Companies.ID = Contacts.CompanyID And 
    (Contacts.Comment Like 'Newsletter' And Companies.UserDefined4 Like '%Unsubscribe%' And
      (Right(Companies.UserDefined4, 10) Like '%.03.2014' Or Right(Companies.UserDefined4, 10) Like '%.04.2014'))
    Group By Right(Companies.UserDefined4, 10)

3 个答案:

答案 0 :(得分:2)

UserDefined4列分成两列,然后将其转换为数字(1 or 0),我们可以在最终结果上执行SUM日期计数:

 SELECT SubscriptionDate,
        SUM(IsRegistration) AS NoOfRegistrations,
        SUM(IsSubscription) AS NoOfSubscriptions,
        SUM(IsUnsubscription) AS NoOfUnsubscriptions
   FROM (
        -- Separate UserDefined4 into SubscriptionType and SubscriptionDate
         SELECT CompanyId,
                CASE
                     WHEN SUBSTRING(UserDefined4, 1, LEN(UserDefined4) - 8) = 'Register' THEN 1
                     ELSE 0
                END AS IsRegistration,
                CASE
                     WHEN SUBSTRING(UserDefined4, 1, LEN(UserDefined4) - 9) = 'Subscribe' THEN 1
                     ELSE 0
                END AS IsSubscription,
                CASE
                     WHEN SUBSTRING(UserDefined4, 1, LEN(UserDefined4) - 11) = 'Unsubscribe' THEN 1
                     ELSE 0
                END AS IsUnsubscription,
                RIGHT(UserDefined4, 10) AS SubscriptionDate
           FROM Companies
         ) AS t
  GROUP BY SubscriptionDate;

编辑2014年4月14日:更正了SUBSTRING长度以匹配实际单词的长度

答案 1 :(得分:1)

使用PIVOT可以在这里提供帮助,假设输出可以作为源表中每个日期的注册,订阅和取消订阅的数量。查询将是这样的:

SELECT Right(UserDefined4, 10) as 'Date', 
[R] as 'Number of Register', 
[S] as 'Number of Subscribe', 
[U] as 'Number of Unsubscribe'
FROM
(
 SELECT 
 CompanyID, 
 userdefined4, 
 LEFT(userdefined4, 1) typ 
 FROM companies join 
 contacts on companies.companyid = contacts.companyid
 WHERE 
 (Right(Companies.UserDefined4, 10) Like '%.03.2014' 
  Or Right(Companies.UserDefined4, 10) Like '%.04.2014'
 )
 AND Contacts.Comment Like 'Newsletter') AS SourceTable
PIVOT
(
 COUNT(CompanyID)
 FOR typ IN ([R], [S], [U])
) AS PivotTable;

答案 2 :(得分:0)

有很多可能的方法可以实现您的目标,但仍取决于您的决定,您希望如何设计输出

选项1

SELECT 
CASE WHEN SUBSTRING(C.UserDefined4,1,1) = 'R' THEN RIGHT(C.UserDefined4,10) 
ELSE ' ' END 'DateRegister',
CASE WHEN SUBSTRING(C.UserDefined4,1,1) = 'R' THEN COUNT(T.CompanyID) 
ELSE ' ' END 'NumberofSubscribe',
CASE WHEN SUBSTRING(C.UserDefined4,1,1) = 'U' THEN RIGHT(C.UserDefined4,10) 
ELSE ' ' END 'DateSubscribe',
CASE WHEN SUBSTRING(C.UserDefined4,1,1) = 'U' THEN COUNT(T.CompanyID) 
ELSE ' ' END 'NumberofSubscribe',
CASE WHEN SUBSTRING(C.UserDefined4,1,1) = 'S' THEN RIGHT(C.UserDefined4,10) 
ELSE ' ' END 'DateUnsubscribe',
CASE WHEN SUBSTRING(C.UserDefined4,1,1) = 'S' THEN COUNT(T.CompanyID) 
ELSE ' ' END 'NumberofSubscribe'
FROM COMPANIES C
INNER JOIN Contacts T ON T.CompanyID = C.ID
WHERE (T.Comment  = 'Newsletter' OR T.Comment LIKE '%Newsletter%')
AND (RIGHT(C.UserDefined4,10) LIKE '%.03.2014' 
OR RIGHT(C.UserDefined4,10) LIKE'%.04.2014')
GROUP BY  SUBSTRING(C.UserDefined4,1,1),RIGHT(C.UserDefined4,10)

选项2

SELECT RIGHT(C.UserDefined4,10) 'Date',
CASE WHEN SUBSTRING(C.UserDefined4,1,1) = 'R' THEN 'Register'
WHEN SUBSTRING(C.UserDefined4,1,1) = 'S' THEN 'Subscribe'
WHEN SUBSTRING(C.UserDefined4,1,1) = 'U' THEN 'Unsubscribe' END 'Action',
COUNT(SUBSTRING(C.UserDefined4,1,1)) 'NumberOfSubscriber'
FROM COMPANIES C
INNER JOIN Contacts T ON T.CompanyID = C.ID
WHERE (T.Comment  = 'Newsletter' OR T.Comment LIKE '%Newsletter%')
AND (RIGHT(C.UserDefined4,10) LIKE '%.03.2014' 
OR RIGHT(C.UserDefined4,10) LIKE'%.04.2014')
GROUP BY  SUBSTRING(C.UserDefined4,1,1),RIGHT(C.UserDefined4,10)