Join 3 tables to update Prices based on last Ship Dates

I'm having a hard time wrapping my head around this one. I need to find out which products have not shipped in the last 2 years, 1 year and 6 months (three queries) and then update the prices based on a percentage.

The tables I am using are:

BX
BXCNT
STOCK


BX tabele has SHIPDATE and ORDERNO

BXCNT table has ORDERNO and STOCKNUMBER

STOCK table has STOCKNUMBER and PRICE

The BXCNT table is like an order detail that contains all the stock numbers inside the order. That is the way I tie the shipdate to the number

The date format looks like this: 1999-11-18 00:00:00.000

So I want to select STOCKNUMBER and update the PRICE (price * .75) if SHIPDATE > 2 years

Hope that makes sense. Thank you for any help and information.

-S
summersetAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
The correct ways for handling DATETIME values in MySQL and PHP are given in this article.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html

Your date format is good.  Do you have PHP available to you?
0
summersetAuthor Commented:
Hi, Thank you for your quick reply. No, I am on a Microsoft server (remote) using Microsoft SQL Server Management Studio. Version SQL Server 2008 R2
0
PortletPaulfreelancerCommented:
Please check this carefully! It is based only on the minimal information provided and not rigorously tested.
UPDATE
      STOCK
SET
     price = price * factor
FROM (
            SELECT
                  STOCKNUMBER
                , CASE
                        WHEN last_ship_months >= 24 THEN 2.1
                        WHEN last_ship_months BETWEEN 12 AND 23 THEN 1.75
                        WHEN last_ship_months BETWEEN 6 AND 11 THEN 1.5
                        ELSE 1.0 END
                  AS Factor
            FROM (
                        SELECT
                              STOCKNUMBER
                            , MIN(DATEDIFF(MONTH, bx.shipdate, GETDATE())) AS last_ship_months
                        FROM BXCNT
                              INNER JOIN BX
                                          ON BXCNT.ORDERNO = BX.ORDERNO
                        GROUP BY
                              STOCKNUMBER
                  ) sq1
            WHERE last_ship_months >= 6
      ) sq2
WHERE stock.STOCKNUMBER = sq2.STOCKNUMBER
;

Open in new window

In truth it would have been far better if you had provided sample data and expected results. I had to invent some of my own invention which you may now need to understand:
**MS SQL Server 2008 Schema Setup**:

    CREATE TABLE STOCK
    	([STOCKNUMBER] varchar(3), [PRICE] money, [old_price] money)
    ;
    	
    INSERT INTO STOCK
    	([STOCKNUMBER], [PRICE])
    VALUES
    	('abc', 1.10),
    	('def', 2.10),
    	('ghi', 3.10)
    ;
    
    
    CREATE TABLE BXCNT
    	([ORDERNO] int, [STOCKNUMBER] varchar(3))
    ;
    	
    INSERT INTO BXCNT
    	([ORDERNO], [STOCKNUMBER])
    VALUES
    	(100, 'abc'),
    	(200, 'def'),
    	(300, 'ghi')
    ;
    
    CREATE TABLE BX
    	([SHIPDATE] datetime, [ORDERNO] int)
    ;
    	
    INSERT INTO BX
    	([SHIPDATE], [ORDERNO])
    VALUES
    	('2014-05-01 00:00:00', 300),
    	('2013-09-01 00:00:00', 200),
    	('2012-07-01 00:00:00', 100)
    ;

**Query 1**:

    UPDATE
          STOCK
    SET
          old_price = price
        , price = price * factor
    FROM (
                SELECT
                      STOCKNUMBER
                    , CASE
                            WHEN last_ship_months >= 24 THEN 2.1
                            WHEN last_ship_months BETWEEN 12 AND 23 THEN 1.75
                            WHEN last_ship_months BETWEEN 6 AND 11 THEN 1.5
                            ELSE 1.0 END
                      AS Factor
                FROM (
                            SELECT
                                  STOCKNUMBER
                                , MIN(DATEDIFF(MONTH, bx.shipdate, GETDATE())) AS last_ship_months
                            FROM BXCNT
                                  INNER JOIN BX
                                              ON BXCNT.ORDERNO = BX.ORDERNO
                            GROUP BY
                                  STOCKNUMBER
                      ) sq1
                WHERE last_ship_months >= 6
          ) sq2
    WHERE stock.STOCKNUMBER = sq2.STOCKNUMBER
    

**[Results][2]**:
    

**Query 2**:

    select
    *
    from stock
    
    

**[Results][3]**:
    
    | STOCKNUMBER | PRICE | OLD_PRICE |
    |-------------|-------|-----------|
    |         abc |  2.31 |       1.1 |
    |         def |  3.15 |       2.1 |
    |         ghi |   3.1 |    (null) |



  [1]: http://sqlfiddle.com/#!3/41a37/1

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
summersetAuthor Commented:
Thanks for the quick reply. Your example is great learning information.
0
PortletPaulfreelancerCommented:
No problem.

Do consider providing sample data and expected results with questions. The process of creating it really helps focus you on the issues and the combination of data  results will do a lot of the explanation for you too.
sqlfiddle.com is very useful for this also
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.