Item Price changes

  • I have a table where we store price information for each item and prices can be changed anytime during the month. How can I store and retrieve correct information based on price updates. Please check the below table structure and my query.

    Item Price

    Code        Price Description     DateFrom                 DateTo
    110  item 110/10/2015 10/5/2016
    25  item 210/6/201610/7/2016
    37  item 33/7/20175/7/2017
    113  item 110/6/201610/7/2017
    53  item 510/7/201610/10/2017
    115  item 11/1/201612/31/2016

    Sales

               Icode    Ccode        Qty     Price      Date
    1111101010/10/2015
    111151310/7/2016
    111131512/31/2016

    I require the following result based on price updates.

    description                 qty          total             date
    Item110100           13/10/2015
    Item156510/7/2016
    Item 134512/31/2016

    What would be the query to get this result.

  • Could you please post DDL and include your sample data.

    ...

  • Welcome to SSC Waqar

    You've already got what you need in the Sales table, multiple Qty and Price, and join back to ItemPrice table to get the item description.

    If you want better help, please post DDL, sample data in readily consumable format, along with your query which you have tried and people will be more willing to help, please see the first link in my signature on posting readily consumable scripts for people.

  • CREATE TABLE [dbo].[Sales](
        [Icode] [nvarchar](255) ,
        [Ccode] [nvarchar](255) ,
        [Qty] [int] ,
        [Price] [float],
        [Date] [datetime]

    CREATE TABLE [dbo].[Item](
        [Code] [varchar],
        [Price] [float],
        [Description] [varchar],
        [DateFrom] [datetime],
        [DateTo] [datetime] 

    What I have in my sales table is not the solution, it is the table that has information for all the clients who purchased items.However, following is my query and it's result.

    select a.description,sum(b.qty),sum(b.qty)*a.price as total,date
    from item a
    inner join sales b on a.code=b.icode
    where date between '2015-03-03' and '2017-08-10'
    group by description,a.price,date

    description                 qty      total        date
    Item11010010/10/2015
    Item15507/10/2016
    Item 11013010/10/2015
    Item 15657/10/2016
    Item 11015010/10/2015
    Item 15757/10/2016

    which is repetitive and wrong data.

  • You've provided DDL, which is a good start, but you still need to supply that sample data in a consumable format.

    Thom~

    Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.
    Larnu.uk

  • How can I do that as I tried it.

  • Waqar571 - Monday, December 4, 2017 6:32 AM

    How can I do that as I tried it.

    Myself and Anthony both have links in our signatures explaining how to do so.

    Thom~

    Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.
    Larnu.uk

  • CREATE TABLE [dbo].[Sales](
    [Icode] [varchar](255) ,
    [Ccode] [varchar](255) ,
    [Qty] [int] ,
    [Price] [float],
    [Date] [datetime]

    Insert into [dbo].[sales] (Icode,ccode,qty,price,date)
    values
    ('1','111',10,10,'10/10/2015')
    ('1','111',5,13,'10/07/2016')
    ('1','111',3,15,'12/31/2016')

    CREATE TABLE [dbo].[Item](
    [Code] [varchar](10),
    [Price] [float],
    [Description] [varchar](50),
    [DateFrom] [datetime],
    [DateTo] [datetime] 

    Insert into [dbo].[sales] (code,price,description,datefrom,dateto)
    values 
    ('1','10','item1','10/10/2015','10/5/2016')
    ('2','5','item2','10/6/2016','10/7/2016')
    ('3','7','item3','3/7/2017','5/7/2017')
    ('1','13','item1','10/6/2016','10/7/2017')
    ('5','3','item5','10/7/2016','10/10/2017')
    ('1','15','item1','1/1/2016','12/31/2016')

    select a.description,sum(b.qty),sum(b.qty)*a.price as total,date
    from item a 
    inner join sales b on a.code=b.icode
    where date between '2015-03-03' and '2017-08-10'
    group by description,a.price,date

  • You need to move the date up into the join clause

    select a.description,sum(b.qty),sum(b.qty)*a.price as total,date
    from item a
    inner join sales b on a.code=b.icode and b.date between a.datefrom and a.dateto
    group by description,a.price,date                                  

    The problem you have is that you have prices for Item 1 that overlap.  Is that true?  Should an item have multiple prices that overlap?

    Also please test your code, it doesn't compile, your missing ending brackets on your create table statements and comma's at the end of your insert value lines, also if using MDY date formatting specify that SET DATEFORMAT MDY, otherwise use ISO date formatting to ensure you don't get any conversion issues.


    SELECT i.description, s.qty, s.qty*s.price as total, s.date
    FROM sales s
    INNER JOIN (SELECT DISTINCT i.code, i.description FROM item i ) i
    ON s.icode = i.code

  • anthony.green - Tuesday, December 5, 2017 1:20 AM

    You need to move the date up into the join clause

    select a.description,sum(b.qty),sum(b.qty)*a.price as total,date
    from item a
    inner join sales b on a.code=b.icode and b.date between a.datefrom and a.dateto
    group by description,a.price,date                                  

    The problem you have is that you have prices for Item 1 that overlap.  Is that true?  Should an item have multiple prices that overlap?

    Also please test your code, it doesn't compile, your missing ending brackets on your create table statements and comma's at the end of your insert value lines, also if using MDY date formatting specify that SET DATEFORMAT MDY, otherwise use ISO date formatting to ensure you don't get any conversion issues.


    SELECT i.description, s.qty, s.qty*s.price as total, s.date
    FROM sales s
    INNER JOIN (SELECT DISTINCT i.code, i.description FROM item i ) i
    ON s.icode = i.code

    Agreed on all of the above points regarding testing etc. and the overlap of dates, having said that I have been in Supermarkets where an item has two prices!! Great when this happens to be beer!

    An alternative to Anthony's could be to use the WHERE clause as in:


    select I.description,sum(S.qty),sum(S.qty)*I.price as total,S.date
    from item I
    inner join sales S on I.code=S.icode
    where S.[date] >= I.DateFrom
    AND S.[Date] <= I.DateTo
    group by description,I.price,S.date

    ...

  • Thanks, I will check and reply you.

  • HappyGeek - Tuesday, December 5, 2017 2:18 AM

    An alternative to Anthony's could be to use the WHERE clause as in:


    select I.description,sum(S.qty),sum(S.qty)*I.price as total,S.date
    from item I
    inner join sales S on I.code=S.icode
    where S.[date] >= I.DateFrom
    AND S.[Date] <= I.DateTo
    group by description,I.price,S.date

    Since it's an Inner Join, there's no difference in performance of functionality between predicates in the JOIN or in the WHERE. I'd personally put it in the join, as it's part of the condition for matching the tables, but WHERE is perfectly fine too.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass

Viewing 12 posts - 1 through 11 (of 11 total)

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