[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Group By and Function

Posted on 2011-02-17
5
Medium Priority
?
218 Views
Last Modified: 2012-05-11
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?
---***** 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

Open in new window

0
Comment
Question by:Camillia
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 60

Expert Comment

by:HainKurt
ID: 34919952
looks like this is correct

       /*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,*/

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?
0
 
LVL 60

Accepted Solution

by:
HainKurt earned 2000 total points
ID: 34919965
maybe you can use

sum(dbo.fnCalculateCutOffSum (o.PO_Date,'FN','UPSA')) as Today

0
 
LVL 7

Author Comment

by:Camillia
ID: 34920069
>>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?
0
 
LVL 60

Expert Comment

by:HainKurt
ID: 34920114
the function will be called for each row in the group... so, yes, you should return something for each row, not for whole group...
0
 
LVL 7

Author Comment

by:Camillia
ID: 34952820
I will close it but dont quite get it. Will open another one if i dont figure it out.
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question