Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
theHydra
Starting Member
42 Posts |
Posted - 2011-09-23 : 13:50:57
|
| I have a report I wrote awhile back that basically pulls in all our sales for a given period of time. It works great but now I need to figure out what to do with NULL values. For example the report has a column for part number, description and customer address, but SHIPPING costs for example doesn't have a SHIP TO address.I have the report set up to run in Excel and refresh whenever it's opened. But all the line items for SHIPPING have blank entries and per new procedures that can't happen.So long story short...I want to figure out a way (in excel) to say.If ship to address is blank, then check order number, if order number matches any other order number that does have ship to address, then insert ship to address in the blank field.I'm torn between what's fastest and easiest, doing it as a part of my SQL statement, or doing it in Excel.Any recommendations are greatly appreciated.Thanks all. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-09-23 : 13:57:29
|
if you want to do this in SQL, you can do like belowSELECT t.orderno, COALESCE(t.ShiptoAddress,t1.ShipToAddress) AS ShiptoAddress,other columns....FROM Table tOUTER APPLY (SELECT TOP 1 ShipToAddress FROM Table WHERE orderno= t.orderno )t1 ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
theHydra
Starting Member
42 Posts |
Posted - 2011-09-23 : 14:56:01
|
| I think I understand what you're trying to do, but can you help me format my current query to accommodate your recommendation?Here's my code as of now.SELECTcase when dbo.aritem.fpartno = 'SHIPPING' then 'Y' else '' end,dbo.armast.fcstatus AS Status,dbo.armast.fcinvoice AS Invoice,dbo.armast.finvdate AS Invoice_Date,dbo.armast.fcustno AS Cust_Number,dbo.armast.fbcompany AS Bill_To_Company,dbo.armast.fshipdate AS Ship_Date,dbo.armast.fsono AS SO_Number,dbo.aritem.fpartno AS Part_No,dbo.aritem.ftotprice AS Total_Price,dbo.aritem.fitem AS Line_Item,dbo.shmast.fccity AS Ship_To_City,dbo.shmast.fcstate AS Ship_To_State,dbo.shmast.fccountry AS Ship_To_Country,dbo.shmast.fczip AS Ship_To_Zip,dbo.shmast.fshipno AS ShipperFROM dbo.armast INNER JOIN dbo.aritem ON dbo.armast.fcinvoice = dbo.aritem.fcinvoice LEFT OUTER JOIN dbo.shitem ON dbo.aritem.fshipkey = (shitem.fshipno + shitem.fitemno) LEFT OUTER JOIN dbo.shmast ON dbo.shmast.fshipno = shitem.fshipnowhere armast.finvdate > = DATEADD(dd, 0, DATEDIFF(dd, 15, GETDATE()))ORDER BY dbo.armast.fcinvoice ASCThanks for the reply. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-09-23 : 23:23:32
|
| what corresponds to order number here?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
theHydra
Starting Member
42 Posts |
Posted - 2011-09-26 : 09:04:57
|
| Actually lets do invoice number. dbo.armast.fcinvoice AS InvoiceThanks again. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-09-26 : 11:24:32
|
| [code]SELECTcase when dbo.aritem.fpartno = 'SHIPPING' then 'Y' else '' end AS Shipping,dbo.armast.fcstatus AS Status,dbo.armast.fcinvoice AS Invoice,dbo.armast.finvdate AS Invoice_Date,dbo.armast.fcustno AS Cust_Number,dbo.armast.fbcompany AS Bill_To_Company,dbo.armast.fshipdate AS Ship_Date,dbo.armast.fsono AS SO_Number,dbo.aritem.fpartno AS Part_No,dbo.aritem.ftotprice AS Total_Price,dbo.aritem.fitem AS Line_Item,dbo.shmast.fccity AS Ship_To_City,dbo.shmast.fcstate AS Ship_To_State,dbo.shmast.fccountry AS Ship_To_Country,dbo.shmast.fczip AS Ship_To_Zip,dbo.shmast.fshipno AS Shipper INTO #TempFROMdbo.armastINNER JOINdbo.aritemON dbo.armast.fcinvoice = dbo.aritem.fcinvoiceLEFT OUTER JOINdbo.shitemON dbo.aritem.fshipkey = (shitem.fshipno + shitem.fitemno)LEFT OUTER JOIN dbo.shmastON dbo.shmast.fshipno = shitem.fshipnowhere armast.finvdate > = DATEADD(dd, 0, DATEDIFF(dd, 15, GETDATE()))SELECT Shipping,Status,Invoice,Invoice_Date,Cust_Number,Bill_To_Company,Ship_Date,SO_Number,Part_No,Total_Price,Line_Item,COALESCE(t1.Ship_To_City,t2.Ship_To_City) AS Ship_To_City,COALESCE(t1.Ship_To_State,t2.Ship_To_State) AS Ship_To_State,COALESCE(t1.Ship_To_Country,t2.Ship_To_Country) AS Ship_To_Country,COALESCE(t1.Ship_To_Zip,t2.Ship_To_Zip) AS Ship_To_Zip,Shipper FROM #Temp t1OUTER APPLY (SELECT TOP 1 Ship_To_City,Ship_To_State,Ship_To_Country,Ship_To_Zip FROM Table WHERE fcinvoice = t1.fcinvoice )t2ORDER BY t1.fcinvoice ASC[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
theHydra
Starting Member
42 Posts |
Posted - 2011-09-26 : 12:29:32
|
| Ran exactly as you posted, but got a 'incorrect syntax near the keyword TABLE' in the OUTER APPLY section.I changed the FROM Table to ARMAST, but after that I get a 'SQL Server Database Error: There is already an object named '#Temp' in the database.'error.Any ideas? |
 |
|
|
theHydra
Starting Member
42 Posts |
Posted - 2011-09-26 : 12:57:44
|
| UPDATE:I changed the FROM TABLE in the OUTER APPLY section to SHMAST, since that looks like the tables you're selecting, but now I get invalid column name in the WHERE statement in the OUTER APPLY. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-09-26 : 13:02:08
|
quote: Originally posted by theHydra Ran exactly as you posted, but got a 'incorrect syntax near the keyword TABLE' in the OUTER APPLY section.I changed the FROM Table to ARMAST, but after that I get a 'SQL Server Database Error: There is already an object named '#Temp' in the database.'error.Any ideas?
add a drop statement beforeIF OBJECT_ID('tempdb..#temp') IS NOT NULL DROP TABLE #TempSELECTcase when dbo.aritem.fpartno = 'SHIPPING' then 'Y' else '' end AS Shipping,dbo.armast.fcstatus AS Status,dbo.armast.fcinvoice AS Invoice,dbo.armast.finvdate AS Invoice_Date,dbo.armast.fcustno AS Cust_Number,dbo.armast.fbcompany AS Bill_To_Company,dbo.armast.fshipdate AS Ship_Date,dbo.armast.fsono AS SO_Number,dbo.aritem.fpartno AS Part_No,dbo.aritem.ftotprice AS Total_Price,dbo.aritem.fitem AS Line_Item,dbo.shmast.fccity AS Ship_To_City,dbo.shmast.fcstate AS Ship_To_State,dbo.shmast.fccountry AS Ship_To_Country,dbo.shmast.fczip AS Ship_To_Zip,dbo.shmast.fshipno AS Shipper INTO #TempFROMdbo.armastINNER JOINdbo.aritemON dbo.armast.fcinvoice = dbo.aritem.fcinvoiceLEFT OUTER JOINdbo.shitemON dbo.aritem.fshipkey = (shitem.fshipno + shitem.fitemno)LEFT OUTER JOIN dbo.shmastON dbo.shmast.fshipno = shitem.fshipnowhere armast.finvdate > = DATEADD(dd, 0, DATEDIFF(dd, 15, GETDATE()))SELECT Shipping,Status,Invoice,Invoice_Date,Cust_Number,Bill_To_Company,Ship_Date,SO_Number,Part_No,Total_Price,Line_Item,COALESCE(t1.Ship_To_City,t2.Ship_To_City) AS Ship_To_City,COALESCE(t1.Ship_To_State,t2.Ship_To_State) AS Ship_To_State,COALESCE(t1.Ship_To_Country,t2.Ship_To_Country) AS Ship_To_Country,COALESCE(t1.Ship_To_Zip,t2.Ship_To_Zip) AS Ship_To_Zip,Shipper FROM #Temp t1OUTER APPLY (SELECT TOP 1 Ship_To_City,Ship_To_State,Ship_To_Country,Ship_To_Zip FROM Table WHERE fcinvoice = t1.fcinvoice )t2ORDER BY t1.fcinvoice ASC------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
theHydra
Starting Member
42 Posts |
Posted - 2011-09-26 : 13:12:16
|
| Still getting that invoice error. Here's the code i'm working off of.IF OBJECT_ID('tempdb..#temp') IS NOT NULLDROP TABLE #TempSELECT dbo.armast.fcstatus AS Status,dbo.armast.fcinvoice AS Invoice,dbo.armast.finvdate AS Invoice_Date,dbo.armast.fcustno AS Cust_Number,dbo.armast.fbcompany AS Bill_To_Company,dbo.armast.fshipdate AS Ship_Date,dbo.armast.fsono AS SO_Number,dbo.aritem.fpartno AS Part_No,dbo.aritem.ftotprice AS Total_Price,dbo.aritem.fitem AS Line_Item,dbo.shmast.fccity AS Ship_To_City,dbo.shmast.fcstate AS Ship_To_State,dbo.shmast.fccountry AS Ship_To_Country,dbo.shmast.fczip AS Ship_To_Zip,dbo.shmast.fshipno AS Shipper INTO #TempFROMdbo.armastINNER JOINdbo.aritemON dbo.armast.fcinvoice = dbo.aritem.fcinvoiceLEFT OUTER JOINdbo.shitemON dbo.aritem.fshipkey = (shitem.fshipno + shitem.fitemno)LEFT OUTER JOIN dbo.shmastON dbo.shmast.fshipno = shitem.fshipnowhere armast.finvdate > = DATEADD(dd, 0, DATEDIFF(dd, 15, GETDATE()))SELECT Shipper,Status,Invoice,Invoice_Date,Cust_Number,Bill_To_Company,Ship_Date,SO_Number,Part_No,Total_Price,Line_Item,COALESCE(t1.Ship_To_City,t2.Ship_To_City) AS Ship_To_City,COALESCE(t1.Ship_To_State,t2.Ship_To_State) AS Ship_To_State,COALESCE(t1.Ship_To_Country,t2.Ship_To_Country) AS Ship_To_Country,COALESCE(t1.Ship_To_Zip,t2.Ship_To_Zip) AS Ship_To_Zip,Shipper FROM #Temp t1OUTER APPLY (SELECT TOP 1 Ship_To_City,Ship_To_State,Ship_To_Country,Ship_To_Zip FROM SHMAST WHERE fcinvoice = t1.fcinvoice )t2ORDER BY t1.fcinvoice ASC |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-09-26 : 13:25:42
|
| [code]IF OBJECT_ID('tempdb..#temp') IS NOT NULLDROP TABLE #TempSELECTdbo.armast.fcstatus AS Status,dbo.armast.fcinvoice AS Invoice,dbo.armast.finvdate AS Invoice_Date,dbo.armast.fcustno AS Cust_Number,dbo.armast.fbcompany AS Bill_To_Company,dbo.armast.fshipdate AS Ship_Date,dbo.armast.fsono AS SO_Number,dbo.aritem.fpartno AS Part_No,dbo.aritem.ftotprice AS Total_Price,dbo.aritem.fitem AS Line_Item,dbo.shmast.fccity AS Ship_To_City,dbo.shmast.fcstate AS Ship_To_State,dbo.shmast.fccountry AS Ship_To_Country,dbo.shmast.fczip AS Ship_To_Zip,dbo.shmast.fshipno AS Shipper INTO #TempFROMdbo.armastINNER JOINdbo.aritemON dbo.armast.fcinvoice = dbo.aritem.fcinvoiceLEFT OUTER JOINdbo.shitemON dbo.aritem.fshipkey = (shitem.fshipno + shitem.fitemno)LEFT OUTER JOIN dbo.shmastON dbo.shmast.fshipno = shitem.fshipnowhere armast.finvdate > = DATEADD(dd, 0, DATEDIFF(dd, 15, GETDATE()))SELECT Shipper,Status,Invoice,Invoice_Date,Cust_Number,Bill_To_Company,Ship_Date,SO_Number,Part_No,Total_Price,Line_Item,COALESCE(t1.Ship_To_City,t2.Ship_To_City) AS Ship_To_City,COALESCE(t1.Ship_To_State,t2.Ship_To_State) AS Ship_To_State,COALESCE(t1.Ship_To_Country,t2.Ship_To_Country) AS Ship_To_Country,COALESCE(t1.Ship_To_Zip,t2.Ship_To_Zip) AS Ship_To_Zip,Shipper FROM #Temp t1OUTER APPLY (SELECT TOP 1 Ship_To_City,Ship_To_State,Ship_To_Country,Ship_To_ZipFROM #TempWHERE fcinvoice = t1.fcinvoice )t2ORDER BY t1.fcinvoice ASC[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
theHydra
Starting Member
42 Posts |
Posted - 2011-09-26 : 13:36:50
|
| So sorry, but same error, this time it looks like the error is for the Invoice in the SELECT statement right before the COALESCE. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-09-26 : 13:50:41
|
| can you post the exact error message?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
theHydra
Starting Member
42 Posts |
Posted - 2011-09-26 : 13:52:16
|
|
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-09-26 : 14:00:22
|
ok..got itIF OBJECT_ID('tempdb..#temp') IS NOT NULLDROP TABLE #TempSELECTdbo.armast.fcstatus AS Status,dbo.armast.fcinvoice AS Invoice,dbo.armast.finvdate AS Invoice_Date,dbo.armast.fcustno AS Cust_Number,dbo.armast.fbcompany AS Bill_To_Company,dbo.armast.fshipdate AS Ship_Date,dbo.armast.fsono AS SO_Number,dbo.aritem.fpartno AS Part_No,dbo.aritem.ftotprice AS Total_Price,dbo.aritem.fitem AS Line_Item,dbo.shmast.fccity AS Ship_To_City,dbo.shmast.fcstate AS Ship_To_State,dbo.shmast.fccountry AS Ship_To_Country,dbo.shmast.fczip AS Ship_To_Zip,dbo.shmast.fshipno AS Shipper INTO #TempFROMdbo.armastINNER JOINdbo.aritemON dbo.armast.fcinvoice = dbo.aritem.fcinvoiceLEFT OUTER JOINdbo.shitemON dbo.aritem.fshipkey = (shitem.fshipno + shitem.fitemno)LEFT OUTER JOIN dbo.shmastON dbo.shmast.fshipno = shitem.fshipnowhere armast.finvdate > = DATEADD(dd, 0, DATEDIFF(dd, 15, GETDATE()))SELECT Shipper,Status,Invoice,Invoice_Date,Cust_Number,Bill_To_Company,Ship_Date,SO_Number,Part_No,Total_Price,Line_Item,COALESCE(t1.Ship_To_City,t2.Ship_To_City) AS Ship_To_City,COALESCE(t1.Ship_To_State,t2.Ship_To_State) AS Ship_To_State,COALESCE(t1.Ship_To_Country,t2.Ship_To_Country) AS Ship_To_Country,COALESCE(t1.Ship_To_Zip,t2.Ship_To_Zip) AS Ship_To_Zip,Shipper FROM #Temp t1OUTER APPLY (SELECT TOP 1 Ship_To_City,Ship_To_State,Ship_To_Country,Ship_To_ZipFROM #TempWHERE invoice = t1.invoice )t2ORDER BY t1.invoice ASC------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
theHydra
Starting Member
42 Posts |
Posted - 2011-09-26 : 14:12:49
|
Close, real close.It runs now, no errors, but some are still pulling in nulls whereas some aren't. I'm trying to figure out why... |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-09-26 : 14:19:03
|
| that means it has only one record for same invoice and it doesnt have match address------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
theHydra
Starting Member
42 Posts |
Posted - 2011-09-26 : 14:24:27
|
| So do you think there's a way around that? |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-09-26 : 14:31:40
|
first check if they've at least one record with not null values for address. what does below return?SELECT InvoiceFROM #TempGROUP BY InvoiceHAVING SUM(CASE WHEN Ship_To_City IS NOT NULL THEN 1 ELSE 0 END) =0 ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
theHydra
Starting Member
42 Posts |
Posted - 2011-09-26 : 15:31:36
|
| I'm having a hard time running that, but I think I understand what you're getting at.If you look a that screen shot I sent a few posts back, under part number, there will be 1 of 3 things that show up, an actual part number, SHIPPING or PAYMENT. Under SHIPPING and PAYMENT there is never an address (which was the initial problem), so it's null.If there's a part number, it has an address, it has to, or it wouldn't invoice, and that's what we're pulling from, ARMAST, the invoice master pretty much.Hope that helps. |
 |
|
|
theHydra
Starting Member
42 Posts |
Posted - 2011-09-28 : 12:43:17
|
| Hi visakh16, hopefully you're still following this thread.I've been trying to run this query in Excel, but I keep getting errors (Excel errors, not because of the SQL query)Any idea what Excel is choking on?Thanks |
 |
|
|
Next Page
|
|
|
|
|