Solved

SQL Select Prior Ship Date Data

Posted on 2016-11-10
2
49 Views
Last Modified: 2016-11-16
Receiving incorrect syntax error near H in the last line of my code attempting to select all data from yesterday.

SELECT  CASE WHEN H.inv_num = 0 THEN SPACE(24)
             ELSE ' Invoice No.: ' + RTRIM(CAST(H.inv_num AS CHAR(24)))
        END AS cSKU ,
        ( (H.ar_post) ) AS AR_POST ,
        ( (H.bill_num) ) AS BILL_NUM ,
        ( (H.carton) ) AS CARTON ,
        ( (H.cncl_num) ) AS CNCL_NUM ,
        ( (H.consol_num) ) AS CONSOL_NUM ,
        ( (H.co_rate) ) AS CO_RATE ,
        ( (H.customer) ) AS CUSTOMER ,
        ( (H.division) ) AS DIVISION ,
        ( ( H.frgt_amt ) * H.co_rate ) AS FRGT_AMT ,
        ( (H.inv_num) ) AS INV_NUM ,
        ( (H.location) ) AS LOCATION ,
        ( (H.mani_num) ) AS MANI_NUM ,
        ( ( H.misc_amt ) * H.co_rate ) AS MISC_AMT ,
        ( (H.ord_num) ) AS ORD_NUM ,
        ( (H.ord_qty) ) AS ORD_QTY ,
        ( (H.pick_num) ) AS PICK_NUM ,
        ( (H.po_num) ) AS PO_NUM ,
        ( (H.proc_856) ) AS PROC_856 ,
        ( ( H.zsumorderextendedstandardcost ) * H.co_rate ) AS SFCORDEREXTENDEDCOST_B ,
        ( ( CASE WHEN H.ord_status <> 'C' THEN H.zordgrossext
                 ELSE 0
            END ) * H.co_rate ) AS SFCORDEREXTGROSSPRICE_B ,
        ( ( ROUND(H.zsumordextdetaildisc, 5) ) * H.co_rate ) AS SFCTOTALLINEDISCOUNT_B ,
        ( ( I.zinvoiced_amount ) * H.co_rate ) AS SFC_INV_AMOUNT_BH ,
        ( (CAST(CASE WHEN ( YEAR(H.ship_date) * 100 + MONTH(H.ship_date) ) = 190001
                     THEN 0
                     ELSE ( YEAR(H.ship_date) * 100 + MONTH(H.ship_date) )
                END AS VARCHAR(6))) ) AS SFSSHIP_DATE_YYYYMM_B ,
        ( (ORDAD2.S_STATE) ) AS SFS_SHIP_STATE_V ,
        ( (ORDAD2.S_ZIPCODE) ) AS SFS_SHIP_ZIP_V ,
        ( (H.shipper) ) AS SHIPPER ,
        ( (H.ship_date) ) AS SHIP_DATE ,
        ( (H.slsperson1) ) AS SLSPERSON1 ,
        ( (H.store) ) AS STORE ,
        ( ( H.tax_amt ) * H.co_rate ) AS TAX_AMT ,
        ( (H.weight) ) AS WEIGHT ,
        H.co_curr AS SFC_CURR_CODE ,
        H.co_rate AS SFC_RPTRATE
FROM    zvoordersh H
        LEFT OUTER JOIN zzxcbolr CB ON H.customer = CB.CUSTOMER
                                       AND H.bill_num = CB.BILL_NUM
        LEFT JOIN ( SELECT  pick_num ,
                            MIN(carton_id) AS CARTON_ID
                    FROM    zzoupsin
                    GROUP BY pick_num
                  ) LTN ON LTN.pick_num = H.pick_num
        LEFT OUTER JOIN zzxcustr CR ON H.customer = CR.customer
        LEFT OUTER JOIN zzxcslsr CS ON H.customer = CS.customer
                                       AND H.store = CS.store
                                       AND H.department = CS.department
                                       AND H.division = CS.division
                                       AND H.ord_type = CS.ord_type
                                       AND CS.active_rec = 'Y'
        LEFT OUTER JOIN vvoshprh I ON H.pkey = I.pkey
                                      AND H.inv_num = I.inv_num
        LEFT OUTER JOIN zzordad2 ORDAD2 ON H.ord_num = ORDAD2.ORD_NUM
                                           AND H.pick_num = ORDAD2.PICK_NUM
                                           AND H.inv_num = ORDAD2.INV_NUM
                                           AND H.cncl_num = ORDAD2.CNCL_NUM
WHERE   ( (H.division = 'KLL') )
        AND ( (H.inv_num > 0) )
        --AND ( (H.ship_date = {TS '2016-11-09 00:00:00' }) )
		 (H.ship_date = DATEADD(d,-1,GETDATE()) )

Open in new window

0
Comment
Question by:mburk1968
2 Comments
 
LVL 21

Accepted Solution

by:
JesterToo earned 500 total points
ID: 41882896
Put an "AND" in the beginning of line 66 like this...

AND  (H.ship_date = DATEADD(d,-1,GETDATE()) )
0
 
LVL 28

Expert Comment

by:Pawan Kumar
ID: 41883201
Yes looking at your code it looks like AND is missing. If it OR then make changes accordingly.

Also note that you are using lot of extra brackets which we can remove. 'AS' keyword can also be removed. We can directly type the name of the column,

Also use SINGLE character as alias name. This will help you with more readability.


SELECT  CASE WHEN H.inv_num = 0 THEN SPACE(24)
             ELSE ' Invoice No.: ' + RTRIM(CAST(H.inv_num AS CHAR(24)))
        END cSKU ,
        ( (H.ar_post) ) AS AR_POST ,
        ( (H.bill_num) ) AS BILL_NUM ,
        ( (H.carton) ) AS CARTON ,
        ( (H.cncl_num) ) AS CNCL_NUM ,
        ( (H.consol_num) ) AS CONSOL_NUM ,
        ( (H.co_rate) ) AS CO_RATE ,
        ( (H.customer) ) AS CUSTOMER ,
        ( (H.division) ) AS DIVISION ,
        ( ( H.frgt_amt ) * H.co_rate ) AS FRGT_AMT ,
        ( (H.inv_num) ) AS INV_NUM ,
        ( (H.location) ) AS LOCATION ,
        ( (H.mani_num) ) AS MANI_NUM ,
        ( ( H.misc_amt ) * H.co_rate ) AS MISC_AMT ,
        ( (H.ord_num) ) AS ORD_NUM ,
        ( (H.ord_qty) ) AS ORD_QTY ,
        ( (H.pick_num) ) AS PICK_NUM ,
        ( (H.po_num) ) AS PO_NUM ,
        ( (H.proc_856) ) AS PROC_856 ,
        ( ( H.zsumorderextendedstandardcost ) * H.co_rate ) AS SFCORDEREXTENDEDCOST_B ,
        ( ( CASE WHEN H.ord_status <> 'C' THEN H.zordgrossext
                 ELSE 0
            END ) * H.co_rate ) AS SFCORDEREXTGROSSPRICE_B ,
        ( ( ROUND(H.zsumordextdetaildisc, 5) ) * H.co_rate ) AS SFCTOTALLINEDISCOUNT_B ,
        ( ( I.zinvoiced_amount ) * H.co_rate ) AS SFC_INV_AMOUNT_BH ,
        ( (CAST(CASE WHEN ( YEAR(H.ship_date) * 100 + MONTH(H.ship_date) ) = 190001
                     THEN 0
                     ELSE ( YEAR(H.ship_date) * 100 + MONTH(H.ship_date) )
                END AS VARCHAR(6))) ) AS SFSSHIP_DATE_YYYYMM_B ,
        ( (ORDAD2.S_STATE) ) AS SFS_SHIP_STATE_V ,
        ( (ORDAD2.S_ZIPCODE) ) AS SFS_SHIP_ZIP_V ,
        ( (H.shipper) ) AS SHIPPER ,
        ( (H.ship_date) ) AS SHIP_DATE ,
        ( (H.slsperson1) ) AS SLSPERSON1 ,
        ( (H.store) ) AS STORE ,
        ( ( H.tax_amt ) * H.co_rate ) AS TAX_AMT ,
        ( (H.weight) ) AS WEIGHT ,
        H.co_curr AS SFC_CURR_CODE ,
        H.co_rate AS SFC_RPTRATE
FROM    zvoordersh H
        LEFT OUTER JOIN zzxcbolr CB ON H.customer = CB.CUSTOMER
                                       AND H.bill_num = CB.BILL_NUM
        LEFT JOIN ( SELECT  pick_num ,
                            MIN(carton_id) AS CARTON_ID
                    FROM    zzoupsin
                    GROUP BY pick_num
                  ) LTN ON LTN.pick_num = H.pick_num
        LEFT OUTER JOIN zzxcustr CR ON H.customer = CR.customer
        LEFT OUTER JOIN zzxcslsr CS ON H.customer = CS.customer
                                       AND H.store = CS.store
                                       AND H.department = CS.department
                                       AND H.division = CS.division
                                       AND H.ord_type = CS.ord_type
                                       AND CS.active_rec = 'Y'
        LEFT OUTER JOIN vvoshprh I ON H.pkey = I.pkey
                                      AND H.inv_num = I.inv_num
        LEFT OUTER JOIN zzordad2 ORDAD2 ON H.ord_num = ORDAD2.ORD_NUM
                                           AND H.pick_num = ORDAD2.PICK_NUM
                                           AND H.inv_num = ORDAD2.INV_NUM
                                           AND H.cncl_num = ORDAD2.CNCL_NUM
WHERE   H.division = 'KLL'
        AND H.inv_num > 0
        AND H.ship_date = DATEADD(d,-1,GETDATE())

Open in new window

0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question