【问题标题】:I need to merge two separate queries using CASE statement我需要使用 CASE 语句合并两个单独的查询
【发布时间】:2015-11-23 21:13:07
【问题描述】:

所以我需要使用 CASE 语句合并这两个单独的查询。我的总体目标是“我希望找到所有直播和免费直播的地点,而不是演示。这些地点必须位于美国或加拿大。找出这些地点有多少客户填写了他们的国家/地区不是美国或加拿大,而不是他们有多少客户。”

所以我把两个查询都写出来了,它们都是分开工作的。

这是第一个返回非美国客户的。

SELECT 
    COUNT(1) AS "Not in US or Canada verse Total Customers" 
FROM 
    dbo.Spa (nolock)
LEFT JOIN 
    dbo.Customer ON dbo.Customer.SpaID = dbo.Spa.ID
WHERE 
    dbo.Spa.TimeZoneID IN (6, 9, 12, 18) 
    AND dbo.Spa.IsDeleted = 0
    AND dbo.Spa.StatusID IN (3, 4)
    AND dbo.Customer.CountryID != 1
    AND dbo.Customer.countryID != 2;

这是返回所有客户的第二个。

SELECT 
    COUNT(1) AS "Total Customers" 
FROM 
    dbo.Spa (nolock)
LEFT JOIN 
    dbo.Customer ON dbo.Customer.SpaID = dbo.Spa.ID
WHERE 
    dbo.Spa.TimeZoneID IN (6, 9, 12, 18) 
    AND dbo.Spa.IsDeleted = 0
    AND dbo.Spa.StatusID IN (3, 4);

谁能帮我用case语句把这些放在一起,它会使用CASE语句在一个表中显示不在美国和加拿大的客户和总客户。

谢谢!

【问题讨论】:

    标签: sql sql-server


    【解决方案1】:

    如果我理解你的问题,我认为这就是你要找的:

    SELECT      COUNT(1) AS "All customers",
                SUM(CASE WHEN (dbo.Customer.CountryID != 1 AND dbo.Customer.countryID != 2)
                    THEN 1 ELSE 0 END) AS "Not in US or Canada verse Total Customers"
    FROM        dbo.Spa (nolock)
    LEFT JOIN   dbo.Customer    ON  dbo.Customer.SpaID = dbo.Spa.ID
    WHERE       dbo.Spa.TimeZoneID IN(6,9,12,18) 
    AND         dbo.Spa.IsDeleted= 0
    AND         dbo.Spa.StatusID IN (3,4)
    

    【讨论】:

      【解决方案2】:

      您可以使用 case 表达式来做到这一点。

      SELECT sum(case when dbo.Customer.CountryID <> 1 AND dbo.Customer.countryID <> 2 then 1 end) as NotInUS
          , COUNT(1) AS "Not in US or Canada verse Total Customers" 
      FROM dbo.Spa (nolock)
      LEFT JOIN dbo.Customer ON dbo.Customer.SpaID = dbo.Spa.ID
      WHERE dbo.Spa.TimeZoneID IN(6,9,12,18) 
      AND dbo.Spa.IsDeleted= 0
      AND dbo.Spa.StatusID IN (3,4)
      

      此外,您可能应该改掉将 NOLOCK 溅到各处的习惯。它有一些非常严重且经常被误解的副作用。 http://blogs.sqlsentry.com/aaronbertrand/bad-habits-nolock-everywhere/

      【讨论】:

        【解决方案3】:

        您可以让每个查询成为子选择:

        SELECT (query1) as '', (query2) as ''
        

        【讨论】:

          【解决方案4】:

          在您的第一个查询中,您使用左连接,然后过滤 CountryID,这将消除没有匹配国家/地区的 Spa 行。实际上可能没有,但您应该知道,由于该更改,您可能会在总计中获得不同的计数。如果每个“Spa”(?)不能有超过一个客户,那么加入该表(在第二个查询中)甚至不会完成任何事情,但很难猜测您是否打算这样做。

          select
              count(case when c.CountryID not in (1, 2) then 1 else null end),
              count(*)
          from
              dbo.Spa as s
              left outer join dbo.Customer as c
                  on c.SpaID = s.SpaID
          where
                  s.TimeZoneID in (6, 9, 12, 18)
              and s.IsDeleted = 0
              and s.StatusID in (3, 4)
          

          【讨论】:

            【解决方案5】:

            这是一个如何在 COUNT() 中使用 CASE 语句来仅计算指定组的示例:

                WITH
                /*
                Get a list of all customers and their country to use as the main data set as an example
                */
                mainSet AS
                    (
                    SELECT
                        contact.FirstName,
                        address.city,
                        stateProvince.countryRegionCode 
                    FROM person.address AS address
                    INNER JOIN sales.customerAddress AS customerAddress ON
                        address.addressID = customerAddress.addressID
                    INNER JOIN sales.customer AS customer ON
                        customerAddress.customerID = customer.customerID
                    INNER JOIN person.contact AS contact ON 
                        customer.customerID = contact.contactID
                    INNER JOIN person.stateProvince AS stateProvince ON
                        address.stateProvinceID = stateProvince.stateProvinceID
                    )
            
                SELECT
                    --------------------------------------------------Count all customers using COUNT()
                    COUNT(countryRegionCode) AS 'All Customers Count',
            
                    --------------------------------------------------Count all customers using COUNT() with a CASE statement
                    COUNT(CASE WHEN countryRegionCode NOT IN('US','CA') THEN countryRegionCode END) AS 'Customers not in US/CA'
                FROM mainSet
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2012-12-26
              • 1970-01-01
              • 1970-01-01
              • 2017-09-17
              • 1970-01-01
              • 2012-08-21
              • 1970-01-01
              相关资源
              最近更新 更多