![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
So, I have a query SELECT type FROM order, product WHERE order.id = product.id GROUP BY type ORDER BY sum(units) DESC but I only want the first row. MS SQL 2005 doesn't seem to support "LIMIT" or "FIRST" which is unfortunate. I can shove that whole query into another one that checks the MAX, but then I can only get the MAX number of units within a group, when I want to know the type that has the max units in any group. |
#3
| |||
| |||
|
|
So, I have a query SELECT type FROM order, product WHERE order.id = product.id GROUP BY type ORDER BY sum(units) DESC but I only want the first row. MS SQL 2005 doesn't seem to support "LIMIT" or "FIRST" which is unfortunate. I can shove that whole query into another one that checks the MAX, but then I can only get the MAX number of units within a group, when I want to know the type that has the max units in any group. |
#4
| |||
| |||
|
|
Alternatively you can do the following using standard ANSI SQL, which should work on many different platforms: SELECT type FROM ord, product WHERE ord.id = product.id GROUP BY type HAVING SUM(units) >= ALL (SELECT DISTINCT SUM(units) FROM ord, product WHERE ord.id = product.id GROUP BY type); (untested) |
#5
| |||
| |||
|
|
This could return multiple values if there's a tie for most common type. |
#6
| |||
| |||
|
|
On 18 Sep, 08:30, Ed Murphy <emurph... (AT) socal (DOT) rr.com> wrote: This could return multiple values if there's a tie for most common type. Correct. So could the version using TOP WITH TIES. Mark said: "I want to know the type that has the max units in any group". If there is more than one such type then the specification is incomplete because Mark doesn't say which one should come "first". Rather than pick a random row or make the assumption that there is only one row I decided it was safer to return everything - that way Mark can decide for himself whether he needs to refine his spec. -- David Portas |
#7
| |||
| |||
|
|
Thanks guys. You'd think they'd have a "standard" (and simple) method for doing this eh? |
#8
| |||
| |||
|
|
You'd think they'd have a "standard" (and simple) method for doing this eh? |
#9
| |||
| |||
|
|
You'd think they'd have a "standard" (and simple) method for doing this eh? It is a little hard to guess what your tables and columns look like from your narrative. There is even a magical, universal "id" attached to nothing in particular! I guess you meant "product_id" and do not have an industry standard identifier to use, like UPC. I guess type is the product type (category?) code, and that it is in the Products table, not the Orders. But it could be the order type, customer blood type or anything. One Standard SQL answer would be: WITH ProductCategorySales(product_type, sold_tot) AS (SELECT P.product_type, SUM(O.sold_units) FROM Orders AS O, Products AS P WHERE O.product_id = P.product_id GROUP BY product_type) SELECT product_type FROM ProductCategorySales HAVING sold_tot = (SELECT MAX(sold_tot) FROM ProductCategorySales); or get fancy and use some stuff not in SQL Server yet: (SELECT P.product_type, SUM(O.sold_units) OVER (PARTITION BY P.product_type) FROM Orders AS O, Products AS P WHERE O.product_id = P.product_id) The reason that Standard SQL does not have LIMIT or something like it, is that SQL is a set-oriented database language, not a sequential file language. Such things would have to be part of a cursor's ORDER BY clause to fit into the language model. |
![]() |
| Thread Tools | |
| Display Modes | |
| |