我有一个返回多行且具有相同列的查询,我不知道这些额外列的来源。
select AccountNum,voucher into #temp from LEDGERTRANS join CUSTINVOICETABLE on CUSTINVOICETABLE.INVOICEID = LedgerTrans.voucher
返回
AccountNum Voucher
110100 mCR1
400850 mCR1
515000 mCR1
409000 mCR1
这很酷,因为它们各自代表不同的金额。 但是,当我运行主查询时:
SELECT distinct
NAME AS 'Customer',
ORDERACCOUNT AS 'Acct #',
'' AS 'Sales Order',
INVOICEDATE AS 'Date',
INVOICEID AS 'Invoice',
CUSTINVOICELINE.AMOUNTCUR AS 'Inv Amt',
'' AS 'Itemid',
(CASE CREDITREASON WHEN 1 THEN 'Other'
WHEN 2 THEN 'Damaged'
WHEN 3 THEN 'Order Entry'
WHEN 4 THEN 'Outside Service'
WHEN 5 THEN 'Pricing'
WHEN 6 THEN 'Quality'
WHEN 7 THEN 'Rework'
WHEN 8 THEN 'Ruined & Lost'
WHEN 9 THEN 'Sales Agreement'
WHEN 10 THEN 'Freight'
WHEN 11 THEN 'Quanity'
WHEN 12 THEN 'Customer Service'
ELSE 'None' END) AS 'Reason',
dbo.DIMENSIONS.DESCRIPTION AS 'Division',
CUSTINVOICETABLE.CREATEDBY,
null as 'CreatedDate',
#temp.AccountNum + ' ' + LEDGERTABLE.ACCOUNTNAME As 'Account'
--#temp1.Dimension as 'Department'
FROM
dbo.CUSTINVOICETABLE INNER JOIN
dbo.CUSTINVOICELINE ON dbo.CUSTINVOICETABLE.RECID = PARENTRECID INNER JOIN
dbo.DIMENSIONS ON NUM = dbo.CUSTINVOICETABLE.DIMENSION2_
join #temp on Custinvoicetable.InvoiceID = #temp.voucher
left JOIN LEDGERTABLE on LEDGERTABLE.AccountNum = #temp.AccountNum
WHERE
dbo.CUSTINVOICELINE.DIMENSION2_ IN ('23')
AND
(INVOICEID LIKE 'MCR-%'
OR
INVOICEID LIKE 'MIV-%')
AND
INVOICEDATE BETWEEN '01/01/2014' AND '01/31/2014'
AND
CREATEDBY IN ('clk')
AND LEDGERTABLE.ACCOUNTNAME NOT Like ('%Accounts Receivable%')
它将返回包含该表中未包含的代码帐户号的美元金额的多个相同结果。这可以在最后一栏中找到(这些是帐户代码以及一些连接的措辞)我做错了什么?
Schaller Corporation 112523 2014-01-24 00:00:00.000 MCR-00005142 -250.000000000000 Other Stephens Road-Detroit clk NULL 400850 Sales-Washing
Schaller Corporation 112523 2014-01-24 00:00:00.000 MCR-00005142 -250.000000000000 Other Stephens Road-Detroit clk NULL 409000 Sales-Ruined & Lost
Schaller Corporation 112523 2014-01-24 00:00:00.000 MCR-00005142 -250.000000000000 Other Stephens Road-Detroit clk NULL 515000 Outside Services