| Author |
Topic |
|
maya_zakry
Constraint Violating Yak Guru
379 Posts |
Posted - 2007-03-20 : 22:14:36
|
| hi all,if we have 100 same records, how can we select any 50 of it? thanks~~~Focus on problem, not solution~~~ |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-03-20 : 22:35:53
|
yes. Use derived table with topTo get 51st to 100th record out of total recordsselect TOP 50 *from ( select top 100 * from table order by cola ) aorder by cola desc KH |
 |
|
|
maya_zakry
Constraint Violating Yak Guru
379 Posts |
Posted - 2007-03-20 : 22:58:17
|
| ahlamak Khtan,how to apply to my scenario :-plt qty rmkA 1 aA 1 bA 1 chow to select any 2 records with plt=A, Qty=1~~~Focus on problem, not solution~~~ |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-03-20 : 23:11:43
|
Ahlamak. Must be buta . Did even not notice the keyword ANY.declare @table table( plt varchar(10), qty int, rmk varchar(10))insert into @tableselect 'A', 1, 'a' union allselect 'A', 1, 'b' union allselect 'A', 1, 'c' union allselect 'A', 1, 'd' union allselect 'A', 2, 'e'-- ANY 2 records with plt = 'A' and Qty = 1select top 2 *from @tablewhere plt = 'A'and qty = 1order by newid() This will randomly return 2 records KH |
 |
|
|
maya_zakry
Constraint Violating Yak Guru
379 Posts |
Posted - 2007-03-21 : 00:03:45
|
| he he he.. thankss :P~~~Focus on problem, not solution~~~ |
 |
|
|
maya_zakry
Constraint Violating Yak Guru
379 Posts |
Posted - 2007-03-21 : 00:25:57
|
hi again khtan.. :(let me be stret.. im stuck again.. how to apply in left join? in my case below, how to display only MaxItems records of itemstorageid that has d.OriginQty-d.QtyPerPlt-d.HoldQty-d.QcQty+d.AdjQty = FaceValue/.. face value from the main table, and itemstorageid from the left join table.. arghh i dunno how to put into wordss.. dunno if u understand full bahasa melayu or not :(SELECT MaxItems AS TotalPallet, FaceValue AS QtyPerPlt, a.ItemStorageID FROM #Data LEFT JOIN ( SELECT Top MaxItems FROM tblItemDetail d ) a ON FaceValue=d.OriginQty-d.QtyPerPlt-d.HoldQty-d.QcQty+d.AdjQty ~~~Focus on problem, not solution~~~ |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-03-21 : 00:38:52
|
is the LEFT JOIN thing related to the "any records" ? KH |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-03-21 : 00:50:42
|
quote: dunno if u understand full bahasa melayu or not
I do understand Bahasa Melayu. But if you post in Malay, but probably very few people here will able to understand it. And i don't think Peter is one of them. You do want Peter's help don't you ? I know sometimes it is hard to put things down in words. Best way is to provide the table structure (simplified) with sample data and the expected result. And also try to explain what do you want. If possible in English or in both English & Malay. KH |
 |
|
|
maya_zakry
Constraint Violating Yak Guru
379 Posts |
Posted - 2007-03-21 : 01:57:33
|
muahhahahha.. ok ok... yes it's related.. actually this is from peter's code :P.. in post http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=80867.. it's done, but im finding a way to bring back the itemstorageid that had been sum up from his code :(.. he had gave me the idea how to do it.. and like usual, im stuck while waiting for him.. :(.. and dun think ill be online when he does.. dunno what time in sweden now :P.. original data from tblitemdetailitemstorageid QtyA1 3A2 3A3 2A4 3peter code will sum match Qty combination = 6 (this is done)MaxItems Qty2 3 (which means 2 data for Qty=3---> 2x3 match with user's parameter=6)and i need to return back what the itemstorageid (here i stuck)MaxItems Qty ITEMSTORAGEID2 3 A12 3 A2 ~~~Focus on problem, not solution~~~ |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-03-21 : 02:15:33
|
Since it is from Peter's code, the more you should post in English or maybe in Swedish ? Frankly speaking, i don't really understand his code  KH |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-03-21 : 02:20:26
|
Now I want a cut of your paycheck!-- Prepare sample dataDECLARE @Detail TABLE (Storage VARCHAR(2), Qty INT)INSERT @DetailSELECT 'A1', 3 UNION ALLSELECT 'A2', 2 UNION ALLSELECT 'A3', 3 UNION ALLSELECT 'A4', 4 UNION ALLSELECT 'A5', 3 UNION ALLSELECT 'A6', 4 UNION ALLSELECT 'A7', 4DECLARE @Data TABLE (Items INT, QTY INT)INSERT @DataSELECT 2, 3 UNION ALLSELECT 1, 4-- Show the expected outputSELECT det.Storage, det.QtyFROM @Detail AS detLEFT JOIN @Data AS dat ON dat.Qty = det.QtyWHERE dat.Items >= (SELECT COUNT(*) FROM @Detail AS x WHERE x.Qty = det.Qty AND x.Storage <= det.Storage)ORDER BY det.Storage, det.Qty Peter LarssonHelsingborg, Sweden |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-03-21 : 02:23:37
|
quote: Originally posted by khtan Frankly speaking, i don't really understand his code 
Yes you do! You are only too modest when you write that!Write in English. It will not look pretty if I write in Swedish back to you, like this:Hej alla. Nu förstår ni varför man ska skriva på engelska. Det är ingen fler än skandinaver och några finnar som kommer att begripa vad här står, eller hur?Decode that! Peter LarssonHelsingborg, Sweden |
 |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2007-03-21 : 02:39:57
|
quote: Originally posted by maya_zakry hi peter.. there u are!!!khtan-> i dont understand too.. i just paste.. change variables.. and then paste back here when facing problem.. :(~~~Focus on problem, not solution~~~
If that's going to be your approach, you will never learn. Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-03-21 : 02:45:41
|
Damn! You got here first now... Peter LarssonHelsingborg, Sweden |
 |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2007-03-21 : 02:46:54
|
Finally... Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
|
maya_zakry
Constraint Violating Yak Guru
379 Posts |
Posted - 2007-03-21 : 02:50:13
|
| hi peter.. there u are!!!khtan-> i dont understand too.. i just paste.. change variables.. and then paste back here when facing problem.. :( (this is only apply to post http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=80867 <-- too tough for me)huhhuh.. peso, i think im going crazy wif replacing dat, det.. arghh harsh --> this is tough to me.. :(~~~Focus on problem, not solution~~~ |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-03-21 : 02:54:34
|
You mean this?-- Show the expected outputSELECT det.ItemStorageID, det.QtyFROM tblItemDetail AS detLEFT JOIN #Data AS dat ON dat.Qty = det.QtyWHERE dat.MaxItems >= (SELECT COUNT(*) FROM tblItemDetail AS x WHERE x.Qty = det.Qty AND x.ItemStorageID <= det.ItemStorageID)ORDER BY det.ItemStorageID, det.Qty Peter LarssonHelsingborg, Sweden |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-03-21 : 03:17:10
|
quote: Originally posted by Peso
quote: Originally posted by khtan Frankly speaking, i don't really understand his code 
Yes you do! You are only too modest when you write that!Write in English. It will not look pretty if I write in Swedish back to you, like this:Hej alla. Nu förstår ni varför man ska skriva på engelska. Det är ingen fler än skandinaver och några finnar som kommer att begripa vad här står, eller hur?Decode that! Peter LarssonHelsingborg, Sweden
 KH |
 |
|
|
maya_zakry
Constraint Violating Yak Guru
379 Posts |
Posted - 2007-03-21 : 03:27:44
|
quote: Originally posted by Peso You mean this?-- Show the expected outputSELECT det.ItemStorageID, det.QtyFROM tblItemDetail AS detLEFT JOIN #Data AS dat ON dat.Qty = det.QtyWHERE dat.MaxItems >= (SELECT COUNT(*) FROM tblItemDetail AS x WHERE x.Qty = det.Qty AND x.ItemStorageID <= det.ItemStorageID)ORDER BY det.ItemStorageID, det.Qty Peter LarssonHelsingborg, Sweden
what the red font line do?~~~Focus on problem, not solution~~~ |
 |
|
|
maya_zakry
Constraint Violating Yak Guru
379 Posts |
Posted - 2007-03-21 : 03:38:16
|
and this running too slow.. and incorrect data? or i left join wrong ly.. ahh.. wish i could post myself and my pc there..(with a good translator)SELECT maxItems AS TotalPallet, FaceValue AS QtyPerPlt, d.ItemStorageID FROM #Data AS data LEFT JOIN tblItemDetail AS d ON data.maxItems= d.OriginQty-d.PickQty-d.HoldQty-d.BookQty-d.QCQty+d.AdjQty WHERE data.MaxItems >= (SELECT COUNT(*) FROM tblItemDetail AS x WHERE x.OriginQty-x.PickQty-x.HoldQty-x.BookQty-x.QCQty+x.AdjQty=d.OriginQty-d.PickQty-d.HoldQty-d.BookQty-d.QCQty+d.AdjQty AND x.ItemStorageID <= d.ItemStorageID ) ORDER BY d.ItemStorageID, data.MaxItems ~~~Focus on problem, not solution~~~ |
 |
|
|
maya_zakry
Constraint Violating Yak Guru
379 Posts |
Posted - 2007-03-21 : 04:13:52
|
| back to the topic,eg : select top 3 *can we replace 3 with variables/param?~~~Focus on problem, not solution~~~ |
 |
|
|
Next Page
|