SQL 2008 Query

  • 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

  • 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

    Author of:
    SQL Server Execution Plans
    SQL Server Query Performance Tuning

  • 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;

  • 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

    Author of:
    SQL Server Execution Plans
    SQL Server Query Performance Tuning

  • Just did found a few odd results. Again thanks.

  • something like this:-

    SELECT SUM(UnitPrice)

    FROM

    (SELECT TOP 3 UnitPrice FROM Sales.SalesOrderDetail

    ORDER BY ModifiedDate DESC )t

  • how to insert sql script in flash which will be displayed on the web?? what it could be??

    Gejala Kanker[/url], Cara Membuat Kue[/url], Cerita Anak[/url]

  • elfishae15 (4/26/2013)


    how to insert sql script in flash which will be displayed on the web?? what it could be??

    Gejala Kanker[/url], Cara Membuat Kue[/url], Cerita Anak[/url]

    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

    Author of:
    SQL Server Execution Plans
    SQL Server Query Performance Tuning

Viewing 8 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic. Login to reply