Click here to Skip to main content
16,022,060 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I’m having trouble keeping the correct count within the correct date.

I am counting order numbers that have an ‘AA’ in the code column and an ‘in’ under the post column. Then checking if that order number has an ‘EE’ in the code column and ‘out’ in the post column. If all that’s true, I count as 1.

Next I would like to see if the order number has an ‘AA’ in the code column and ‘out’ in the post column. If that’s true, I would count that as 1.

The problem I’m having is that the ‘EE’ in the code column and ‘out’ in the post column are counted in one day, but the ‘AA’ in the code column and ‘out’ in the post column is counted in another day.

For example, a customer orders a product on one day but part of the order fails (EE=failed) later in the same day. The shop opens the next day, corrects the order and then its successful but since it was successful the next day, its counted that day and not against the original order date. I would like it counted in the original order date column.

**What I’m getting**
DATE		EE-FAIL		AA-GOOD
2024-10-01	  2		        0
2024-10-02	  0		        2


**What I want**
DATE		EE-FAIL		AA-GOOD
2024-10-01	   2		   2
2024-10-02	   0		   0


order_number	date		post	code	account
88728		2024-10-01	     in	     AA	    6063195
88728		2024-10-01	     out	 EE	    6063195
88728		2024-10-02	     out	 AA	    6063195
76347		2024-10-01	     in	     AA	    2854763
76347		2024-10-01	     out	 EE	    2854763
76347		2024-10-02	     out	 AA	    2854763



THANK YOU !!

What I have tried:

CREATE TABLE ##full_table
	(
	order_number INT
	,date date
	,post VARCHAR(5)
	,code VARCHAR(2)
	,account INT
	)

INSERT INTO ##full_table VALUES ('88728','2024-10-01','in','AA','6063195')
INSERT INTO ##full_table VALUES ('88728','2024-10-01','out','EE','6063195')
INSERT INTO ##full_table VALUES ('88728','2024-10-02','out','AA','6063195')
INSERT INTO ##full_table VALUES ('76347','2024-10-01','in','AA','2854763')
INSERT INTO ##full_table VALUES ('76347','2024-10-01','out','EE','2854763')
INSERT INTO ##full_table VALUES ('76347','2024-10-02','out','AA','2854763')


CREATE TABLE ##research_count
	(
	date date
	,fail_count INT
	,good_count INT
	)


INSERT INTO ##research_count (date, fail_count)


SELECT date, COUNT(DISTINCT order_number) AS ‘failed’
FROM ##full_table 
WHERE code = 'EE'
AND post = 'out'
GROUP BY date


UPDATE t
SET good_count = (SELECT COUNT(DISTINCT order_number)
              FROM ##full_table p
              WHERE code = 'AA'
	          AND post = 'out'
              AND p.date = t.date)
FROM ##research_count t
;
Posted

I am starting from your last para. From here it is assumed, your SQL output is based on order placing date. Order placing date is minimum (first date) of order number with value of [post] is 'in'. We can get if from this subquery

SQL
SELECT ORDER_NUMBER, MIN(DATE) AS OrderDate FROM ##full_table WHERE POST='in' GROUP BY ORDER_NUMBER;

In your What I want table shows the row of 2nd October with '0' value in all the column. As no order placed on 2nd of October, how can you get the row in your output, which is based on order date? If you consider this question, here is your answer -
SQL
SELECT B.OrderDate, SUM(CASE WHEN A.CODE='AA'  AND  A.POST='out' THEN 1 ELSE 0 END) as [AA-Good],
SUM(CASE WHEN A.CODE='EE' AND  A.POST='out' THEN 1 ELSE 0 END) as [EE-Fail]
FROM ##full_table A  
LEFT JOIN   (SELECT ORDER_NUMBER, MIN(DATE) AS OrderDate FROM ##full_table WHERE POST='in' GROUP BY ORDER_NUMBER) B 
ON A.ORDER_NUMBER= B.ORDER_NUMBER
WHERE A.ORDER_NUMBER= B.ORDER_NUMBER 
GROUP BY B.OrderDate;

Just join that subquery with table ##full_table on order_number to get the order_date.
It works, I implemented it on SQL Server 2016.
Thank you.
 
Share this answer
 
Comments
Chris_List 5 days ago    
Thank you ... I appreciate it
hint: SUM(CASE WHEN xxx='AA' THEN 1 ELSE 0 END) as [AA-Good]
 
Share this answer
 
Comments
Chris_List 5 days ago    
Thank you !

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900