Hello-
I have a table that needs to be updated with the number of orders each employee submitted depending on the region, state and other variables. But what my update is doing is counting the total of all variables and updating each with the same value. I'm not sure how to count each separately.
Here are two tables, one shows what my update produces (which is not what I want) and the other is what I'm hoping to achieve for each employee. I only updated the number for employee 1 but I'd like to get all employees updated. I apologize for the table view, it didn't copy over well
Not what I want....
business_date day_of_week region state wire emp_01 emp_02 emp_03 emp_04 emp_05
2023-08-06 Sunday North Michigan internal 572 NULL NULL NULL NULL
2023-08-06 Sunday South Texas internal 572 NULL NULL NULL NULL
2023-08-06 Sunday East New York internal 572 NULL NULL NULL NULL
2023-08-06 Sunday West Arizona internal 572 NULL NULL NULL NULL
2023-08-06 Sunday North Wisconsin internal 572 NULL NULL NULL NULL
2023-08-06 Sunday South Florida internal 572 NULL NULL NULL NULL
2023-08-06 Sunday East Maine internal 572 NULL NULL NULL NULL
2023-08-06 Sunday West Oregon internal 572 NULL NULL NULL NULL
2023-08-06 Sunday North Michigan external 572 NULL NULL NULL NULL
2023-08-06 Sunday South Texas external 572 NULL NULL NULL NULL
2023-08-06 Sunday East New York external 572 NULL NULL NULL NULL
2023-08-06 Sunday West Arizona external 572 NULL NULL NULL NULL
2023-08-06 Sunday North Wisconsin external 572 NULL NULL NULL NULL
2023-08-06 Sunday South Florida external 572 NULL NULL NULL NULL
2023-08-06 Sunday East Maine external 572 NULL NULL NULL NULL
2023-08-06 Sunday West Oregon external 572 NULL NULL NULL NULL
What I want...
business_date day_of_week region state wire emp_01 emp_02 emp_03 emp_04 emp_05
2023-08-06 Sunday North Michigan internal 35 NULL NULL NULL NULL
2023-08-06 Sunday South Texas internal 32 NULL NULL NULL NULL
2023-08-06 Sunday East New York internal 46 NULL NULL NULL NULL
2023-08-06 Sunday West Arizona internal 19 NULL NULL NULL NULL
2023-08-06 Sunday North Wisconsin internal 37 NULL NULL NULL NULL
2023-08-06 Sunday South Florida internal 52 NULL NULL NULL NULL
2023-08-06 Sunday East Maine internal 12 NULL NULL NULL NULL
2023-08-06 Sunday West Oregon internal 9 NULL NULL NULL NULL
2023-08-06 Sunday North Michigan external 58 NULL NULL NULL NULL
2023-08-06 Sunday South Texas external 29 NULL NULL NULL NULL
2023-08-06 Sunday East New York external 23 NULL NULL NULL NULL
2023-08-06 Sunday West Arizona external 57 NULL NULL NULL NULL
2023-08-06 Sunday North Wisconsin external 49 NULL NULL NULL NULL
2023-08-06 Sunday South Florida external 39 NULL NULL NULL NULL
2023-08-06 Sunday East Maine external 53 NULL NULL NULL NULL
2023-08-06 Sunday West Oregon external 22 NULL NULL NULL NULL
Thank you !!!!
What I have tried:
CREATE TABLE temp_table
(
business_date DATETIME
,day_of_week VARCHAR(20)
,region VARCHAR(20)
,state VARCHAR(20)
,wire VARCHAR(20)
,emp_01 INT
,emp_02 INT
,emp_03 INT
,emp_04 INT
,emp_05 INT
)
INSERT INTO temp_table (business_date, day_of_week, region, state, wire)
VALUES
('2023-08-06', 'Sunday', 'North', 'Michigan', 'internal')
,('2023-08-06', 'Sunday', 'South', 'Texas', 'internal')
,('2023-08-06', 'Sunday', 'East', 'New York', 'internal')
,('2023-08-06', 'Sunday', 'West', 'Arizona', 'internal')
,('2023-08-06', 'Sunday', 'North', 'Wisconsin', 'internal')
,('2023-08-06', 'Sunday', 'South', 'Florida', 'internal')
,('2023-08-06', 'Sunday', 'East', 'Maine', 'internal')
,('2023-08-06', 'Sunday', 'West', 'Oregon', 'internal')
,('2023-08-06', 'Sunday', 'North', 'Michigan', 'external')
,('2023-08-06', 'Sunday', 'South', 'Texas', 'external')
,('2023-08-06', 'Sunday', 'East', 'New York', 'external')
,('2023-08-06', 'Sunday', 'West', 'Arizona', 'external')
,('2023-08-06', 'Sunday', 'North', 'Wisconsin', 'external')
,('2023-08-06', 'Sunday', 'South', 'Florida', 'external')
,('2023-08-06', 'Sunday', 'East', 'Maine', 'external')
,('2023-08-06', 'Sunday', 'West', 'Oregon', 'external')
UPDATE t
SET t.emp_01 = (SELECT COUNT(DISTINCT p.order_number)
FROM order_history p WITH (NOLOCK)
WHERE p.employee = 'emp_01')
FROM temp_table t WITH (NOLOCK)
INNER JOIN order_history p WITH (NOLOCK)
ON (t.business_date = p.business_date AND t.region = p.region AND t.state = p.state)
WHERE t.business_date = p.business_date
AND t.region = p.region
AND t.state = p.state