Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
rythm123us
Starting Member
27 Posts |
Posted - 2005-09-11 : 22:43:16
|
| Item QuantityI1 10I2 7I1 5Assume that is the Data. I want it ordered by Quantity first and then show the same Item againif it exists first.How can I display it in this wayItem QuantityI1 10I1 5I2 7 |
|
|
derrickleggett
Pointy Haired Yak DBA
4184 Posts |
Posted - 2005-09-11 : 23:04:37
|
| [code]-- Item Quantity-- I1 10-- I2 7-- I1 5-- -- Assume that is the Data.-- I want it ordered by Quantity first and then show the same Item again-- if it exists first.-- -- How can I display it in this way-- Item Quantity-- I1 10-- I1 5-- I2 7DECLARE @quantity TABLE(item CHAR(2), quantity INT)INSERT @quantity(item, quantity) SELECT 'I1', 10 UNION ALL SELECT 'I2', 7 UNION ALL SELECT 'I1', 5SELECT item, quantityFROM @quantityORDER BY item, quantity DESC[/code]MeanOldDBAderrickleggett@hotmail.comWhen life gives you a lemon, fire the DBA. |
 |
|
|
rythm123us
Starting Member
27 Posts |
Posted - 2005-09-11 : 23:18:38
|
| That would not work. As I said in my original Post, I want it ordered by Quantity first.The query you specified orders by Item first and then Quanity. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-09-12 : 00:53:57
|
| Did you try that query?If you want the records to be displayed Descending order of quantities for each item, then the query suggested by derrick will workOtherwise you need to give more sample data and expected resultMadhivananFailing to plan is Planning to fail |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2005-09-12 : 01:31:04
|
| [code]DECLARE @quantity TABLE(item CHAR(2), quantity INT)INSERT @quantity(item, quantity) SELECT 'I1', 10 UNION ALL SELECT 'I2', 7 UNION ALL SELECT 'I1', 5SELECT Q.item, quantityFROM @quantity Q JOIN ( SELECT M.item, [MAX_QTY] = MAX(quantity) FROM @quantity M GROUP BY M.item ) M ON M.item = Q.itemORDER BY M.MAX_QTY DESC, Q.item, quantity DESC[/code]Kristen |
 |
|
|
derrickleggett
Pointy Haired Yak DBA
4184 Posts |
Posted - 2005-09-12 : 01:46:05
|
| I think what you're saying you want and what you are showing us you want are two different things. Why don't you think about it and post a better example. Include a bigger sample of data, so we can decipher what you're trying to say.MeanOldDBAderrickleggett@hotmail.comWhen life gives you a lemon, fire the DBA. |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2005-09-12 : 07:42:52
|
Seemed clear to me until I discover I've answered the wrong question!, although the sample data did not express the edge-conditions at all Kristen |
 |
|
|
rythm123us
Starting Member
27 Posts |
Posted - 2005-09-12 : 10:05:19
|
| The answer provided by Kristen works.I am sorry for not providing proper Data.Here is what the entire thing looks like.How about this:OrderID Item Quantity1 I1 101 I2 71 I1 52 I3 152 I1 102 I3 4The result I am hoping to generate:OrderID Item Quantity1 I1 101 I1 51 I2 72 I3 152 I3 42 I1 10So the order should be OrderID, Quantity DESC, and the related Item.an ORDER BY clause : "ORDER BY OrderID, Quantity DESC, Item"would give me the result below. Which not what I want.OrderID Item Quantity1 I1 101 I2 71 I1 52 I3 152 I1 102 I3 4 DECLARE @quantity TABLE(OrderID INT, Item CHAR(2), Quantity INT)INSERT INTO @quantity(OrderID, Item, Quantity)VALUES(1, 'I1', 10)INSERT @quantity(OrderID, Item, Quantity)VALUES(1, 'I2', 7)INSERT @quantity(OrderID, Item, Quantity)VALUES(1, 'I1', 5)INSERT @quantity(OrderID, Item, Quantity)VALUES(2, 'I3', 15)INSERT @quantity(OrderID, Item, Quantity)VALUES(2, 'I1', 10)INSERT @quantity(OrderID, Item, Quantity)VALUES(2, 'I3', 4)SELECT OrderID, Q.item, quantityFROM @quantity Q JOIN ( SELECT M.item, [MAX_QTY] = MAX(quantity) FROM @quantity M GROUP BY M.item ) M ON M.item = Q.itemORDER BY OrderID, M.MAX_QTY DESC, Q.item, quantity DESC |
 |
|
|
|
|
|
|
|