Solved

SQL Select Prior Ship Date Data

Posted on 2016-11-10
2
56 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 22

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

820 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