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 |
djorre
Yak Posting Veteran
94 Posts |
Posted - 2011-01-27 : 11:19:14
|
Hello,I have a table like this:tbl_HistoryTankProductsID Tank DateTime Product Amount1 Tank1 13:00 1 52 Tank1 13:00 2 53 Tank1 14:00 1 64 Tank1 14:00 2 65 Tank2 16:00 1 46 Tank2 16:00 2 47 Tank2 17:00 1 3I need=>vw_CurrentTankProductsTank Product AmountTank1 1 6Tank1 2 6Tank2 1 3Please help? The history table is hugh so I would also like it to be fast. It is hard for me not to write this in a stored procedure but a view. |
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2011-01-27 : 11:27:56
|
select tank, product, amount = max(amont)group by tank, productorder by tank, product==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
djorre
Yak Posting Veteran
94 Posts |
Posted - 2011-01-27 : 11:29:34
|
Sorry if that was not clear: the amount can also drop in time, so a max(amount) will not do it. The products themselves can also change in time.I need the product ids and amounts at the max(datetime)s of all tanks. |
 |
|
Ifor
Aged Yak Warrior
700 Posts |
Posted - 2011-01-27 : 12:00:08
|
[code];WITH HTPOrderAS( SELECT ID, Tank, [DateTime], Product, Amount ,ROW_NUMBER() OVER (PARTITION BY ID, Tank ORDER BY [DateTime] DESC) AS RowNum FROM tbl_HistoryTankProducts)SELECT Tank, Product, AmountFROM HTPOrderWHERE RowNum = 1[/code] |
 |
|
djorre
Yak Posting Veteran
94 Posts |
Posted - 2011-01-28 : 03:34:28
|
quote: SELECT ID, Tank, [DateTime], Product, Amount ,ROW_NUMBER() OVER (PARTITION BY ID, Tank ORDER BY [DateTime] DESC) AS RowNum FROM tbl_HistoryTankProducts
All RowNumbers are 1 with this query.If I use thisSELECT ID_EquipProductHist, Equip_ID, TimeStamp_PLC, Product_ID, Product_Perc, ROW_NUMBER() OVER (partition by Equip_Id, TimeStamp_PLC ORDER BY TimeStamp_PLC) AS RowNumFROM tbl_EquipProductHistwhere equip_id = 337order by Equip_Id I get this5351 337 2010-06-24 13:58:22.503 9672 7.68287944793701 15352 337 2010-06-24 13:58:22.503 9728 13.1422958374023 25353 337 2010-06-24 13:58:22.503 9733 6.17041206359863 35354 337 2010-06-24 13:58:22.503 9734 9.09217643737793 45355 337 2010-06-24 13:58:22.503 9740 63.9122352600098 56469 337 2010-07-23 15:15:33.000 9672 7.68287944793701 16470 337 2010-07-23 15:15:33.000 9728 13.1422958374023 26471 337 2010-07-23 15:15:33.000 9733 6.17041206359863 36472 337 2010-07-23 15:15:33.000 9734 9.09217643737793 46473 337 2010-07-23 15:15:33.000 9740 63.9122352600098 5I guess I need it to be this:5351 337 2010-06-24 13:58:22.503 9672 7.68287944793701 15352 337 2010-06-24 13:58:22.503 9728 13.1422958374023 15353 337 2010-06-24 13:58:22.503 9733 6.17041206359863 15354 337 2010-06-24 13:58:22.503 9734 9.09217643737793 15355 337 2010-06-24 13:58:22.503 9740 63.9122352600098 16469 337 2010-07-23 15:15:33.000 9672 7.68287944793701 26470 337 2010-07-23 15:15:33.000 9728 13.1422958374023 26471 337 2010-07-23 15:15:33.000 9733 6.17041206359863 26472 337 2010-07-23 15:15:33.000 9734 9.09217643737793 26473 337 2010-07-23 15:15:33.000 9740 63.9122352600098 2Where RowNum = 1 |
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2011-01-28 : 04:29:39
|
quote: Originally posted by djorreAll RowNumbers are 1 with this query.If I use thisSELECT ID_EquipProductHist, Equip_ID, TimeStamp_PLC, Product_ID, Product_Perc, ROW_NUMBER() OVER (partition by Equip_Id, TimeStamp_PLC ORDER BY TimeStamp_PLC) AS RowNumFROM tbl_EquipProductHistwhere equip_id = 337order by Equip_Id
KH[spoiler]Time is always against us[/spoiler] |
 |
|
djorre
Yak Posting Veteran
94 Posts |
Posted - 2011-01-28 : 04:34:46
|
That gave me all ascending numbers 1 2 3 4 5 6 ....But I found the solution, need to use Dense_Rank in stead of row_numberSELECT ID_EquipProductHist, Equip_ID, TimeStamp_PLC, Product_ID, Product_Perc, Dense_RANK() OVER (partition by Equip_Id ORDER BY TimeStamp_PLC desc) AS DenseNumFROM tbl_EquipProductHist 9533 337 2010-08-26 14:25:05.000 9672 7.68287944793701 19534 337 2010-08-26 14:25:05.000 9728 13.1422958374023 19535 337 2010-08-26 14:25:05.000 9733 6.17041206359863 19536 337 2010-08-26 14:25:05.000 9734 9.09217643737793 19537 337 2010-08-26 14:25:05.000 9740 63.9122352600098 18891 337 2010-08-17 17:13:34.500 9672 7.68287944793701 28892 337 2010-08-17 17:13:34.500 9728 13.1422958374023 8893 337 2010-08-17 17:13:34.500 9733 6.17041206359863 28894 337 2010-08-17 17:13:34.500 9734 9.09217643737793 28895 337 2010-08-17 17:13:34.500 9740 63.9122352600098 2 Thanks to all! |
 |
|
|
|
|
|
|