Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

SQL 2008 Query Expand / Collapse
Author
Message
Posted Thursday, April 25, 2013 12:40 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, May 27, 2013 6:37 AM
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



  Post Attachments 
Capture.JPG (13 views, 57.65 KB)
Post #1446312
Posted Thursday, April 25, 2013 4:44 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 9:47 AM
Points: 13,872, Visits: 28,270
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
SQL Server 2012 Query Performance Tuning
SQL Server 2008 Query Performance Tuning Distilled
and
SQL Server Execution Plans

Product Evangelist for Red Gate Software
Post #1446404
Posted Thursday, April 25, 2013 5:24 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, May 27, 2013 6:37 AM
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;

Post #1446423
Posted Thursday, April 25, 2013 5:53 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 9:47 AM
Points: 13,872, Visits: 28,270
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
SQL Server 2012 Query Performance Tuning
SQL Server 2008 Query Performance Tuning Distilled
and
SQL Server Execution Plans

Product Evangelist for Red Gate Software
Post #1446437
Posted Thursday, April 25, 2013 6:43 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, May 27, 2013 6:37 AM
Points: 14, Visits: 44
Just did found a few odd results. Again thanks.
Post #1446463
Posted Thursday, April 25, 2013 7:56 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, July 11, 2013 10:36 PM
Points: 31, Visits: 335
something like this:-


SELECT SUM(UnitPrice)
FROM
(SELECT TOP 3 UnitPrice FROM Sales.SalesOrderDetail
ORDER BY ModifiedDate DESC )t
Post #1446507
Posted Friday, April 26, 2013 12:29 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, April 26, 2013 3:46 AM
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
Post #1446808
Posted Friday, April 26, 2013 3:44 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 9:47 AM
Points: 13,872, Visits: 28,270
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
SQL Server 2012 Query Performance Tuning
SQL Server 2008 Query Performance Tuning Distilled
and
SQL Server Execution Plans

Product Evangelist for Red Gate Software
Post #1446878
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse