Camillia
asked on
Group By and Function
This is SQL 2000....
I have the SQl below. I replaced that SUM statement with a function. This gets a summary. Since I added the function call, I need
to add "group by" at the end of the SQL. But that will cause it to give lots of of rows..because of that PO_Date. How can I change this OR change
the function to get me the same rows/results as that original SUM?
I have the SQl below. I replaced that SUM statement with a function. This gets a summary. Since I added the function call, I need
to add "group by" at the end of the SQL. But that will cause it to give lots of of rows..because of that PO_Date. How can I change this OR change
the function to get me the same rows/results as that original SUM?
---***** Function ************----
ALTER FUNCTION [dbo].[fnCalculateCutOffSum]
(@orderDate datetime, @warehouse varchar(2),@ShipMethodAbbrev varchar(10))
RETURNS int
AS
BEGIN
-- *** Used in WHT_OrderSummary
declare @Today int
-- minus 45 mins..deduct that from @orderDate
select @Today = sum(case
when datepart(hour,DateAdd(minute,-45, @orderDate)) <= Datepart(Hour,cutoffHour)
Then 1 else 0
end) --compare hour portion of dates
from nbCutOff
where --Priority = @priority
warehouse = @warehouse
and ShipMethodAbbrev = @ShipMethodAbbrev
return @Today
end
------------ SQL ------------------
ALTER PROCEDURE [dbo].[OrderSummaryTest]
AS
BEGIN
SELECT CASE o.Customer_ID
WHEN 100 THEN 'X'
WHEN 200 THEN 'Y'
WHEN 400 THEN 'W'
WHEN 500 THEN 'P'
ELSE 'B'
END AS Company,
sum(...) AS UPS,
sum(...) AS AIR,
sum(..) AS USPS,
sum(..) AS MAN,
dbo.fnCalculateCutOffSum (o.PO_Date,'FN','UPSA') as Today,--added this but then i need to add "group by" for o.PO_Date
/*sum(CASE
WHEN o.PO_Date < dateadd(hour,19,CONVERT(varchar,getdate(),101)) AND c.Abbrev_Name = 'UPSA' THEN 1
WHEN o.PO_Date < dateadd(hour,14,CONVERT(varchar,getdate(),101)) AND c.Abbrev_Name = 'USPS' THEN 1
WHEN o.PO_Date < dateadd(hour,15,convert(varchar,getdate(),101)) THEN 1
ELSE 0
END) AS Today,*/
count(*) AS Total,
FROM Orders o
.......
GROUP BY
CASE o.Customer_ID
WHEN 100 THEN 'X'
WHEN 200 THEN 'Y'
WHEN 400 THEN 'W'
WHEN 500 THEN 'P'
ELSE 'B'
END
ORDER BY
1 desc
END
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
>>but above statement will run for each row in the group...
I want to run function to run for each row...basically, it should be the same results..
Let me try your second suggestion...the thing is...the function is already summing up...so maybe remove the sum from the function?
I want to run function to run for each row...basically, it should be the same results..
Let me try your second suggestion...the thing is...the function is already summing up...so maybe remove the sum from the function?
the function will be called for each row in the group... so, yes, you should return something for each row, not for whole group...
ASKER
I will close it but dont quite get it. Will open another one if i dont figure it out.
/*sum(CASE
WHEN o.PO_Date < dateadd(hour,19,CONVERT(va
WHEN o.PO_Date < dateadd(hour,14,CONVERT(va
WHEN o.PO_Date < dateadd(hour,15,convert(va
ELSE 0
END) AS Today,*/
you cannot use the function here, that function will be called once, but above statement will run for each row in the group... whats the problem with the code above?