SELECT
I.Invoice_Number
, i.INVOICE_TYPE
, CONVERT(MONEY, I.Items_Net) AS Items_Net
, SUM(CONVERT(MONEY, II.Net_Amount)) AS Value
FROM Invoice I
INNER JOIN Invoice_Item II ON I.Invoice_Number = II.Invoice_Number
GROUP BY
I.Invoice_Number
, i.INVOICE_TYPE
, CONVERT(MONEY, I.Items_Net)
HAVING SUM(CONVERT(MONEY, II.Net_Amount)) <> CONVERT(money, I.Items_Net)
ORDER BY
I.Invoice_Number
You can substitute "DECIMAL(12,4)" or similar where you see "MONEY"SELECT
I.Invoice_Number
, i.INVOICE_TYPE
, CONVERT(MONEY, I.Items_Net) AS Items_Net
, CONVERT(MONEY, SUM(II.Net_Amount)) AS Value
FROM Invoice I
INNER JOIN Invoice_Item II ON I.Invoice_Number = II.Invoice_Number
GROUP BY
I.Invoice_Number
, i.INVOICE_TYPE
, CONVERT(MONEY, I.Items_Net)
HAVING CONVERT(MONEY, SUM(II.Net_Amount)) <> CONVERT(MONEY, I.Items_Net)
ORDER BY
I.Invoice_Number
Not sure why you have monetary data in real columns, it isn't a god idea.
ASKER
SELECT
I.Invoice_Number
, i.INVOICE_TYPE
, CASE WHEN I.Invoice_TYPE = 'Product Credit Note' THEN -1.0 * CONVERT(MONEY, I.Items_Net)
ELSE CONVERT(MONEY, I.Items_Net)
END
AS Items_Net
, SUM(CONVERT(MONEY, II.Net_Amount)) AS Value
FROM Invoice I
INNER JOIN Invoice_Item II ON I.Invoice_Number = II.Invoice_Number
GROUP BY
I.Invoice_Number
, i.INVOICE_TYPE
, CASE WHEN I.Invoice_TYPE = 'Product Credit Note' THEN -1.0 * CONVERT(MONEY, I.Items_Net)
ELSE CONVERT(MONEY, I.Items_Net)
END
HAVING SUM(CONVERT(MONEY,II.Net_Amount)) <> CASE WHEN I.Invoice_TYPE = 'Product Credit Note' THEN -1.0 * CONVERT(MONEY, I.Items_Net) ELSE CONVERT(MONEY, I.Items_Net) END
;
SELECT
I.Invoice_Number
, i.INVOICE_TYPE
, Items_Net
, Value
FROM (
SELECT
I.Invoice_Number
, i.INVOICE_TYPE
, CASE WHEN I.Invoice_TYPE = 'Product Credit Note' THEN -1.0 * CONVERT(MONEY, I.Items_Net)
ELSE CONVERT(MONEY, I.Items_Net)
END
AS Items_Net
, SUM(CONVERT(MONEY, II.Net_Amount)) AS Value
FROM Invoice I
INNER JOIN Invoice_Item II ON I.Invoice_Number = II.Invoice_Number
GROUP BY
I.Invoice_Number
, i.INVOICE_TYPE
, CASE WHEN I.Invoice_TYPE = 'Product Credit Note' THEN -1.0 * CONVERT(MONEY, I.Items_Net)
ELSE CONVERT(MONEY, I.Items_Net)
END
) AS d
WHERE Value <> Items_Net
;
nb: I probably will not be online while you read this.
ASKER
ASKER
Microsoft SQL Server is a suite of relational database management system (RDBMS) products providing multi-user database access functionality.SQL Server is available in multiple versions, typically identified by release year, and versions are subdivided into editions to distinguish between product functionality. Component services include integration (SSIS), reporting (SSRS), analysis (SSAS), data quality, master data, T-SQL and performance tuning.
TRUSTED BY
CAST(Value AS DECIMAL(9,2), i.INVOICE_TYPE
From Invoice I
Inner Join Invoice_Item II on I.Invoice_Number = II.Invoice_Number
Group By I.Invoice_Number, I.Items_Net,i.INVOICE_TYPE
having Sum(II.Net_Amount)<>I.Item
Order By I.Invoice_Number