Solved

SQL Select Prior Ship Date Data

Posted on 2016-11-10
2
64 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 29

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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

     When we have to pass multiple rows of data to SQL Server, the developers either have to send one row at a time or come up with other workarounds to meet requirements like using XML to pass data, which is complex and tedious to use. There is a …
Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

707 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