Groups | Blog | Home
all groups > sql server programming > february 2004 >

sql server programming : Please help me with this query...thanks



Otis B. Driftwood
2/22/2004 4:39:56 PM
[quoted text, click to view]

If you have pivoting with sql server on your mind
perhaps you would like to check out the RAC utility for S2k.
Be aware that RAC cuts to the chase:

No more Cursors,
No more Case,
No more sql Code to put in your face:).

RAC and QALite @
www.rac4sql.net


Kim
2/22/2004 8:55:45 PM
How can I do this in one query

I've got a table like this

ID RESOURCEID DATE QUANTITY

The Id is unique (but just a counter not of interest here), there may be one
resourceid for each date and then an asscoiated quantity.
I want to select all resourceid (distinct) and I just want just the
resourceid and the date and quantity associated with that post that is less
than today but nearast in time.

To examplify

if some records look like like this

ID RESOURCEID DATE QUANTITY
1 1 2003-12-01 1.3
2 1 2004-01-25 1.4
3 2 2002-01-01 1.7
4 2 2003-07-27 2.8

I want my select statement to return only the two following post

ID RESOURCEID DATE QUANTITY
2 1 2004-01-25 1.4
4 2 2003-07-27 2.8

That is one post for each resource and the post should be the one were the
date is less than today

I want to do this without using stored procedures or view, since I'm not
allowed to create this kind if objects in the database.
I thought that perhaps using a #temp table would be a solution but I just
can't get it to work as I want

Thanks in advance


Dandy WEYN
2/22/2004 9:10:30 PM
this will probably get you on the way


--
Dandy Weyn, Belgium
MCSE, MCSA, MCDBA, MCT

http://www.dandyman.net

Check my SQL Server resource pages (currently under construction)
http://www.dandyman.net/sql


[quoted text, click to view]

Dandy WEYN
2/22/2004 9:10:38 PM
You need to pivot the table

http://www.dandyman.net/sql/samplesByTopic.aspx?topicid=2&id=6

--
Dandy Weyn, Belgium
MCSE, MCSA, MCDBA, MCT

http://www.dandyman.net

Check my SQL Server resource pages (currently under construction)
http://www.dandyman.net/sql


[quoted text, click to view]

Kim
2/22/2004 9:53:42 PM
Thanks a lot for your answer and an elegant way to solve my issue.

An additional question though, should I conclude from the answer that I'm
"bound" to use SPs and cursors to get the job done.
Isn't there a way to do some kind of self join operation...
I'm asking since I'm not sure I'll be allowed to set up SPs or Views in the
destination database.

(I'll be using a vb.net app and ado (not ado.net) to execute the t-sql)

Thanks again

/Kim



"Dandy WEYN" <no_spam_info@dandyman.net> skrev i meddelandet
news:40390d9d$0$7043$ba620e4c@news.skynet.be...
[quoted text, click to view]

Dandy WEYN
2/22/2004 10:09:13 PM
I'm so sorry i didn't take a close look at your request
In fact the answer I provided you was totally wrong.


try this



select id2.id,id1.resourceid, id2.date, id2.quantity from
tbl_resultset id1 join tbl_resultset id2 on id1.resourceid = id2.resourceid
and id2.id > id1.id
--
Dandy Weyn, Belgium
MCSE, MCSA, MCDBA, MCT

http://www.dandyman.net

Check my SQL Server resource pages (currently under construction)
http://www.dandyman.net/sql


[quoted text, click to view]

Mark
2/23/2004 9:29:16 AM
select * from TABLE a where a.DATE = ( select max(b.DATE) from TABLE b where
a.ID = b.ID )

[quoted text, click to view]

AddThis Social Bookmark Button