| Author | Topic | 
                            
                                    | krisdmdStarting Member
 
 
                                        9 Posts | 
                                            
                                            |  Posted - 2015-03-10 : 15:04:51 
 |  
                                            | Hello,I'm stuck with building a query.I need to select for each year, the shipment company which has the biggest freight cost and the total amount of shipment costs for each year. Well i'm stuck at grouping per year and how i can get the total costs of all freight costs for that company with the biggest freight cost. SELECT YEAR(o.OrderDate) ShowYear, MAX(o.Freight) ShipmentCosts, o.ShipVia, s.CompanyName FROM Orders oLEFT JOIN Shippers s ON (o.ShipVia=s.ShipperID)GROUP BY o.ShipVia, s.CompanyName, YEAR(o.OrderDate), o.FreightORDER BY ShipmentCosts DESC |  | 
       
                            
                       
                          
                            
                                    | gbrittonMaster Smack Fu Yak Hacker
 
 
                                    2780 Posts | 
                                        
                                          |  Posted - 2015-03-10 : 15:43:33 
 |  
                                          | not sure what you mean by "stuck at grouping per year".Can you provide some sample data for both tables, show what you're getting with your query and what you want to get?I see o.Freight in your group by, that shouldn't be there. |  
                                          |  |  | 
                            
                       
                          
                            
                                    | krisdmdStarting Member
 
 
                                    9 Posts | 
                                        
                                          |  Posted - 2015-03-11 : 15:47:48 
 |  
                                          | When i use this query =>SELECT Distinct YEAR(o.OrderDate) Jaar, MAX(o.Freight) ShipmentCosts, s.CompanyName FROM Orders oINNER JOIN Shippers s ON (o.ShipVia=s.ShipperID)GROUP BY YEAR(o.OrderDate), s.CompanyNameIt's not grouping the year. It shows 3 items for each year. i need unique years. |  
                                          |  |  | 
                            
                       
                          
                            
                                    | gbrittonMaster Smack Fu Yak Hacker
 
 
                                    2780 Posts | 
                                        
                                          |  Posted - 2015-03-11 : 15:59:06 
 |  
                                          | 1. remove Distinct2. It groups by customer name within year, the way it is written3. post some sample output. |  
                                          |  |  | 
                            
                       
                          
                            
                                    | krisdmdStarting Member
 
 
                                    9 Posts | 
                                        
                                          |  Posted - 2015-03-11 : 16:11:10 
 |  
                                          | Sample =>1996	360,63	Federal Shipping1997	1007,64	Federal Shipping1998	606,19	Federal Shipping1996	214,27	Speedy Express1997	458,78	Speedy Express1998	411,88	Speedy Express1996	890,78	United Package1997	810,05	United Package1998	830,75	United Package |  
                                          |  |  | 
                            
                       
                          
                            
                                    | gbrittonMaster Smack Fu Yak Hacker
 
 
                                    2780 Posts | 
                                        
                                          |  Posted - 2015-03-11 : 16:18:16 
 |  
                                          | So, taking 1998 as an example, if you only want to see one row, which customer should it display?  Federal Shipping, Speedy Express or United Package? |  
                                          |  |  | 
                            
                       
                          
                            
                                    | krisdmdStarting Member
 
 
                                    9 Posts | 
                                        
                                          |  Posted - 2015-03-11 : 16:42:43 
 |  
                                          | It should only show the one with the maximum price so for 1998 united package |  
                                          |  |  | 
                            
                       
                          
                            
                                    | Vinnie881Master Smack Fu Yak Hacker
 
 
                                    1231 Posts | 
                                        
                                          |  Posted - 2015-03-11 : 17:58:12 
 |  
                                          | select *(SELECT YEAR(o.OrderDate) as ShowYear, o.Freight as ShipmentCosts, o.ShipVia, s.CompanyName ,row_number() over (Partition by YEAR(o.OrderDate) order by o.freight desc) as rowidFROM Orders oLEFT JOIN Shippers s ON (o.ShipVia=s.ShipperID)) aawhere aa.rowid = 1 Vincent Senese\_/ _/ _/\_/ _/\_/ _/ _/- 881
 |  
                                          |  |  | 
                            
                       
                          
                            
                                    | gbrittonMaster Smack Fu Yak Hacker
 
 
                                    2780 Posts | 
                                        
                                          |  Posted - 2015-03-11 : 18:33:38 
 |  
                                          | simple: SELECT YEAR(o.OrderDate) Jaar, MAX(o.Freight) ShipmentCosts, s.CompanyName FROM Orders oINNER JOIN Shippers s ON (o.ShipVia=s.ShipperID)GROUP BY YEAR(o.OrderDate), s.CompanyNamehaving o.freight = max(o.freight) |  
                                          |  |  | 
                            
                       
                          
                            
                                    | krisdmdStarting Member
 
 
                                    9 Posts | 
                                        
                                          |  Posted - 2015-03-12 : 15:58:07 
 |  
                                          | Having this error gbrittonMsg 8121, Level 16, State 1, Line 64Column 'Orders.Freight' is invalid in the HAVING clause because it is not contained in either an aggregate function or the GROUP BY clause. |  
                                          |  |  | 
                            
                       
                          
                            
                                    | gbrittonMaster Smack Fu Yak Hacker
 
 
                                    2780 Posts | 
                                        
                                          |  Posted - 2015-03-12 : 16:01:12 
 |  
                                          | quote:Since there is no 'Orders.Freight' in the query I sent you, please post the whole query that gives this errorOriginally posted by krisdmd
 Having this error gbrittonMsg 8121, Level 16, State 1, Line 64Column 'Orders.Freight' is invalid in the HAVING clause because it is not contained in either an aggregate function or the GROUP BY clause.
 
 |  
                                          |  |  | 
                            
                       
                          
                            
                                    | krisdmdStarting Member
 
 
                                    9 Posts | 
                                        
                                          |  Posted - 2015-03-12 : 16:04:56 
 |  
                                          | It's a ms sql. The above queries didn't worked. |  
                                          |  |  | 
                            
                       
                          
                            
                                    | krisdmdStarting Member
 
 
                                    9 Posts | 
                                        
                                          |  Posted - 2015-03-12 : 16:28:57 
 |  
                                          | I'm using this query =>SELECT YEAR(o.OrderDate) Jaar, MAX(o.Freight) ShipmentCosts, s.CompanyName FROM Orders oINNER JOIN Shippers s ON (o.ShipVia=s.ShipperID)GROUP BY YEAR(o.OrderDate), s.CompanyNamehaving o.freight = MAX(o.freight) |  
                                          |  |  | 
                            
                       
                          
                            
                                    | gbrittonMaster Smack Fu Yak Hacker
 
 
                                    2780 Posts | 
                                        
                                          |  Posted - 2015-03-12 : 16:31:15 
 |  
                                          | quote:Yes, this is all ms sql, I said to try:Originally posted by krisdmd
 It's a ms sql. The above queries didn't worked.
 
 SELECT YEAR(o.OrderDate) Jaar, MAX(o.Freight) ShipmentCosts, s.CompanyName FROM Orders oINNER JOIN Shippers s ON (o.ShipVia=s.ShipperID)GROUP BY YEAR(o.OrderDate), s.CompanyNamehaving o.freight = max(o.freight)you replied that you got the error: quote:however there is no Orders.Freight column in the code I posted. Therefore you modified my query to include a reference to Orders.Freight in the HAVING clause. That is why I want you to post the exact query you used that generated the error.Msg 8121, Level 16, State 1, Line 64Column 'Orders.Freight' is invalid in the HAVING clause because it is not contained in either an aggregate function or the GROUP BY clause.
 
 |  
                                          |  |  | 
                            
                       
                          
                            
                                    | krisdmdStarting Member
 
 
                                    9 Posts | 
                                        
                                          |  Posted - 2015-03-12 : 16:39:13 
 |  
                                          | i don't know why sql management studio is showing this error. i'm using your query.it changes o.Freight in Orders.Freight in the error message. |  
                                          |  |  | 
                            
                       
                          
                            
                                    | gbrittonMaster Smack Fu Yak Hacker
 
 
                                    2780 Posts | 
                                        
                                          |  Posted - 2015-03-12 : 16:45:22 
 |  
                                          | Sorry, my bad.  That's a SQL anomaly (which I forgot about!).  Have a go with this one: with cte as(	SELECT YEAR(o.OrderDate) Jaar, o.Freight, s.CompanyName 	     , max(o.Freight) over(partition by YEAR(o.OrderDate), o.ShipVia) as ShipmentCosts	FROM Orders o	INNER JOIN Shippers s ON o.ShipVia=s.ShipperID)select Jaar, ShipmentCosts, CompanyNameFrom ctewhere Freight = ShipmentCostsbtw what if there is a tie? |  
                                          |  |  | 
                            
                       
                          
                            
                                    | krisdmdStarting Member
 
 
                                    9 Posts | 
                                        
                                          |  Posted - 2015-03-12 : 16:51:13 
 |  
                                          | Well it's for my school. This is the only query i was stuck. I didn't know it was that difficult to create it.We haven't seen with {} yet in our lessons.Thanks  alot for the help. |  
                                          |  |  | 
                            
                       
                          
                            
                                    | gbrittonMaster Smack Fu Yak Hacker
 
 
                                    2780 Posts | 
                                        
                                          |  Posted - 2015-03-12 : 21:08:16 
 |  
                                          | the with is just a convenience.  You can rewrite it like this:select Jaar, ShipmentCosts, CompanyNamefrom (SELECT YEAR(o.OrderDate) Jaar, o.Freight, s.CompanyName 	     , max(o.Freight) over(partition by YEAR(o.OrderDate), o.ShipVia) as ShipmentCosts	FROM Orders o	INNER JOIN Shippers s ON o.ShipVia=s.ShipperID) qwhere Freight = ShipmentCosts |  
                                          |  |  | 
                            
                       
                          
                            
                                    | dbgajendrayadavStarting Member
 
 
                                    1 Post | 
                                        
                                          |  Posted - 2015-03-18 : 05:22:41 
 |  
                                          | Hi,This can give you the required result....Select YM.OrderDate, YM.Freight, C.CompanyNamefrom dbo.Orders  YMInner Join(Select Year(OrderDate) Odt, MAX(Freight) FreightAmt from dbo.Orders  Ygroup By Year(OrderDate)) XON YEAR(YM.OrderDate) = X.Odt and YM.Freight = X.FreightAmtLeft Join dbo.Shippers  CON YM.ShipVia = C.ShipperID |  
                                          |  |  | 
                            
                            
                                |  |