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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Nested Queries yet again.

Author  Topic 

DavidChel
Constraint Violating Yak Guru

474 Posts

Posted - 2008-11-19 : 16:39:59
Here is my code (yes, I know it's ugly as heck)

SELECT CMA.InvoiceNum,
M2M.InvoiceNumber,
CMA.fsono,
CMA.fprodcl,
CMA.fpartno,
CMA.frev,
CMA.fcustno,
CMA.fccompany,
CMA.ftotprice,
CMA.fnmatlcost,
CMA.fnlabcost,
CMA.fnovhdcost,
CMA.fnothrcost
FROM cheltoncustomizations.dbo.ComantAnalysis1 CMA
full OUTER JOIN
(SELECT right(AM.fcinvoice, 5) AS 'InvoiceNumber',
AM.finvdate AS 'InvoiceDate',
-- SOM.fsono AS 'OrderNumber',
AM.fpono AS 'ContractNumber',
AI.FPRODCL AS 'ProductType',
AI.fpartno AS 'SKUNumber',
/* AI.frev AS 'ProductDescription',
SL.fcustno AS 'CustomerID',
AM.fccompany AS 'CustomerName',
*/
SL.fcusrchr1 AS 'CustomerClass',
SM.fccity AS 'DeliveryCity',
SM.fcstate AS 'DeliveryState',
SM.fczip AS 'DeliveryZip',
AM.fsalespn AS 'SalesRep',
/*
' ' AS 'AccountRepName',
' ' AS 'BranchName',
' ' AS 'BranchNumber',
AI.ftotprice AS 'ListPrice',
' ' AS 'TierPrice',
AI.ftotprice AS 'InvoicePrice', */
(CASE SOM.fterm
WHEN 'A' THEN 'NET 30'
WHEN 'I' THEN 'NET 60'
WHEN 'COD' THEN 'COD'
WHEN '0189' THEN 'NET 15'
WHEN 'C004' THEN '1% 15, NET 30'
WHEN '0191' THEN '1/2% 15, NET 45'
WHEN '0170' THEN 'DUE NOW'
WHEN '0187' THEN 'NOT A CUSTOMER YET'
WHEN 'D' THEN 'NET 45'
WHEN 'B' THEN 'NET 10'
WHEN 'J' THEN 'PREPAID'
WHEN 'G' THEN '1/2% 15, NET 30'
WHEN '0192' THEN 'NET 90'
ELSE SOM.fterm
END)
AS 'PaymentTerms',
/*
' ' AS 'DeductionAmount',
' ' AS 'DeductionType',
' ' AS 'ExceptionFlag',
(SOR.fmatlcost/SOR.forderqty)*AI.fshipqty AS 'MaterialCost',
(sor.ftoolcost/SOR.forderqty)*AI.fshipqty AS 'ToolingCost',
(sor.flabcost/SOR.forderqty)*AI.fshipqty AS 'LaborCost',
(sor.fovhdcost/SOR.forderqty)*AI.fshipqty AS 'OverheadCost',
(sor.fsubcost/SOR.forderqty)*AI.fshipqty AS 'SubContractCost',
(sor.fothrcost/SOR.forderqty)*AI.fshipqty AS 'OtherCosts',
(sor.fsetupcost/SOR.forderqty)*AI.fshipqty AS 'SetUpCost',
(SOR.fmatlcost
+ sor.ftoolcost
+ sor.flabcost
+ sor.fovhdcost
+ sor.fsubcost
+ sor.fothrcost
+ sor.fsetupcost)
AS 'DirectProductCost',
' ' AS 'FreightCost', */
AM.ffreight AS 'FreightCharged'
/*' ' AS 'WarrantyCosts',
' ' AS 'TechSupport',
' ' AS 'OtherOperatingCosts'*/
FROM dbo.slcdpm SL
INNER JOIN
dbo.armast AM
ON AM.fcustno = SL.fcustno
INNER JOIN
dbo.aritem AI
ON AM.fcinvoice = AI.fcinvoice
LEFT OUTER JOIN
dbo.somast SOM
ON left(AI.fsokey, 6) = SOM.fsono
LEFT OUTER JOIN
dbo.shmast SM
ON left(AI.fshipkey, 6) = SM.fshipno
LEFT OUTER JOIN
dbo.sorels SOR
ON AI.fsokey = sor.fsono + sor.finumber + sor.frelease
WHERE SL.ftype = 'C' AND year(AM.finvdate) >= 2006
AND rtrim(ltrim(AM.fcinvoice)) LIKE
'[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]')
M2M
ON CMA.InvoiceNum = M2M.InvoiceNumber


I was originally going to inner join these together. However, I wasn't getting any records. I switched to full outer so I could see what the problem is. Apparently it doesn't like how I've given my second query the alias "M2M" as it doesn't return anything at all from that query.

What am I doing wrong here?

hanbingl
Aged Yak Warrior

652 Posts

Posted - 2008-11-19 : 16:51:55
You just said your "M2M" isn't returning any results. That's the problem. Your LIKE operator doesn't have a wildcard.. maybe that's the problem.
Go to Top of Page

DavidChel
Constraint Violating Yak Guru

474 Posts

Posted - 2008-11-19 : 16:55:10
It returns results as it's own query, but doesn't as the sub query.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-11-20 : 01:11:59
thats means the InvoiceNumber values returned by M2M doesnt mach with ones in cheltoncustomizations.dbo.ComantAnalysis1 table. You're actually taking only rightmost 5 characters of AM.fcinvoice field as InvoiveNumber. Are you sure that ComantAnalysis1 table has only 5 character invoice number values? if not that might be problem.
Also you're taking full outer join. so make sure you include any filter conditions of fields in on condition rather than where condition after posted code if any are present.
Go to Top of Page
   

- Advertisement -