Using a range with GETDATE & BETWEEN - sql-server

I'm fairly new to T-SQL and am still learning so please bear with me on this one...I've tried several ways to do this with no luck. I have my 'current' and 90+ days but it's the 30 and 60 days that I'm struggling with. Why can't I use the 'between' clause?
,[CURRENT]=(SELECT sum (cle.[Amount])
FROM [NISNAV].[dbo].[NIS$Cust_ Ledger Entry] cl3 left outer join
[NISNAV].[dbo].[NIS$Detailed Cust_ Ledg_ Entry] cle on cl3.[entry no_]=cle.[cust_ ledger entry no_]
where C.[NO_]=CL3.[CUSTOMER NO_] AND cl3.[open]='1' AND cl3.[Due Date]>getdate()-30)
,[30 DAYS]=(SELECT sum (cle.[Amount])
FROM [NISNAV].[dbo].[NIS$Cust_ Ledger Entry] cl3 left outer join
[NISNAV].[dbo].[NIS$Detailed Cust_ Ledg_ Entry] cle on cl3.[entry no_]=cle.[cust_ ledger entry no_]
where C.[NO_]=CL3.[CUSTOMER NO_] AND cl3.[open]='1' AND cl3.[Due Date]BETWEEN GETDATE()-31 AND getdate()-59)
,[60 DAYS]=(SELECT sum (cle.[Amount])
FROM [NISNAV].[dbo].[NIS$Cust_ Ledger Entry] cl3 left outer join
[NISNAV].[dbo].[NIS$Detailed Cust_ Ledg_ Entry] cle on cl3.[entry no_]=cle.[cust_ ledger entry no_]
where C.[NO_]=CL3.[CUSTOMER NO_] AND cl3.[open]='1' AND cl3.[Due Date] BETWEEN GETDATE()-60 AND getdate()-89)
,[90 + DAYS]=((SELECT sum (cle.[Amount])
FROM [NISNAV].[dbo].[NIS$Cust_ Ledger Entry] cl3 left outer join
[NISNAV].[dbo].[NIS$Detailed Cust_ Ledg_ Entry] cle on cl3.[entry no_]=cle.[cust_ ledger entry no_]
where C.[NO_]=CL3.[CUSTOMER NO_] AND cl3.[open]='1' AND cl3.[Due Date]<=getdate()-90 ))
I know there are entries for that particular customer in both of those buckets.
suggestions?

When filtering a range of dates, BETWEEN expects the earlier date to come before the later date.
You need to reverse the sides of the BETWEEN so that the older date (with highest -value) comes first:
,[30 DAYS]=(SELECT sum (cle.[Amount])
FROM [NISNAV].[dbo].[NIS$Cust_ Ledger Entry] cl3 left outer join
[NISNAV].[dbo].[NIS$Detailed Cust_ Ledg_ Entry] cle on cl3.[entry no_]=cle.[cust_ ledger entry no_]
where C.[NO_]=CL3.[CUSTOMER NO_] AND cl3.[open]='1' AND cl3.[Due Date]BETWEEN GETDATE()-59 AND getdate()-31)
,[60 DAYS]=(SELECT sum (cle.[Amount])
FROM [NISNAV].[dbo].[NIS$Cust_ Ledger Entry] cl3 left outer join
[NISNAV].[dbo].[NIS$Detailed Cust_ Ledg_ Entry] cle on cl3.[entry no_]=cle.[cust_ ledger entry no_]
where C.[NO_]=CL3.[CUSTOMER NO_] AND cl3.[open]='1' AND cl3.[Due Date] BETWEEN GETDATE()-89 AND getdate()-60)

Instead of four separate subqueries, you can use one query with CASE statements
SELECT
[CURRENT] = SUM(CASE WHEN cl3.[Due Date] >= DATEADD(day,-30,GETDATE())
THEN cle.[Amount]
END)
,[30 DAYS] = SUM(CASE WHEN cl3.[Due Date] < DATEADD(day,-30,GETDATE())
AND cl3.[Due Date] >= DATEADD(day,-60,GETDATE())
THEN cle.[Amount]
END)
,[60 DAYS] = SUM(CASE WHEN cl3.[Due Date] < DATEADD(day,-60,GETDATE())
AND cl3.[Due Date] >= DATEADD(day,-90,GETDATE())
THEN cle.[Amount]
END)
,[90 DAYS] = SUM(CASE WHEN cl3.[Due Date] < DATEADD(day,-90,GETDATE())
THEN cle.[Amount]
END)
FROM [NISNAV].[dbo].[NIS$Cust_ Ledger Entry] cl3
LEFT OUTER JOIN [NISNAV].[dbo].[NIS$Detailed Cust_ Ledg_ Entry] cle
ON( cl3.[entry no_]=cle.[cust_ ledger entry no_])
WHERE C.[NO_]=CL3.[CUSTOMER NO_]
AND cl3.[open]='1'

Related

SQL Query - SUM and JOIN multiple tables but return missing record

SQL Query - SUM and JOIN multiple tables but return missing record;
Good day!
here's the table lists;
Barcodes Table
Item Ledger Entry Table
Trans_ Sales Entry Table
Transfer Line Table
Note: all table has a relation Store No/Location, Item No and Variant Code
Need help.
when i JOIN and SUM the Trans_ Sales Entry Table and Transfer Line Table , but the query result return some missing record
screenshot: missing record.jpg
Thank you!
here's my sql query code;
;WITH Barcodes AS
(
SELECT
[BBI$Barcodes].[Item No_]
,[BBI$Barcodes].[Description]
,[BBI$Barcodes].[Variant Code]
FROM [BBI$Barcodes]
),
ILEtransfer AS
(
SELECT
[BBI$Item Ledger Entry].[Entry Type]
,[BBI$Item Ledger Entry].[Location Code] AS [Location]
,[BBI$Item Ledger Entry].[Item No_]
,MAX([BBI$Item Ledger Entry].[Description]) AS [Description]
,[BBI$Item Ledger Entry].[Variant Code]
,SUM([BBI$Item Ledger Entry].[Quantity]) AS [PDel]
FROM [BBI$Item Ledger Entry]
WHERE
[BBI$Item Ledger Entry].[Location Code]='HPGW'
AND [BBI$Item Ledger Entry].[Entry Type] = '4'
GROUP BY
[BBI$Item Ledger Entry].[Location Code]
,[BBI$Item Ledger Entry].[Entry Type]
,[BBI$Item Ledger Entry].[Item No_]
,[BBI$Item Ledger Entry].[Variant Code]
),
ILEmsales AS
(
SELECT
[BBI$Item Ledger Entry].[Entry Type]
,[BBI$Item Ledger Entry].[Location Code] AS [Location]
,[BBI$Item Ledger Entry].[Item No_]
,MAX([BBI$Item Ledger Entry].[Description]) AS [Description]
,[BBI$Item Ledger Entry].[Variant Code]
,SUM([BBI$Item Ledger Entry].[Quantity]) AS [MSales]
FROM [BBI$Item Ledger Entry]
WHERE
[BBI$Item Ledger Entry].[Location Code]='HPGW'
AND [BBI$Item Ledger Entry].[Entry Type] = '1'
AND [BBI$Item Ledger Entry].[Document No_] NOT LIKE 'HP%'
GROUP BY
[BBI$Item Ledger Entry].[Location Code]
,[BBI$Item Ledger Entry].[Entry Type]
,[BBI$Item Ledger Entry].[Item No_]
,[BBI$Item Ledger Entry].[Variant Code]
),
ILEpadj AS
(
SELECT
[BBI$Item Ledger Entry].[Entry Type]
,[BBI$Item Ledger Entry].[Location Code] AS [Location]
,[BBI$Item Ledger Entry].[Item No_]
,MAX([BBI$Item Ledger Entry].[Description]) AS [Description]
,[BBI$Item Ledger Entry].[Variant Code]
,SUM([BBI$Item Ledger Entry].[Quantity]) AS [PAdj]
FROM [BBI$Item Ledger Entry]
WHERE
[BBI$Item Ledger Entry].[Location Code]='HPGW'
AND [BBI$Item Ledger Entry].[Entry Type] = '2'
GROUP BY
[BBI$Item Ledger Entry].[Location Code]
,[BBI$Item Ledger Entry].[Entry Type]
,[BBI$Item Ledger Entry].[Item No_]
,[BBI$Item Ledger Entry].[Variant Code]
),
ILEnadj AS
(
SELECT
[BBI$Item Ledger Entry].[Entry Type]
,[BBI$Item Ledger Entry].[Location Code] AS [Location]
,[BBI$Item Ledger Entry].[Item No_]
,MAX([BBI$Item Ledger Entry].[Description]) AS [Description]
,[BBI$Item Ledger Entry].[Variant Code]
,SUM([BBI$Item Ledger Entry].[Quantity]) AS [NAdj]
FROM [BBI$Item Ledger Entry]
WHERE
[BBI$Item Ledger Entry].[Location Code]='HPGW'
AND [BBI$Item Ledger Entry].[Entry Type] = '3'
GROUP BY
[BBI$Item Ledger Entry].[Location Code]
,[BBI$Item Ledger Entry].[Entry Type]
,[BBI$Item Ledger Entry].[Item No_]
,[BBI$Item Ledger Entry].[Variant Code]
),
Transfeline AS
(
SELECT
[BBI$Transfer Line].[Transfer-to Code]
,[BBI$Transfer Line].[Item No_]
,MAX([BBI$Transfer Line].[Description]) AS [Description]
,MAX([BBI$Transfer Line].[Description 2]) AS [Description 2]
,[BBI$Transfer Line].[Variant Code]
,SUM([BBI$Transfer Line].[Quantity]) AS [UDel]
FROM [BBI$Transfer Line]
WHERE
[BBI$Transfer Line].[Transfer-to Code] = 'HPGW'
GROUP BY
[BBI$Transfer Line].[Transfer-to Code]
,[BBI$Transfer Line].[Item No_]
,[BBI$Transfer Line].[Variant Code]
),
TSEpsales AS
(
SELECT
[BBI$Trans_ Sales Entry].[Store No_]
,[BBI$Trans_ Sales Entry].[Item No_]
,[BBI$Trans_ Sales Entry].[Variant Code]
,SUM([BBI$Trans_ Sales Entry].[Quantity]) AS [PSales]
FROM [BBI$Trans_ Sales Entry]
WHERE
[BBI$Trans_ Sales Entry].[Store No_]='HPGW'
AND [BBI$Trans_ Sales Entry].[Transaction No_] NOT IN (5271,5272,5273,5278,5279,5280,5281,5282,5283,5284,5285,5286,8530,8531,8532,8533,8534,8535,8536,8537,13133,13849)
GROUP BY
[BBI$Trans_ Sales Entry].[Store No_]
,[BBI$Trans_ Sales Entry].[Item No_]
,[BBI$Trans_ Sales Entry].[Variant Code]
)
SELECT DISTINCT
BAR.[Item No_] AS [Item No_]
,BAR.[Description] AS [Description]
,BAR.[Variant Code] AS [Variant Code]
,ISNULL(ILETR.[PDel],0) AS [PDel]
,ISNULL(ILEMS.[MSales],0) AS [MSales]
,ISNULL(ILEPA.[PAdj],0) AS [PAdj]
,ISNULL(ILENA.[NAdj],0) AS [NAdj]
,ISNULL(TL.[UDel],0) AS [UDel]
,ISNULL(TSEPS.[PSales],0) AS [PSales]
,ISNULL(ILETR.[PDel],0)+ISNULL(ILEPA.[PAdj],0)+ISNULL(TL.[UDel],0)+ISNULL(ILEMS.[MSales],0)+ISNULL(ILENA.[NAdj],0)+ISNULL(TSEPS.[PSales],0) AS Total
FROM [BBI$Barcodes] BAR
LEFT JOIN [ILEtransfer] ILETR
ON ILETR.[Item No_]=BAR.[Item No_]
AND ILETR.[Variant Code]=BAR.[Variant Code]
LEFT JOIN [ILEmsales] ILEMS
ON ILEMS.[Item No_]=BAR.[Item No_]
AND ILEMS.[Variant Code]=BAR.[Variant Code]
LEFT JOIN [ILEpadj] ILEPA
ON ILEPA.[Item No_]=BAR.[Item No_]
AND ILEPA.[Variant Code]=BAR.[Variant Code]
LEFT JOIN [ILEnadj] ILENA
ON ILENA.[Item No_]=BAR.[Item No_]
AND ILENA.[Variant Code]=BAR.[Variant Code]
FULL JOIN [Transfeline] TL
ON TL.[Item No_]=BAR.[Item No_]
AND TL.[Variant Code]=BAR.[Variant Code]
FULL JOIN [TSEpsales] TSEPS
ON TSEPS.[Item No_]=BAR.[Item No_]
AND TSEPS.[Variant Code]=BAR.[Variant Code]
WHERE
ILETR.[Location]='HPGW'
AND ILETR.[Entry Type]='4'
OR ILEMS.[Entry Type]='1'
OR ILEPA.[Entry Type]='2'
OR ILENA.[Entry Type]='3'

I want to convert inner join Query in SubQuery

I am using inner join in query but I want Subquery instead of join
select [Document No_] as DocumentNo,
ledger.[Posting Date] as Date,
[Sales (LCY)] as Amount,
header.[Customer Reference] as PONo
from [Uneek Clothing Company Ltd$Cust_ Ledger Entry] ledger
inner join [Uneek Clothing Company Ltd$Sales Invoice Header] header on ledger.[Entry No_]=header.[Cust_ Ledger Entry No_]
where [Customer No_] = 'DRC01'
and [Document Type]=2
and [Sales (LCY)]!=0
and ledger.[Posting Date] between '01/01/2019' and '12/31/2019'
order by [Sales (LCY)] asc offset 0 ROWS FETCH NEXT 20 ROWS ONLY
[Entry No_] is foreign key in [Uneek Clothing Company Ltd$Sales Invoice Header] with name [Cust_ Ledger Entry No_]
if you really want to drop the join and use a subquery instead,
it will look something like this.
Since you did not use an table alias for all columns, I had to assume that no other column of table in the subquery is used anywhere else. If that is not the case then this will not work
It will also only work if the subquery returns no more then 1 row, otherwise a subquery cannot be used at all
select [Document No_] as DocumentNo,
ledger.[Posting Date] as Date,
[Sales (LCY)] as Amount,
( select header.[Customer Reference]
from [Uneek Clothing Company Ltd$Sales Invoice Header] header
where ledger.[Entry No_] = header.[Cust_ Ledger Entry No_]
) as PONo
from [Uneek Clothing Company Ltd$Cust_ Ledger Entry] ledger
where [Customer No_] = 'DRC01'
and [Document Type]=2
and [Sales (LCY)]!=0
and ledger.[Posting Date] between '01/01/2019' and '12/31/2019'
order by [Sales (LCY)] asc
offset 0 ROWS FETCH NEXT 20 ROWS ONLY

Scheduled datetime conversion error (SQL)

When trying to convert a datetime field to get rid of NULL's I am getting the error that the datetime cannot be converted.
This query is populating another database where the column datatype is date.
This job runs perfectly fine when not scheduled, but results in an error during the scheduled insert job.
Any ideas?
, (SELECT
CASE
WHEN MIN([Discount Ledger].[Posting Date]) IS NOT NULL
THEN CONVERT(VARCHAR(50), MIN([Discount Ledger].[Posting Date]), 121)
ELSE '-'
END
FROM
[Wings$Discount Ledger Entry] AS "Discount Ledger"
LEFT JOIN
[Wings$Periodic Discount] AS "MixMatchHeader" ON [Discount Ledger].[Offer No_] = [MixMatchHeader].[No_]
LEFT JOIN
[Wings$Periodic Discount Line] AS "MixMatch" ON [MixMatchHeader].[No_] = [MixMatch].[Offer No_]
LEFT JOIN
[Wings$Item_Special Group Link] AS "Special Group" ON [MixMatch].[No_] = [Special Group].[Special Group Code]
LEFT JOIN
[Wings$Store Price Group] AS "Store Price Group" ON [MixMatch].[Price Group] = [Store Price Group].[Price Group Code]
WHERE
[MixMatch].[No_] = [Item Ledger].[Item No_]
AND [MixMatch].[Variant Code] = [Item Ledger].[Variant Code]
AND [Store Price Group].[Store] = [Item Ledger].[Location Code]) AS "Discount Start Date"
You can do this using ISNULL. Since you are looking for the MIN date, I will use GETDATE() as the argument/replacement value for NULL. Unless your data has future dates, then GETDATE() should function as the MAX value. Unless you want the CASE statement to evaluate the NULL values as the MIN. In that case, you can use an empty string '' or '1900-01-01' instead of GETDATE().
, (SELECT
CASE
WHEN
MIN([Discount Ledger].[Posting Date]) IS NOT NULL
THEN CONVERT(varchar(50),MIN(ISNULL([Discount Ledger].[Posting Date],GETDATE())),121)
ELSE '-'
END
FROM [Wings$Discount Ledger Entry] AS "Discount Ledger"
LEFT JOIN [Wings$Periodic Discount] AS "MixMatchHeader" ON [Discount Ledger].[Offer No_] = [MixMatchHeader].[No_]
LEFT JOIN [Wings$Periodic Discount Line] AS "MixMatch" ON [MixMatchHeader].[No_] = [MixMatch].[Offer No_]
LEFT JOIN [Wings$Item_Special Group Link] AS "Special Group" ON [MixMatch].[No_] = [Special Group].[Special Group Code]
LEFT JOIN [Wings$Store Price Group] AS "Store Price Group" ON [MixMatch].[Price Group] = [Store Price Group].[Price Group Code]
WHERE [MixMatch].[No_] = [Item Ledger].[Item No_] AND [MixMatch].[Variant Code] = [Item Ledger].[Variant Code] AND [Store Price Group].[Store] = [Item Ledger].[Location Code])
AS "Discount Start Date"

T-SQL - WHERE condition performance [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I have variable declared like this
DECLARE #Customer_No VARCHAR(MAX)
SET #Customer_No = 'Z110073574;Z110027464;Z110229752;Z110274156;Z110170566;Z110102837;Z110074199'
The query contains condition
WHERE ((#Customer_No = '') OR ([Customer No_] IN (SELECT [No_] FROM Customer)))
If the variable #Customer_No can contains blank value (''), filtering should be skipped. In case the variable contains customers separated by semicolons, the string is parsed into table Customer and the query is filtered by values in Customer table.
The query takes 15 minutes to run. If I use just the second condition, it takes about 50 seconds.
WHERE [Customer No_] IN (SELECT [No_] FROM Customer)
I don't get it. How is this possible? Any solution? Explanation?
Thank you.
Edit: Query
SELECT DET.[Cust_ Ledger Entry No_]
, DET.[Amount]
, DET.[Entry Type]
, CLE.[Document No_]
FROM [dbo].[Company$Cust_ Ledger Entry] CLE
LEFT JOIN [dbo].[Company$Detailed Cust_ Ledg_ Entry] DET ON CLE.[Entry No_] = DET.[Cust_ Ledger Entry No_]
WHERE ((#Customer_No = '') OR (DET.[Customer No_] IN (SELECT [No_] FROM Customer)))
AND (DET.[Entry Type] = 1 OR DET.[Entry Type] = 2)
AND (CLE.[Due Date] >= #FromDueDate AND CLE.[Due Date] <= #ToDueDate)
AND CLE.[Posting Date] <= #ToDate
I think you can simply the query without loss of integrity to the following. Like Aaron said, can you post a query plan?
SELECT
DET.[Cust_ Ledger Entry No_],
DET.[Amount],
DET.[Entry Type],
CLE.[Document No_]
FROM
[dbo].[Company$Cust_ Ledger Entry] CLE
LEFT JOIN
[dbo].[Company$Detailed Cust_ Ledg_ Entry] DET
ON
CLE.[Entry No_] = DET.[Cust_ Ledger Entry No_]
WHERE
( #FAS_No = '' OR DET.[Customer No_] IN (SELECT [No_] FROM Customer) )
AND DET.[Entry Type] in (1, 2)
AND CLE.[Due Date] >= #FromDueDate
AND CLE.[Due Date] <= #ToDueDate
AND CLE.[Posting Date] <= #ToDate
It's not easy to pinpoint the exact problem without more information (table structure, # of rows, etc). However, one thing for sure is the the use of OR in TSQL reduces overall performance. If there is a way to use an AND instead of an OR than it's recommended you do so.
what you can always do it use an IF statement that check if the variable is empty. If not, you use the SELECT with the WHERE. If it is empty, you use the SELECT without the WHERE.
Another thing to check might be your indexes. That can speed things a little.
The DET in the where is killing the left so might as well join
If you pull conditions into the join then the query optimizer seems to do a better
I think this gives a the query optimizer a better chance
SELECT DET.[Cust_ Ledger Entry No_]
, DET.[Amount]
, DET.[Entry Type]
, CLE.[Document No_]
FROM [dbo].[Company$Cust_ Ledger Entry] CLE
JOIN [dbo].[Company$Detailed Cust_ Ledg_ Entry] DET
ON CLE.[Entry No_] = DET.[Cust_ Ledger Entry No_]
AND ( DET.[Entry Type] IN (1,2) )
AND ( CLE.[Due Date] >= #FromDueDate AND CLE.[Due Date] <= #ToDueDate )
AND CLE.[Posting Date] <= #ToDate
JOIN Customer
ON #Customer_No = ''
OR DET.[Customer No_] = Customer.[No_]
And try hints
I would start with INNER MERGE JOIN Customer
And please [NO_] is declared as a PK
I think you can reduce the performance by writing in
USE CTE for - (SELECT [No_] FROM Customer)
Join cte with the main table which will give you performance

How to return NULL value is no records are found in multiple join conditions

I'm creating a report where i need to show all Purchase Indents as per filters and corresponding purchase lines of these indent lines.
If there is no purchase line found as per my condition then it should return NULL value. But report is not showing Indents for which there are no records in purchase lines.
I'm joining 4 tables - INH,INL,PL,PH. The Query is shown below.
SELECT
INH.No_
,INH.[Approved Date]
,INH.Indentor
,INL.No_ AS ItemCode
,INL.description
,INL.Description2
,INL.Req_Quantity
,INL.[Unit of Measure]
,PL.[Document No_]
,PH.[Order Date]
,PL.Quantity AS OrderedQuantity
,PL.[Quantity Received]
FROM [ICTL | HYDERABAD$Indent Header] AS INH
INNER JOIN [ICTL | HYDERABAD$Indent Line] AS INL ON INH.No_ = INL.[Document No_]
LEFT OUTER JOIN [ICTL | HYDERABAD$Purchase Line] AS PL ON INL.[Document No_] = PL.[Indent No_] AND INL.[Line No_] = PL.[Indent Line No_]
LEFT OUTER JOIN [ICTL | HYDERABAD$Purchase Header] AS PH ON PL.[Document No_] = PH.No_
WHERE (UPPER(INH.Indentor) = UPPER(#Name)
OR #Name IS NULL)
AND (INL.No_ <> '')
AND (INH.[Approved Date] >= #StartDate
OR #StartDate IS NULL)
AND (INH.[Approved Date] <= #EndDate
OR #EndDate IS NULL)
AND (PL.[Document Type] = 1)
ORDER BY ItemCode
The problem is that you are have PL.[Document Type] = 1 in your query. This will automatically exclude all rows where there is no PL record (your OUTER JOIN does not matter then).
Either you'd have to do PL.[Document Type] IS NULL OR PL.[Document Type] = 1 in the WHERE clause, or you'd have to add the PL.[Document Type] = 1 to the JOIN condition of the PL table.
Possible this be helpful for you -
SELECT
INH.No_
, INH.[Approved Date]
, INH.Indentor
, INL.No_ AS ItemCode
, INL.description
, INL.Description2
, INL.Req_Quantity
, INL.[Unit of Measure]
, PL.[Document No_]
, PH.[Order Date]
, PL.Quantity AS OrderedQuantity
, PL.[Quantity Received]
FROM [ICTL | HYDERABAD$Indent Header] INH
JOIN [ICTL | HYDERABAD$Indent Line] INL ON INH.No_ = INL.[Document No_]
LEFT JOIN [ICTL | HYDERABAD$Purchase Line] PL ON INL.[Document No_] = PL.[Indent No_] AND INL.[Line No_] = PL.[Indent Line No_]
LEFT JOIN [ICTL | HYDERABAD$Purchase Header] PH ON PL.[Document No_] = PH.No_
WHERE UPPER(INH.Indentor) = UPPER(ISNULL(#Name, INH.Indentor))
AND INL.No_ <> ''
AND INH.[Approved Date] BETWEEN ISNULL(#StartDate, '19000101') AND ISNULL(#EndDate, '30000101')
AND ISNULL(PL.[Document Type], 1) = 1
ORDER BY ItemCode

Resources