Solved

SQL Select Prior Ship Date Data

Posted on 2016-11-10
2
29 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 17

Expert Comment

by:Pawan Kumar Khowal
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I have written a PowerShell script to "walk" the security structure of each SQL instance to find:         Each Login (Windows or SQL)             * Its Server Roles             * Every database to which the login is mapped             * The associated "Database User" for this …
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.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

706 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now