Sql左连接加CASE查询

时间:2016-12-05 23:33:28

标签: sql sql-server database left-join

我一直在使用数据库访问,但是我第一次使用MS SQL server 2012,因此,我必须编写查询,因为我发现查询构建器不像访问那样友好。

我坚持的问题不是一个非常具有挑战性的问题。我有两个表如下:

  • dbo.WF_PM_ACCT_DET_DB with columns to use Account, Postal_Code, Client_Birth_Date
  • dbo.WF_PM_SUM_DB with columns to use Account, Total_Avg_AuA

我想要做的是查询结果按邮政编码分组,计算相应邮政编码中客户的数量,还按年龄分组,其中AuA是> 0

首先,我需要按年龄(<35,35-55和&gt; 55)将客户分为3组。然后计算该特定年龄组中特定邮政编码中的客户数量。同时仅选择第二个表中总和(AuA)> 0的客户端。输出看起来像这样

Postal code   CountofClient Age_group
1m1 2m2          50            <35
1m1 2m2          20           35-50
and so on...
到目前为止,这就是我所拥有的:

SELECT        Postal_Code, COUNT(Account) AS Count_Account, Age,                 
AVG(YEAR(GETDATE()) - YEAR(client_birth_date)) AS 'Age'

CASE WHEN AGE <=35                       THEN 'AGEGROUPA'
       WHEN AGE >35 AND AGE <55          THEN 'AGEGROUPB'
       WHEN AGE >=55                     THEN 'AGEGROUPC'
end as 'clientageseg'

FROM            dbo.WF_PM_ACCT_DET_DB
GROUP BY Postal_Code, Client_Birth_Date, Age

任何帮助将不胜感激。谢谢。

3 个答案:

答案 0 :(得分:1)

;WITH cte AS (
    SELECT
       postal_code
       ,Account
       ,CASE
          WHEN
             (DATEADD(year,DATEDIFF(year, [DOB]  ,GETDATE()) , [DOB]) > GETDATE())
          THEN DATEDIFF(year, [DOB]  ,GETDATE()) -1
          ELSE DATEDIFF(year, [DOB]  ,GETDATE())
       END as Age
   ,SUM(s.AuA) OVER (PARTITION BY a.Account) as AuASum
    FROM
       dbo.WF_PM_ACCT_DET_DB a
       INNER JOIN dbo.WF_PM_SUM_DB s
   ON a.Account = s.Account
)

SELECT
    postal_code
    ,COUNT(Account) as Count_Account
    ,CASE
       WHEN Age <= 35 THEN 'AGEGROUPA'
       WHEN Age > 35 AND Age < 55 THEN 'AGEGROUPB'
       WHEN Age >= 55 THEN 'AGEGROUPC'
    END as ClientAgeSeg
FROM
    cte
WHERE
   AuASum > 0
GROUP BY
    postal_code
    ,CASE
          WHEN Age <= 35 THEN 'AGEGROUPA'
          WHEN Age > 35 AND Age < 55 THEN 'AGEGROUPB'
          WHEN Age >= 55 THEN 'AGEGROUPC'
   END

欢迎使用Acess的SQL Server!最后你会喜欢它,你可以做更多的事情。当开始设置一些很好的功能时,公用表表达式[CTE] 窗口函数。我在上面的例子中使用了CTE。你没有必要,但是当你进行日期比较时,它会让你的生活更轻松。

请注意,在计算年龄时,您需要确定生日是否已在一年中发生或者是否仍未来。

答案 1 :(得分:0)

试试这个:

SELECT  DISTINCT  Postal_Code,
        COUNT(Account)OVER (PARTITION BY Postal_Code) AS Count_Account,
        Age,                 
        AVG(YEAR(GETDATE()) - YEAR(client_birth_date)) AS 'Age',
        CASE WHEN AGE <=35                       THEN 'AGEGROUPA'
             WHEN AGE >35 AND AGE <55          THEN 'AGEGROUPB'
             WHEN AGE >=55                     THEN 'AGEGROUPC'
        END as 'clientageseg'
FROM dbo.WF_PM_ACCT_DET_DB A
LEFT JOIN dbo.WF_PM_SUM S ON A.Account=S.AccountNumber
WHERE S.AccountNumber IS NOT NULL

答案 2 :(得分:0)

您可以尝试先使用CTE对数据进行分组,然后应用案例:

;WITH CTE AS
(
    SELECT        
        Postal_Code, 
        COUNT(Account) AS Count_Account,                 
        AVG(YEAR(GETDATE()) - YEAR(client_birth_date)) AS 'Age'
    FROM 
        dbo.WF_PM_ACCT_DET_DB
    GROUP BY Postal_Code
)
SELECT
    Postal_Code,
    Count_Account,
    CASE   
    WHEN AGE <= 35             THEN 'AGEGROUPA'
    WHEN AGE > 35 AND AGE < 55 THEN 'AGEGROUPB'
    WHEN AGE >= 55             THEN 'AGEGROUPC'
    END AS 'clientageseg'
FROM CTE