I'm trying to write a query to compare the number of customers with visits and the number of customers who buy per year. If I do the query for the individual year without Group By, the number is correct, but I need it as a correct table in the system to be able to continue working with it.
This is the one with the incorrect results.
SELECT
COUNT (DISTINCT a.I_CUSTOMER_M) AS 'Visited Customer',
COUNT (DISTINCT c.S_CUSTNO) AS 'Buying Customer',
year ( i.D_INVOICEDATE) AS 'Year'
FROM
sao.customer_M c
LEFT JOIN sao.invoice_p i
on i.I_CUSTOMER_M = c.I_CUSTOMER_M
LEFT JOIN sao.ACTIVITY_P a
on (i.I_customer_M = a.I_CUSTOMER_M and a.I_ACTIVTYPE_C = 11
OR i.I_CUSTOMER_M IS NULL )
WHERE
i.i_invoice_p>0 AND
i.B_PROFORMA = 0 and
i.B_CREDITNOTE = 0 and
i.B_CANCEL = 0 and
i.dt_deleted IS NULL AND
datepart(yyyy,getdate()) - datepart(yyyy,i.D_INVOICEDATE) <= 2
GROUP BY
year (i.D_INVOICEDATE)
Visited Customer | Buying Customer | Year |
---|---|---|
507 | 1490 | 2021 |
509 | 1452 | 2022 |
438 | 1143 | 2023 |
And here is an example if I just make the query for one single year.
SELECT
COUNT (DISTINCT a.I_CUSTOMER_M) AS 'Visited Customer',
COUNT (DISTINCT c.S_CUSTNO) AS 'Buying Customer',
year ( i.D_INVOICEDATE) AS 'Year'
FROM
sao.customer_M c
LEFT JOIN sao.invoice_p i
on i.I_CUSTOMER_M = c.I_CUSTOMER_M
LEFT JOIN sao.ACTIVITY_P a
on (i.I_customer_M = a.I_CUSTOMER_M and a.I_ACTIVTYPE_C = 11
and datepart(yyyy,getdate()) - datepart(yyyy,a.D_ACTIVITYDATE) = 1 OR i.I_CUSTOMER_M IS NULL )
WHERE
i.i_invoice_p>0 AND
i.B_PROFORMA = 0 and
i.B_CREDITNOTE = 0 and
i.B_CANCEL = 0 and
i.dt_deleted IS NULL AND
datepart(yyyy,getdate()) - datepart(yyyy,i.D_INVOICEDATE) = 1
GROUP BY YEAR(i.D_INVOICEDATE)
Visited Customer | Buying Customer | Year |
---|---|---|
235 | 1452 | 2022 |
So as you can see the count of the Visited Customer has a big difference. Everything else is fine, not sure what I#m doing wrong. Can someone please help me?
1条答案
按热度按时间cwdobuhd1#
When asking questions like this it's very helpful to provide example DDL/DML. It helps folks who want to answer your question by detailing exactly what your data looks like.
Here's some pretty basic example DDL/DML which is hopefully enough to show where I think you've gotten hung up.
All we're doing here is creating a few table variables, and populating them with some random data. The customers are just random pairings of first and last names, and the invoices/activities are randomly generated dates at some point between today and three years ago.
I think you're getting incorrect values because of the relationship between the data only being valid where there's commonality, perhaps where a customer visited and purchased? If you segregate those aggregates first, and then bring them back together you should find the numbers you're looking for:
First we aggregate up the data from Invoices, and then the same for visits. The third CTE just gives us a list of years with visits or activity. Finally, we use the CTEs and join them on the year.
You do have some work to do here, to make this applicable to your data set. You should be able to take your queries and replace the collection CTEs with appropriate queries.
no data table as the results are random and won't match anyway ;) the
WHERE
for the collection CTEs serves no purpose but as an example.