| Author |
Topic |
|
jonasalbert20
Constraint Violating Yak Guru
300 Posts |
Posted - 2006-01-12 : 03:17:41
|
I had this data....SELECT '1' as theID, '0001' as OrderNo, 'AAA' as Item, '5' as QTYUNION ALLSELECT '2', '0001', 'AAA', '10' as QTYUNION ALLSELECT '3', '0001', 'AAA', '15' as QTYUNION ALLSELECT '4', '0002', 'AAd', '10' as QTYUNION ALLSELECT '5', '0002', 'AAd', '15' as QTYUNION ALLSELECT '5', '0002', 'AAd', '25' as QTYUNION ALLSELECT '6', '0003', 'AAc', '15' as QTYUNION ALLSELECT '7', '0003', 'AAc', '20' as QTYUNION ALLSELECT '8', '0003', 'AAc', '30' as QTY Now I want to get the QTY based on "Max" and "Min" of their "theID"Result should look like this...OrderNo Item qtyMIN qtyMAX--------------------------------------------001 AAA 5 15002 AAd 10 25003 AAc 15 30 Want Philippines to become 1st World COuntry? Go for World War 3... |
|
|
shallu1_gupta
Constraint Violating Yak Guru
394 Posts |
Posted - 2006-01-12 : 03:36:43
|
| Do you want the item name displayed should be of minimum qty item?See if this is what u requireselect a.orderno, a.item, qtymin,qtymaxfrom Tablename ainner join( select orderno, min(qty) qtymin, max(qty) qtymaxfrom Tablenamegroup by orderno)bon a.orderno = b.orderno and a.qty = b.qtymin |
 |
|
|
jonasalbert20
Constraint Violating Yak Guru
300 Posts |
Posted - 2006-01-12 : 03:50:11
|
| tnx shallu1_gupta!but result should be the QTY based on "Max" and "Min" of their "theID"please see my desired result.tnxWant Philippines to become 1st World COuntry? Go for World War 3... |
 |
|
|
jonasalbert20
Constraint Violating Yak Guru
300 Posts |
Posted - 2006-01-12 : 04:12:25
|
my approach is this...select CAST(theID as int) as theID, OrderNo, Item, QTYinto #tempfrom ( SELECT '1' as theID, '0001' as OrderNo, 'AAA' as Item, '5' as QTY UNION ALL SELECT '2', '0001', 'AAA', '10' as QTY UNION ALL SELECT '3', '0001', 'AAA', '15' as QTY UNION ALL SELECT '4', '0002', 'AAd', '10' as QTY UNION ALL SELECT '5', '0002', 'AAd', '15' as QTY UNION ALL SELECT '6', '0002', 'AAd', '25' as QTY UNION ALL SELECT '7', '0003', 'AAc', '15' as QTY UNION ALL SELECT '8', '0003', 'AAc', '20' as QTY UNION ALL SELECT '9', '0003', 'AAc', '30' as QTY ) AS TSELECT t.orderno , qtyMIN = (select qty from #temp where theID = min(t.theID)) , qtyMAX = (select qty from #temp where theID = max(t.theID))FROM #temp AS tGROUP BY t.ordernodrop table #temp Any enhancement?Want Philippines to become 1st World COuntry? Go for World War 3... |
 |
|
|
shallu1_gupta
Constraint Violating Yak Guru
394 Posts |
Posted - 2006-01-12 : 04:35:09
|
| is this not satisfying ur requirement? |
 |
|
|
jonasalbert20
Constraint Violating Yak Guru
300 Posts |
Posted - 2006-01-12 : 04:40:56
|
| sorry to inform shallu1_gupta your approach is doesn't meet my needs.Want Philippines to become 1st World COuntry? Go for World War 3... |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-01-12 : 04:45:18
|
| [code]SELECT t.OrderNo , t.Item , qtyMIN = (select top 1 QTY from #temp x where x.OrderNo = t.OrderNo order by theID, QTY) , qtyMAX = (select top 1 QTY from #temp x where x.OrderNo = t.OrderNo order by theID DESC, QTY DESC)FROM #temp AS tGROUP BY t.OrderNo, t.Item[/code]EDIT in RED-----------------'KH'Only two things are infinite, the universe and human stupidity, and I'm not sure about the former. |
 |
|
|
avishivani
Starting Member
4 Posts |
Posted - 2006-01-12 : 05:00:49
|
| select orderno, item, min(qty) qtyMin, max(qty) qtyMaxfrom (SELECT '1' as theID, '0001' as OrderNo, 'AAA' as Item, '5' as QTYUNION ALLSELECT '2', '0001', 'AAA', '10' as QTYUNION ALLSELECT '3', '0001', 'AAA', '15' as QTYUNION ALLSELECT '4', '0002', 'AAd', '10' as QTYUNION ALLSELECT '5', '0002', 'AAd', '15' as QTYUNION ALLSELECT '5', '0002', 'AAd', '25' as QTYUNION ALLSELECT '6', '0003', 'AAc', '15' as QTYUNION ALLSELECT '7', '0003', 'AAc', '20' as QTYUNION ALLSELECT '8', '0003', 'AAc', '30' as QTY) agroup by orderno, itemorder by 1,2 |
 |
|
|
jonasalbert20
Constraint Violating Yak Guru
300 Posts |
Posted - 2006-01-12 : 05:11:48
|
wrong result avishivani.result should be....OrderNo Item qtyMIN qtyMAX--------------------------------------------001 AAA 5 15002 AAd 10 25003 AAc 15 30 Want Philippines to become 1st World COuntry? Go for World War 3... |
 |
|
|
avishivani
Starting Member
4 Posts |
Posted - 2006-01-12 : 05:26:59
|
| Oops! I missed your something basic in your question.Thanks |
 |
|
|
|
|
|