1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

  2. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

How to Count Distinct on Case When?

Discussão em 'StackOverflow' iniciado por fdantas, Março 15, 2019.

  1. fdantas

    fdantas Administrator Moderador

    I have been building up a query today and I have got stuck. I have two unique Ids that identify if and order is Internal or Web. I have been able to split this out so it does the count of how many times they appear but unfortunately it is not providing me with the intended result. From research I have tried creating a Count Distinct Case When statement to provide me with the results.

    Please see below where I have broken down what it is doing and how I expect it to be.

    Original data looks like:

    Company Name Order Date Order Items Orders Value REF
    -------------------------------------------------------------------------------
    CompanyA 03/01/2019 Item1 Order1 170 INT1
    CompanyA 03/01/2019 Item2 Order1 0 INT1
    CompanyA 03/01/2019 Item3 Order2 160 WEB2
    CompanyA 03/01/2019 Item4 Order2 0 WEB2


    How I expect it to be:

    Company Name Order Date Order Items Orders Value WEB INT
    -----------------------------------------------------------------------------------------
    CompanyA 03/01/2019 4 2 330 1 1


    What currently comes out

    Company Name Order Date Order Items Orders Value WEB INT
    -----------------------------------------------------------------------------------------
    CompanyA 03/01/2019 4 2 330 2 2


    As you can see from my current result it is counting every line even though it is the same reference. Now it is not a hard and fast rule that it is always doubled up. This is why I think I need a Count Distinct Case When. Below is my query I am currently using. This pull from a Progress V10 ODBC that I connect through Excel. Unfortunately I do not have SSMS and Microsoft Query is just useless.

    My Current SQL:

    SELECT

    Company_0.CoaCompanyName
    , SopOrder_0.SooOrderDate
    , Count(DISTINCT SopOrder_0.SooOrderNumber) AS 'Orders'
    , SUM(CASE WHEN SopOrder_0.SooOrderNumber IS NOT NULL THEN 1 ELSE 0 END) AS 'Order Items'
    , SUM(SopOrderItem_0.SoiValue) AS 'Order Value'
    , SUM(CASE WHEN SopOrder_0.SooParentOrderReference LIKE 'INT%' THEN 1 ELSE 0 END) AS 'INT'
    , SUM(CASE WHEN SopOrder_0.SooParentOrderReference LIKE 'WEB%' THEN 1 ELSE 0 END) AS 'WEB'

    FROM

    SBS.PUB.Company Company_0
    , SBS.PUB.SopOrder SopOrder_0
    , SBS.PUB.SopOrderItem SopOrderItem_0

    WHERE

    SopOrder_0.SopOrderID = SopOrderItem_0.SopOrderID
    AND Company_0.CompanyID = SopOrder_0.CompanyID
    AND SopOrder_0.SooOrderDate > '2019-01-01'

    GROUP BY

    Company_0.CoaCompanyName
    , SopOrder_0.SooOrderDate


    I have tried using the following line but it errors on me when importing:

    , Count(DISTINCT CASE WHEN SopOrder_0.SooParentOrderReference LIKE 'INT%' THEN SopOrder_0.SooParentOrderReference ELSE 0 END) AS 'INT'


    Just so know the error I get when importing at the moment is syntax error at or about "CASE WHEN sopOrder_0.SooParentOrderRefer" (10713)

    Continue reading...

Compartilhe esta Página