T-SQL Query - Return One Result, or return 0 (SQL 2008R2) - sql

I'm quite the novice when it comes to T-SQL, and I can't figure out this query.
I am trying to compare an Item's value to it's ledger Value to check for any anomalies, because occasionally they go out of sync.
I need the query to, for all current stock, subtract the Item Ledger's 'Amount' field from the Item's 'Item Value' field. There are several Item Ledger Entries, it must subtract the amount from the ledger entry that is Open. If there is no Open Ledger Entry, subtract zero instead.
I've butchered together a query in the query designer, apologies for the sloppiness:
SELECT dbo.[Company$ItemTable].[Item No_],
dbo.[Company$ItemTable].[Current Stock],
dbo.[Company$ItemTable].[Item Value],
dbo.[Company$Item Ledger Entry].[Open],
dbo.[Company$ItemTable].[Item Value] - SUM(CASE
WHEN dbo.[Company$Item Ledger Entry].[Open] = 1 THEN dbo.[Company$Item Ledger Entry].Amount
ELSE 0
END) AS Diff,
SUM(CASE
WHEN dbo.[Company$Item Ledger Entry].[Open] = 1 THEN dbo.[Company$Item Ledger Entry].Amount
ELSE 0
END) AS AmountILE,
dbo.[Company$Item Ledger Entry].[Entry No_]
FROM dbo.[Company$ItemTable]
LEFT OUTER JOIN dbo.[Company$Item Ledger Entry]
ON dbo.[Company$ItemTable].[Item No_] = dbo.[Company$Item Ledger Entry].[Lot No_]
GROUP BY dbo.[Company$ItemTable].[Item No_],
dbo.[Company$ItemTable].[Current Stock],
dbo.[Company$ItemTable].[Item Value],
dbo.[Company$Item Ledger Entry].[Open],
dbo.[Company$Item Ledger Entry].[Entry No_]
HAVING ( dbo.[Company$ItemTable].[Current Stock] = 1 )
AND ( dbo.[Company$ItemTable].[Item Value] - SUM(CASE
WHEN dbo.[Company$Item Ledger Entry].[Open] = 1 THEN dbo.[Company$Item Ledger Entry].Amount
ELSE 0
END) > 1 )
AND ( dbo.[Company$Item Ledger Entry].[Open] = 1 )
Does this make sense? Can anyone help? Thanks!

Your table structure is unclear. I can guess what you mean.
Is dbo.[Company$ItemTable].[Item No_] unique in table?
Is dbo.[Company$ItemTable] relates to dbo.[Company$Item Ledger Entry] as one-to-many?
If yes then
SELECT
items.[Item No_],
items.[Current Stock],
items.[Item Value],
items.[Item Value] - ISNULL( le.OpenAmount, 0) AS Diff,
ISNULL( le.OpenAmount, 0) AS AmountILE
FROM dbo.[Company$ItemTable] items
OUTER APPPLY
(
SELECT SUM(e.Amount)
FROM dbo.[Company$Item Ledger Entry] e
WHERE items.[Item No_] = e.[Lot No_]
AND e.[Open] = 1
) le(OpenAmount)
WHERE items.[Current Stock] = 1

Related

Get latest Vendor No. in sql query from Item Ledger Entry table

I'm trying to get last Vendor No. from Item Ledger Entry table with no result.
What should I put to my query to get this latest Vendor No.?
SQL query looks as follow:
SELECT
ile.[Posting Date],
CASE
WHEN ile.[Entry Type]=1 THEN 'Sale'
WHEN ile.[Entry Type]=5 THEN 'Consumption'
END AS Entry_Type,
,ile.[Item No_]
,ile.[Description]
,ile.[Quantity]
,ile.[Unit of Measure Code]
,item.[Description]
--,Last Vendor No.
FROM [MyServer]..[MyDatabase$Item Ledger Entry] as ile with (nolock)
LEFT JOIN [MyServer]..[MyDatabase$Item] as item ON item.[No_]=ile.[Item No_]
--LEFT JOIN
-- place for query to find last Vendor No. for particular Item No.
-- ... FROM [MyServer]..[MyDatabase$Item Ledger Entry] WHERE [Entry Type]=0 and [Source Type]=2
--
WHERE ile.[Entry Type] In (1,5) AND ile.[Item No_] ='123456789';
select top 1 [Source No_]
from [DatabaseName].dbo.[CompanyName$Item Ledger Entry]
where [Source Type] = 2 /* Vendor */
and [Entry Type] = 0 /* Purchase */
and [Item No_] = '<Some Item No.>'
order by [Entry No_] desc

How to SQL join payments with invoices in NAV Dynamics (former Navision)

The question is about SQL fiddling in Dynamics Nav tables, especially the table [$Vendor Ledger Entry]. How to link each payment to appropriate invoices.
The aim is to get [External Document No_] and [Amount] of the invoice. One payment can be matched with many invoices (happens) or one invoice with many payments (rare case).
My desperate approach is:
;with cte as
(
select
*
,[Row_max]=max([Row]) OVER(PARTITION BY CBEN,[Document Type])
from (
select
*
,[Row]=ROW_NUMBER() OVER(PARTITION BY CBEN,[Document Type] ORDER BY [Entry No_] asc)
from (
select
*
,CBEN=case when [Closed by Entry No_]=0 then [Entry No_] else [Closed by Entry No_] end
from [CompanyName$Vendor Ledger Entry] --here put correct table name
) tb1
) tb2
)
select
a.[Entry No_] [Entry No_payment] -- letter t means transfers
,b.[Entry No_] [Entry No_invoice] -- letter p means payables
--,a.CBEN as CBEN_t
--,b.CBEN as CBEN_p
--,a.[Row_max] as Row_max_t
--,b.[Row_max] as Row_max_p
--,a.[Row] as Row_t
--,b.[Row] as Row_p
,a.[Open]
,b.[External Document No_] [External Document No_invoice]
,a.[Document No_] [Document No_payment]
,b.[Document No_] [Document No_invoice]
,b.[Document Date]
,b.[Posting Date]
,b.[Due Date]
,Amount_p=b.[Closed by Amount]
--,a.[Company]
from cte a
left join cte b
on
(
a.CBEN=b.CBEN and -- join using Closed By Entry No
(a.[Row]=b.[Row] and a.[Row_max]=b.[Row_max] or
a.Row_max=1 and not (a.[Row_max]>1 and b.[Row_max]>1) or
b.Row_max=1 and not (a.[Row_max]>1 and b.[Row_max]>1)
) and
a.[Document Type] in (0,1) and
b.[Document Type]=2
and a.[Open]=0
)
or
(
a.CBEN = b.[Entry No_] and -- or join by Closed By Entry No to EntryNo
(a.[Row]=b.[Row] and a.[Row_max]=b.[Row_max] or
a.Row_max=1 and not (a.[Row_max]>1 and b.[Row_max]>1) or
b.Row_max=1 and not (a.[Row_max]>1 and b.[Row_max]>1)
) and
a.[Document Type] in (0,1) and
b.[Document Type]=2
and a.[Open]=0
)
where
a.[Open]=0 and a.[Document Type] in (0,1) and b.[Entry No_] is not null -- show payments with matched invoices
or
a.[Open]=1 and a.[Document Type] in (0,1) and b.[Entry No_] is null -- or payments with open status
order by a.CBEN,1,2,a.[Document No_]
I am at a loss to join Invoice [Amount] and I do not know in which Dynamics NAV table it might be.
the table is
[Detailed Vendor Ledg. Entry]
with [Entry Type] = Application

How do i sum data from 2 tables left joined to 1 table?

When I comment out all lines pertaining to either the "Sales Line" or "Warehouse Entry" table the query works as expected. But when I use this query as is; it returns faulty(unrealistically high by at least 5 digits) data.
The idea is to iterate the "Item" table and to obtain the amount in stock as well as the sum of quantity sold as well as It's worth. I think there must be something wrong by the way I'm joining these tables. Both "Warehouse Entry" and "Sales Line" table have multiple entries that can be linked to I.[No_].
USE NAV2009_R2_SHOWTEX_LIVE
SELECT I.[No_]
,sum(S.[Quantity (Base)]) AS [Quantity Sold]
,min(I.[IP Total (Manual)]) * sum(WE.[Quantity]) AS [Stock Value]
,sum(WE.[Quantity]) AS [Quantity Stock]
FROM [Item] AS I
INNER JOIN [Warehouse Entry] AS WE ON WE.[Item No_] = I.[No_]
AND WE.[Location Code] = 'BU'
AND WE.[Bin Code] <> 'SHIPPING'
AND WE.[Bin Code] <> 'WORKSHOP'
AND WE.[Bin Code] <> 'OUTBOX'
AND WE.[Bin Code] <> 'CUT'
AND WE.[Bin Code] <> 'VERZEND'
INNER JOIN [Sales Line] AS S ON S.[No_] = I.[No_]
AND S.[Shipment Date] > '07/01/2015'
AND S.[Document Type] = 1
WHERE I.[No_] LIKE '140003000007'
GROUP BY I.[No_]
Sample Data
No.|Quantity Sold|Stock Value|Quantity Stock
Wrong Data
140003000007|204484537.36000000000000000000|13051602.124400| 2355884.86000000000000000000
Right Data
140003000007|61703.24000000000000000000|13623.801800|2459.17000000000000000000
SELECT sumQuantityBase as [Quantity Sold],
minIP * sumQuantityBase as [Stock Value],
sumQuantityBase as [Quantity Stock]
FROM
(SELECT MIN(I.[IP Total (Manual)] )as minIP,
No_
FROM I
WHERE [No_] like '140003000007'
GROUP BY No_) MinI INNER JOIN
(SELECT SUM([Quantity]) as sumQuantity,
[Item No_]
FROM WE
WHERE [Location Code] = 'BU'
AND [Bin Code] <> 'SHIPPING'
AND [Bin Code] <> 'WORKSHOP'
AND [Bin Code] <> 'OUTBOX'
AND [Bin Code] <> 'CUT'
AND [Bin Code] <> 'VERZEND'
GROUP BY [Item No_] ) SumWE
ON MinI.[No_] = SumWE[Item No_] INNER JOIN
(SELECT SUM([Quantity (Base)]) as sumQuantityBase,
No_
FROM S
WHERE [Shipment Date] > '07/01/2015'
AND [Shipment Date] IS NOT NULL
AND [Document Type] = 1
GROUP BY No_ ) SumS
ON SumS.[No_] = MinI.[No_]
Try this one. When you want to aggregate data using sum or min function i'd recommend you do it in a subset. that way you keep it simple

Pass value in a Variable

I want to pass the value of 1 of my columns in my SELECT statements in a Variable.
Here's my sample code:
DECLARE #TotalExpense DECIMAL(18,2)
SELECT a.[Posting Date] AS [Check Date],
a.[Document No_] AS [Check Number],
a.[Vendor No_],
(
SELECT SUM(CASE WHEN GLEntry.[VAT Amount] <> 0 THEN CAST(GLEntry.Amount AS DECIMAL(18,2)) ELSE 0 END) FROM [BLI$Detailed Vendor Ledg_ Entry] DtldVendLedger
LEFT JOIN [BLI$G_L Entry] GLEntry
ON GLEntry.[Document No_] = (SELECT VendLedger.[Document No_] FROM [BLI$Vendor Ledger Entry] VendLedger
WHERE VendLedger.[Entry No_] = DtldVendLedger.[Vendor Ledger Entry No_])
WHERE DtldVendLedger.[Entry Type] = 2 AND DtldVendLedger.[Initial Document Type] = 2 AND DtldVendLedger.[Document No_] = a.[Document No_]
AND GLEntry.[Source Code] = 'PURCHASES' AND GLEntry.[G_L Account No_] NOT IN ('2003','1402','1403','1401')
) AS [Vatable],
(
SELECT SUM(CASE WHEN GLEntry.[VAT Amount] <> 0 THEN CAST(GLEntry.Amount * 0.12 AS DECIMAL(18,2)) ELSE 0 END) FROM [BLI$Detailed Vendor Ledg_ Entry] DtldVendLedger
LEFT JOIN [BLI$G_L Entry] GLEntry
ON GLEntry.[Document No_] = (SELECT VendLedger.[Document No_] FROM [BLI$Vendor Ledger Entry] VendLedger
WHERE VendLedger.[Entry No_] = DtldVendLedger.[Vendor Ledger Entry No_])
WHERE DtldVendLedger.[Entry Type] = 2 AND DtldVendLedger.[Initial Document Type] = 2 AND DtldVendLedger.[Document No_] = a.[Document No_]
AND GLEntry.[Source Code] = 'PURCHASES' AND GLEntry.[G_L Account No_] NOT IN ('2003','1402','1403','1401')
) AS [Input Tax],
(
SELECT SUM(CASE WHEN GLEntry.[VAT Amount] = 0 THEN CAST(GLEntry.Amount AS DECIMAL(18,2)) ELSE 0 END) FROM [BLI$Detailed Vendor Ledg_ Entry] DtldVendLedger
LEFT JOIN [BLI$G_L Entry] GLEntry
ON GLEntry.[Document No_] = (SELECT VendLedger.[Document No_] FROM [BLI$Vendor Ledger Entry] VendLedger
WHERE VendLedger.[Entry No_] = DtldVendLedger.[Vendor Ledger Entry No_])
WHERE DtldVendLedger.[Entry Type] = 2 AND DtldVendLedger.[Initial Document Type] = 2 AND DtldVendLedger.[Document No_] = a.[Document No_]
AND GLEntry.[Source Code] = 'PURCHASES' AND GLEntry.[G_L Account No_] NOT IN ('2003','1402','1403','1401')
) AS [Non-Vat]
I want to Sum the Vatable, Input Tax, and Nonvat then pass the value into a variable then that variable will be used in my SELECT statements.
I tried to rearrange your query a bit. In your query the part with the DtldVendLedger query inside the join condition should not be valid. If you got that to run I have no idea how.
Because of the way I think you're summing I don't think it matters whether you used outer joins or inner joins. The rows with null GLEntry count as zeroes anyway.
DECLARE #vatable DECIMAL(18, 2);
DECLARE #inputtax DECIMAL(18, 2);
DECLARE #nonvat DECIMAL(18, 2);
SELECT
#vatable = SUM(
CASE
WHEN GLEntry.[VAT Amount] <> 0
THEN CAST(GLEntry.Amount AS DECIMAL(18,2)) ELSE 0 END
),
#nonvat = SUM(
CASE
WHEN GLEntry.[VAT Amount] = 0
THEN CAST(GLEntry.Amount AS DECIMAL(18,2)) ELSE 0 END
)
FROM
A as a /* I'm assuming there was a FROM referencing a table "a" */
[BLI$Detailed Vendor Ledg_ Entry] DtldVendLedger
INNER JOIN
ON DtldVendLedger.[Document No_] = a.[Document No_]
LEFT OUTER JOIN [BLI$Vendor Ledger Entry] VendLedger
ON VendLedger.[Entry No_] = DtldVendLedger.[Vendor Ledger Entry No_]
LEFT OUTER JOIN [BLI$G_L Entry] GLEntry
ON GLEntry.[Document No_] = VendLedger.[Document No_]
WHERE
a.??? = ???
AND DtldVendLedger.[Entry Type] = 2
AND DtldVendLedger.[Initial Document Type] = 2
AND GLEntry.[Source Code] = 'PURCHASES'
AND GLEntry.[G_L Account No_] NOT IN ('2003','1402','1403','1401');
SET #inputtax = 0.12 * #vatable;

Returning Multiples of SUM when joining two tables

When I join these two tables the sum results are multiples of the number of rows in the tables being queried. For instance, sum(jle.Unit Price) should be $834,485.00. However, it returns a value of $7760710.50. It is summing the lines from that table 31 times, the same number, minus one, of rows in the jpl table. The same for the sum on jpl.[Total Price]. Returning 3 times (for the three rows in jle table) than the result should be. What am I doing wrong. I want to add this to a query that is already working based on the Job No_ field.
select jle.[Job No_], sum(jle.[Unit Price]) as 'Invoiced Amount', sum(jpl.[Total Price])as 'Sale $'
from [Job Ledger Entry] jle join [Job Planning Line] jpl on jle.[Job No_] = jpl.[Job No_]
where jle.[Job No_] = 'j-2397'
group by jle. [Job No_]
Try this.
select jpl.[Job No_], SUM(jle.[Unit Price]) as 'Invoiced Amount', MIN(jpl.[Total Price]) AS 'Sale $'
from [Job Planning Line] jpl INNER JOIN (SELECT DISTINCT [Job No_], [Unit Price] FROM [Job LEdger Entry]) AS jle on jle.[Job No_] = jpl.[Job No_]
where jpl.[Job No_] = 'j-2397'
group by jpl. [Job No_]