[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 145
  • Last Modified:

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
0
summerset
Asked:
summerset
  • 2
  • 2
1 Solution
 
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
 
PortletPaulCommented:
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
 
summersetAuthor Commented:
Thanks for the quick reply. Your example is great learning information.
0
 
PortletPaulCommented:
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now