SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


SQL 2008 Query


SQL 2008 Query

Author
Message
CoryJ72
CoryJ72
Grasshopper
Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)

Group: General Forum Members
Points: 14 Visits: 44
Hi

I am a complete novice and would like some help. I have a table (see attached image) that i need to query and return the average cost for the latest 3 supply dates. Some items may have one or 100 records. Let me know if you need any further information. Any help would be great.

Cheers
Cory
Attachments
Capture.JPG (15 views, 57.00 KB)
Grant Fritchey
Grant Fritchey
SSC-Dedicated
SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)

Group: General Forum Members
Points: 39131 Visits: 32616
Any time you need to write a query, I usually take the approach of breaking it down into steps. For example, you need to see the average cost for each item, start there:

SELECT AVG(x.Cost)
FROM dbo.MyTable AS x
GROUP BY x.ItemID;

That will give you the averages. Then, you've specified a filter, the last three days. So you need to add a WHERE clause to limit the data that is being grouped. Further, you need to do a little date math to subtract three days from the current date. Since this sure sounds like homework, rather than just giving you the final answer, I'll link to the information you need.

----------------------------------------------------
The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood...
Theodore Roosevelt

The Scary DBA
Author of: SQL Server Query Performance Tuning and SQL Server Execution Plans
Product Evangelist for Red Gate Software
CoryJ72
CoryJ72
Grasshopper
Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)

Group: General Forum Members
Points: 14 Visits: 44
Thanks Very much for the help Grant you have defiantly pointed me in the right direction. I was almost there but it was one were i was heading down the wrong path. As you can see below I was looking for the top 3 dates not not 3 days from [today]. But I didn't explain it to well :-) Again Thanks for the information



SELECT x.ItemID, AVG(Cost)as Cost
FROM dbo.Inventory_tb AS x
where RecordID in
(
select TOP 3 RecordID
from Inventory_tb as b
where b.ItemID = x.ItemID
)
GROUP BY x.ItemID;


Grant Fritchey
Grant Fritchey
SSC-Dedicated
SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)

Group: General Forum Members
Points: 39131 Visits: 32616
I would just add an ORDER BY clause to that TOP query. You can't guarantee the order of the data without it.

----------------------------------------------------
The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood...
Theodore Roosevelt

The Scary DBA
Author of: SQL Server Query Performance Tuning and SQL Server Execution Plans
Product Evangelist for Red Gate Software
CoryJ72
CoryJ72
Grasshopper
Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)

Group: General Forum Members
Points: 14 Visits: 44
Just did found a few odd results. Again thanks.
kritika
kritika
SSC Rookie
SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)

Group: General Forum Members
Points: 31 Visits: 335
something like this:-


SELECT SUM(UnitPrice)
FROM
(SELECT TOP 3 UnitPrice FROM Sales.SalesOrderDetail
ORDER BY ModifiedDate DESC )t
elfishae15
elfishae15
Forum Newbie
Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: General Forum Members
Points: 2 Visits: 2
how to insert sql script in flash which will be displayed on the web?? what it could be??



Gejala Kanker, Cara Membuat Kue, Cerita Anak
Grant Fritchey
Grant Fritchey
SSC-Dedicated
SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)

Group: General Forum Members
Points: 39131 Visits: 32616
elfishae15 (4/26/2013)
how to insert sql script in flash which will be displayed on the web?? what it could be??



Gejala Kanker, Cara Membuat Kue, Cerita Anak


To get a good set of people to see your question and get an answer, post it as a new question. The only people who will see this are the people already subscribed to this question or people who stumble by.

----------------------------------------------------
The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood...
Theodore Roosevelt

The Scary DBA
Author of: SQL Server Query Performance Tuning and SQL Server Execution Plans
Product Evangelist for Red Gate Software
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search