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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 find x record

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 top

To get 51st to 100th record out of total records

select TOP 50 *
from (
select top 100 *
from table
order by cola
) a
order by cola desc



KH

Go to Top of Page

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 rmk
A 1 a
A 1 b
A 1 c

how to select any 2 records with plt=A, Qty=1

~~~Focus on problem, not solution~~~
Go to Top of Page

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 @table
select 'A', 1, 'a' union all
select 'A', 1, 'b' union all
select 'A', 1, 'c' union all
select 'A', 1, 'd' union all
select 'A', 2, 'e'

-- ANY 2 records with plt = 'A' and Qty = 1
select top 2 *
from @table
where plt = 'A'
and qty = 1
order by newid()


This will randomly return 2 records


KH

Go to Top of Page

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~~~
Go to Top of Page

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~~~
Go to Top of Page

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

Go to Top of Page

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

Go to Top of Page

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 tblitemdetail
itemstorageid Qty
A1 3
A2 3
A3 2
A4 3

peter code will sum match Qty combination = 6 (this is done)
MaxItems Qty
2 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 ITEMSTORAGEID
2 3 A1
2 3 A2


~~~Focus on problem, not solution~~~
Go to Top of Page

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

Go to Top of Page

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 data
DECLARE @Detail TABLE (Storage VARCHAR(2), Qty INT)

INSERT @Detail
SELECT 'A1', 3 UNION ALL
SELECT 'A2', 2 UNION ALL
SELECT 'A3', 3 UNION ALL
SELECT 'A4', 4 UNION ALL
SELECT 'A5', 3 UNION ALL
SELECT 'A6', 4 UNION ALL
SELECT 'A7', 4

DECLARE @Data TABLE (Items INT, QTY INT)

INSERT @Data
SELECT 2, 3 UNION ALL
SELECT 1, 4

-- Show the expected output
SELECT det.Storage,
det.Qty
FROM @Detail AS det
LEFT JOIN @Data AS dat ON dat.Qty = det.Qty
WHERE 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 Larsson
Helsingborg, Sweden
Go to Top of Page

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 Larsson
Helsingborg, Sweden
Go to Top of Page

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 Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-03-21 : 02:45:41
Damn! You got here first now...




Peter Larsson
Helsingborg, Sweden
Go to Top of Page

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2007-03-21 : 02:46:54
Finally...

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

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~~~
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-03-21 : 02:54:34
You mean this?
-- Show the expected output
SELECT det.ItemStorageID,
det.Qty
FROM tblItemDetail AS det
LEFT JOIN #Data AS dat ON dat.Qty = det.Qty
WHERE 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 Larsson
Helsingborg, Sweden
Go to Top of Page

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 Larsson
Helsingborg, Sweden





KH

Go to Top of Page

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 output
SELECT det.ItemStorageID,
det.Qty
FROM tblItemDetail AS det
LEFT JOIN #Data AS dat ON dat.Qty = det.Qty
WHERE 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 Larsson
Helsingborg, Sweden



what the red font line do?

~~~Focus on problem, not solution~~~
Go to Top of Page

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~~~
Go to Top of Page

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~~~
Go to Top of Page
    Next Page

- Advertisement -