Count ticket number in sql - sql-server

What im trying to do is only display records created for a customer that only has more then one ticketnumber in a certain month.
Select name,ticketnumber, title,
description,statename,personname,charge,createdon
from case
Where sum(ticketnumber) > 2 AND createdon >= '2017-01-01' ;
i have tried
select sum(ticketnumber) AS total
and
where sum(ticketnumber) > 2
Where am i going wrong? ticketnumber is a varchar data type
The error im getting;
Operand data type nvarchar is invalid for sum operator.

To limit the check to a single month, not just after some date, you can use dateadd(month, datediff(month, 0, createdon ) , 0) to truncate a date down to the first of a month.
Assuming you are identifying your customer by personname:
using exists():
select
name
, ticketnumber
, title
, description
, statename
, personname
, charge
, createdon
from [case] as c
where dateadd(month, datediff(month, 0, createdon ) , 0) = '20170101'
and exists (
select 1
from [case] as i
where --createdon >= '2017-01-01'
dateadd(month, datediff(month, 0, createdon ) , 0) = '20170101'
and i.personname = c.personname
having count(*) > 1
)
using in()
select
name
, ticketnumber
, title
, description
, statename
, personname
, charge
, createdon
from [case]
where dateadd(month, datediff(month, 0, createdon ) , 0) = '20170101'
and personname in (
select personname
from [case] as i
where --createdon >= '2017-01-01'
dateadd(month, datediff(month, 0, createdon ) , 0) = '20170101'
group by personname
having count(*) > 1
)

I usually just do this
SELECT Total from (select sum(ticketnumber) AS total from case)A
Where Total>2

Related

How to get status of a record on specific day. SQL

I'm trying to create a query to see if a record(Alert) was open during a specific time period.
The first table has only the current status of the record.
table: ALERT
intID |CurrentStateID| DateCreated
-----------------------------------
3 |Closed | 10/11/2009
the second table has the history of the status for the alert.
tblState
intContextID|strToStateName|datTimeStamp
-----------------------------------
3 |Unassigned |10/11/2009
3 |Closed |10/14/2009
Here is my desired output:
DESIRED OUTPUT
DATE |DAY |TOTAL_OPEN
-----------------------------------
10/10/2009 |Friday |0
10/11/2009 |Saturday |1
10/12/2009 |Sunday |1
10/13/2009 |Monday |1
10/14/2009 |Tuesday |0
10/15/2009 |Wednesday |0
I was able to write some of the code but I think its the join on the AllDays table that might be wrong.
DECLARE #StartDate DATETIME = '2009-10-10';
DECLARE #EndDate DATETIME = '2009-10-15 23:59:59';
WITH AllDays
AS (
SELECT #StartDate AS [Date]
, 1 AS [level]
UNION ALL
SELECT DATEADD(DAY, 1, [Date])
, [level] + 1
FROM AllDays
WHERE [Date] < #EndDate
)
SELECT CAST(AllDays.[Date] AS DATE) AS 'DATE'
, datename(dw, AllDays.[Date]) AS 'DAY'
,ISNULL(TOTAL_OPEN, 0) as TOTAL_OPEN
FROM AllDays
LEFT JOIN (
SELECT DISTINCT s.datTimeStamp AS 'DATE'
, count(A.intID) AS 'TOTAL_OPEN'
FROM Alert A
INNER JOIN tblState S ON A.intID = S.intContextID
WHERE strToStateName = 'Unassigned'
GROUP BY datTimeStamp
) AS TOTAL_OPEN ON TOTAL_OPEN.DATE = AllDays.[Date]
The Alert was open from 10-11 to 10-13 but since I'm joining on datetimestamp the results only show 1 for 10/11.
Here's the schema a link!
OK.. one (admittedly hideous) way to do this to create a udf that calculates the open count for a given day like so:
CREATE FUNCTION [dbo].[fn_open_alert_count] (#date date)
RETURNS int
AS
BEGIN
RETURN (SELECT COUNT(intContextID) as opencount FROM tblState WHERE strToStateName = 'Unassigned' AND datTimeStamp <= #date) - (SELECT COUNT(intContextID) as closedcount FROM tblState WHERE strToStateName = 'Closed' AND datTimeStamp <= #date)
END
Then you can do the query like so:
DECLARE #StartDate DATETIME = '2009-10-10';
DECLARE #EndDate DATETIME = '2009-10-15 23:59:59';
WITH AllDays
AS (
SELECT #StartDate AS [Date]
, 1 AS [level]
UNION ALL
SELECT DATEADD(DAY, 1, [Date])
, [level] + 1
FROM AllDays
WHERE [Date] < #EndDate
)
SELECT CAST(AllDays.[Date] AS DATE) AS 'DATE'
, datename(dw, AllDays.[Date]) AS 'DAY'
,[dbo].[fn_open_alert_count](AllDays.[Date])
FROM AllDays
If you don't have permission to create functions you can inline the functionality like so:
DECLARE #StartDate DATETIME = '2009-10-10';
DECLARE #EndDate DATETIME = '2009-10-15 23:59:59';
WITH AllDays
AS (
SELECT #StartDate AS [Date]
, 1 AS [level]
UNION ALL
SELECT DATEADD(DAY, 1, [Date])
, [level] + 1
FROM AllDays
WHERE [Date] < #EndDate
)
SELECT CAST(AllDays.[Date] AS DATE) AS 'DATE'
, datename(dw, AllDays.[Date]) AS 'DAY'
,(SELECT COUNT(intContextID) as opencount FROM tblState WHERE strToStateName = 'Unassigned' AND datTimeStamp <= AllDays.[Date]) - (SELECT COUNT(intContextID) as closedcount FROM tblState WHERE strToStateName = 'Closed' AND datTimeStamp <= AllDays.[Date]) AS TOTAL_OPEN
FROM AllDays
This gives me the following output:
DATE DAY TOTAL_OPEN
2009-10-10 Saturday 0
2009-10-11 Sunday 1
2009-10-12 Monday 1
2009-10-13 Tuesday 1
2009-10-14 Wednesday 0
2009-10-15 Thursday 0
2009-10-16 Friday 0

Getting Error from the following script

I am going to create SI for each record of table, getting error from stored procedure:
No column name was specified for column 6 of 'cte_Alldates'.
Please check this script and let me know where the problem is:
table screenshot
and the stored procedure:
create proc sp_calcualtteSI
as
begin
DECLARE #today datetime
SET #today = dateadd(day,datediff(day,0,current_timestamp),0)
; WITH cte_dates AS
(
SELECT DISTINCT
name, Pamount, Rateofint, cdate,
CASE
WHEN ISNULL(today, #today) < DATEADD(month, 1, DATEADD(month, datediff(month, 0, cdate), 0))
THEN ISNULL(#today, #today)
ELSE
DATEADD(month, 1, dateadd(month, datediff(month, 0, cdate), 0))
END AS MonthEnd,
ISNULL(#today, #today) AS End_date
FROM
tbl_intestcalculate),
cte_Alldates AS
(
SELECT
name, Pamount, Rateofint, cdate, monthEnd, #today
FROM
cte_dates
UNION
SELECT
name, Pamount, Rateofint,
DATEADD(month, number, monthEnd),
CASE
WHEN DATEADD(month, number + 1, monthEnd) < #today
THEN DATEADD(month, number + 1, monthEnd)
ELSE #today
END,
#today
FROM
cte_dates c
CROSS JOIN
(SELECT number
FROM master..spt_values
WHERE type = 'p' AND number BETWEEN 0 AND 11) a
WHERE
dateadd(month, number, monthEnd) < #today
)
SELECT
name, cdate, monthEnd, Pamount, Rateofint,
DATEDIFF(day, cdate, monthEnd) AS No_Of_Days,
ROUND(Pamount * Rateofint * DATEDIFF(day, cdate, monthEnd) / 36500, 2) AS SI
FROM
cte_Alldates
END
A CTE needs to have column names specified for all columns. In this case you are missing a column name for the sixth column. Your cte_Alldates CTE should read:
cte_Alldates AS
(
SELECT
name, Pamount, Rateofint, cdate, monthEnd, [day]=#today
FROM
cte_dates
UNION
SELECT
name, Pamount, Rateofint,
DATEADD(month, number, monthEnd),
CASE
WHEN DATEADD(month, number + 1, monthEnd) < #today
THEN DATEADD(month, number + 1, monthEnd)
ELSE #today
END,
[day]=#today
FROM
cte_dates c
CROSS JOIN
(SELECT number
FROM master..spt_values
WHERE type = 'p' AND number BETWEEN 0 AND 11) a
WHERE
dateadd(month, number, monthEnd) < #today
)

display all the date between two date with data

I got the following code from this forum.It retrieve data from staff attendance from the table like:
userid dateandtime checktype
1. 100 01/01/2015 I
2. 100 01/01/2015 O
3. 102 02/02/2015 I
4. 102 02/02/2015 O
Now I want to retrieve all the data between two specific date but all the date should be retrieved even one absent and these days should show blank or zero in the column of hours
SELECT t.UserID ,
[Date] = DATEADD(dd, 0, DATEDIFF(dd, 0, t.CheckIn)) ,
CheckIn = CONVERT(VARCHAR(10), t.CheckIn, 108) ,
CheckOut = CONVERT(VARCHAR(10), t.CheckOut, 108) ,
[Hours] = CAST(DATEDIFF(MINUTE, t.CheckIn, t.CheckOut) / 60. AS DECIMAL(10,
2))
FROM ( SELECT t.UserID ,
CheckIn = t.Checktime ,
CheckOut = r.Checktime ,
RowNum = ROW_NUMBER() OVER ( PARTITION BY t.UserID,
r.Checktime ORDER BY 1 / 0 )
FROM CHECKINOUT t
OUTER APPLY ( SELECT TOP 1
*
FROM CHECKINOUT t2
WHERE t2.UserID = t.UserID
AND t2.Checktime > t.Checktime
AND DATEADD(dd, 0,
DATEDIFF(dd, 0,
t.Checktime)) = DATEADD(dd,
0,
DATEDIFF(dd, 0,
t2.Checktime))
AND t2.Checktype = 'O'
ORDER BY t2.Checktime
) r
WHERE t.Checktype = 'I'
) t
WHERE t.RowNum = 1

Issue with DateCreated

I have a table dbo.participation:
ID INT NOT NULL IDENTITY(1,1) PRIMARY KEY CLUSTERED,
User VARCHAR(MAX) NOT NULL,
ParticipationLevel TINYINT NOT NULL,
Selector VARCHAR(MAX) NOT NULL,
DateCreated DATETIME NOT NULL
I created the code below but unfortunately it shows bad performance for #DateStart and #DateStop
SELECT
dateadd(month, datediff(month, 0, DateCreated), 0) AS MDate
,COUNT(CASE WHEN ParticipationLevel >= 10 THEN Selector ELSE NULL END) AS ParticipationLevel1
,COUNT(CASE WHEN ParticipationLevel >= 30 THEN Selector ELSE NULL END) AS ParticipationLevel2
FROM
Participation
WHERE
(#DateStart IS NULL OR (#DateStart IS NOT NULL
AND DateCreated >= #DateStart))
AND (#DateEnd IS NULL OR (#DateEnd IS NOT NULL
AND DateCreate < #DateEnd))
GROUP BY
Dateadd(month, datediff(month, 0, DateCreate), 0)
Do you happen to have any ideas how to improve my code or alternatively how to modify the table to improve performance?
You need an index along the following lines
CREATE INDEX ix
ON dbo.Participation(DateCreated)
INCLUDE (ParticipationLevel);
And you should rewrite the query to get rid of the OR and to avoid the unnecessary reference to a column defined as NOT NULL.
(Note a simple COUNT(Selector) would not look up the value as SQL Server recognizes it can't be NULL but wrapping in an expression defeats this logic)
SELECT DATEADD(month, DATEDIFF(month, 0, DateCreated), 0) AS MDate,
COUNT(CASE
WHEN ParticipationLevel >= 10 THEN 1
END) AS ParticipationLevel1,
COUNT(CASE
WHEN ParticipationLevel >= 30 THEN 1
END) AS ParticipationLevel2
FROM Participation
WHERE DateCreated >= ISNULL(#DateStart, '17530101')
AND DateCreated <= ISNULL(#DateEnd, '99991231')
GROUP BY DATEDIFF(month, 0, DateCreated)
This can give a plan with a seek as below
Note that it would be possible to get rid of the sort by processing chunks of the index a month at the time (possibly in a recursive CTE) but this may be overkill.
Code for that could look something like
/*Cheap to find out from the index*/
IF #DateStart IS NULL
SELECT #DateStart = MIN(DateCreated)
FROM dbo.Participation
IF #DateStart IS NULL
SELECT #DateEnd = MAX(DateCreated)
FROM dbo.Participation
/*Adjust to start of month*/
SELECT #DateStart = DATEADD(month, DATEDIFF(month, 0, #DateStart), 0),
#DateEnd = DATEADD(month, 1 + DATEDIFF(month, 0, #DateEnd), 0);
WITH Dates
AS (SELECT #DateStart AS MDate
UNION ALL
SELECT dateadd(MONTH, 1, MDate) AS MDate
FROM Dates
WHERE dateadd (MONTH, 1, MDate) <= #DateEnd)
SELECT D.MDate,
CA.ParticipationLevel1,
CA.ParticipationLevel2
FROM Dates D
CROSS APPLY (SELECT COUNT(CASE
WHEN ParticipationLevel >= 10
THEN 1
END) AS ParticipationLevel1,
COUNT(CASE
WHEN ParticipationLevel >= 30
THEN 1
END) AS ParticipationLevel2
FROM Participation P WITH (INDEX = ix)
WHERE P.DateCreated >= D.MDate
AND P.DateCreated < DATEADD(MONTH, 1, D.MDate)
GROUP BY () /* So no grouping row returned for empty months */
) CA(ParticipationLevel1, ParticipationLevel2)
OPTION (MAXRECURSION 0);
Which gives a plan with repeated seeks and no sorts
Below two checks not needed in your WHERE clause
#DateStart IS NOT NULL AND
#DateEnd IS NOT NULL AND
SELECT dateadd(month, datediff(month, 0, DateCreated), 0) AS MDate
,COUNT(CASE WHEN ParticipationLevel >= 10 THEN Tracking ELSE NULL END) AS ParticipationLevel1
,COUNT(CASE WHEN ParticipationLevel >= 30 THEN Tracking ELSE NULL END) AS ParticipationLevel2
FROM Participation
WHERE (#DateStart IS NULL OR DateCreated >= #DateStart) AND (#DateEnd IS NULL OR DateCreate < #DateEnd)
GROUP BY Dateadd(month, datediff(month, 0, DateCreate), 0)

Creating a derived table in SQL Server

I need some help summing two (or more) alias columns.
I know I need a derived table to do it, but so far I get lost with online tutorials and documentation as their examples are far too simple. they only have one table, two columns, etc.
What could be my best option here:
I need to calculate the sum of two alias columns: 'InFxO' and 'OnTxO' and my code is as follows:
ALTER PROC [dbo].[DIFOTIS]
#Mode as Varchar (5)
AS
Begin
Declare
#StartDate date,
#EndDate date
SET #StartDate=
CASE #Mode
WHEN 'MTD' THEN DATEADD(mm,DATEDIFF(mm,0,GETDATE()),0)
WHEN 'YTD' THEN DATEADD(yy,DATEDIFF(yy,0,GETDATE()),0)
WHEN 'QTD' THEN DATEADD(qq,DATEDIFF(qq,0,GETDATE()),0)
WHEN 'WTD' THEN DATEADD(wk,DATEDIFF(wk,0,GETDATE()),0)
END
Set #EndDate=
CASE #Mode
WHEN 'MTD' THEN DATEADD(mm,DATEDIFF(mm,0,GETDATE())+1,0)
WHEN 'YTD' THEN DATEADD(yy,DATEDIFF(yy,0,GETDATE())+1,0)
WHEN 'QTD' THEN DATEADD(qq,DATEDIFF(qq,0,GETDATE())+1,0)
WHEN 'WTD' THEN DATEADD(wk,DATEDIFF(wk,0,GETDATE())+1,0)
END
Select DATEPART(ISO_WEEK,d.DateOpn) AS 'Week#'
,Clients.CustCateg, Clients.ClntGroup
,d.DocumentCode as 'CORD_DocumentCode'
,CDSPDocs.DocumentCode AS 'DESP_DocumentCode'
,Count(CORDLines.Qnty) AS 'Cord_Lines'
,SUM(CORDLines.Qnty) AS 'CORD_Qty'
,Count(CDSPLines.Qnty) AS 'DESP_Lines'
,Sum(CDSPLines.Qnty) AS 'DESP_Qty'
,CDSPLines.Status, d.Status as d_status
,d.OpenDate, d.DateDue
,CDSPDocs.PostDate AS 'DESP_PostedDate'
,d.DocType, DATEDIFF(day, d.OpenDate, d.DateDue) AS 'Lead times'
--in-full
,CASE WHEN SUM(CORDLines.Qnty) = Sum(CDSPLines.Qnty) THEN '1' ELSE '0' END as InFxO
--On-Time by order according to Despatch SLAs
,CASE WHEN (Clients.ClntGroup IN ('Local Market','Local Market - Pharm','Web Sales - Local','Web Sales - Export', 'Mail Order','Mail Order - Export')) AND (Datediff(day, d.OpenDate, CDSPDocs.PostDate) - (Datediff(Week, d.OpenDate, CDSPDocs.PostDate)*2) <= 2) then '1'
WHEN (Clients.ClntGroup = 'Export Market') AND (Datediff(day, d.OpenDate, CDSPDocs.PostDate) - (Datediff(Week, d.OpenDate, CDSPDocs.PostDate)*2) <= 14) then '1'
WHEN (Clients.ClntGroup = 'Export Market') or Clients.CustCateg = 'UK Transfer' AND (d.DateDue >= CDSPDocs.PostDate) then '1'
ELSE '0'
END as OnTxO
From dbo.Documents AS d INNER JOIN
dbo.Clients ON d.ObjectID = dbo.Clients.ClntID AND Clients.ClientName <> 'Samples - Free / Give-aways' LEFT Outer JOIN
dbo.DocumentsLines AS CORDLines ON d.DocID = CORDLines.DocID AND CORDLines.TrnType = 'L'
LEFT OUTER JOIN
dbo.DocumentsLines AS CDSPLines ON CORDLines.TranID = CDSPLines.SourceID AND CDSPLines.TrnType = 'L' AND (CDSPLines.Status = 'Posted' OR CDSPLines.Status = 'Closed') LEFT OUTER JOIN
dbo.Documents AS CDSPDocs ON CDSPLines.DocID = CDSPDocs.DocID
WHERE (d.DocType IN ('CASW', 'CORD','MORD'))
AND (CORDLines.LneType NOT In ('Fght','MANF','Stor', 'PACK','EXPS'))
AND d.DateOpn >= #StartDate AND d.DateOpn < #EndDate
AND (CORDLines.LneType is not null)
AND (d.DateDue <= Convert(Date, GetDate(), 101))
Group by d.DateOpn
,d.DocumentCode
,Clients.CustCateg
,CDSPDocs.DocumentCode
,d.[Status]
,d.DocType
,d.OpenDate
,d.DateReq
,CDSPDocs.PostDate
,CDSPLines.[Status]
,Clients.ClntGroup
,d.DocumentName
,d.DateDue
,d.DateOpn
ORDER BY d.DateOpn, 'Week#'
END
GO
All help is appreciated.
Thanks
H
Below is an example that wraps the original query in a derived table so that you don't need to repeat the CASE expressions for the SUM. You could similarly wrap the query in a common table expression to achieve the same result.
I suggest you use single-quotes only to enclose string literals and enclose identifiers (column names, aliases, and object names) with either square brackets or double quotes as described in the SQL Server Books Online reference (http://msdn.microsoft.com/en-us/library/ms175874.aspx). Identifiers need be enclosed only when they don't conform to identifier naming rules or are a reserved keyword.
ALTER PROC dbo.DIFOTIS #Mode AS varchar(5)
AS
BEGIN
DECLARE #StartDate date
, #EndDate date;
SET #StartDate = CASE #Mode
WHEN 'MTD'
THEN DATEADD(mm, DATEDIFF(mm, 0, GETDATE()), 0)
WHEN 'YTD'
THEN DATEADD(yy, DATEDIFF(yy, 0, GETDATE()), 0)
WHEN 'QTD'
THEN DATEADD(qq, DATEDIFF(qq, 0, GETDATE()), 0)
WHEN 'WTD'
THEN DATEADD(wk, DATEDIFF(wk, 0, GETDATE()), 0)
END;
SET #EndDate = CASE #Mode
WHEN 'MTD'
THEN DATEADD(mm, DATEDIFF(mm, 0, GETDATE()) + 1, 0)
WHEN 'YTD'
THEN DATEADD(yy, DATEDIFF(yy, 0, GETDATE()) + 1, 0)
WHEN 'QTD'
THEN DATEADD(qq, DATEDIFF(qq, 0, GETDATE()) + 1, 0)
WHEN 'WTD'
THEN DATEADD(wk, DATEDIFF(wk, 0, GETDATE()) + 1, 0)
END;
SELECT Week#
, CustCateg
, ClntGroup
, CORD_DocumentCode
, DESP_DocumentCode
, Cord_Lines
, CORD_Qty
, DESP_Lines
, DESP_Qty
, Status
, d_status
, OpenDate
, DateDue
, DESP_PostedDate
, DocType
, [Lead times]
, InFxO
, OnTxO
, InFxO + OnTxO AS InFullAndOneTime
FROM (
SELECT DATEPART(ISO_WEEK, d.DateOpn) AS Week#
, Clients.CustCateg
, Clients.ClntGroup
, d.DocumentCode AS CORD_DocumentCode
, CDSPDocs.DocumentCode AS DESP_DocumentCode
, COUNT(CORDLines.Qnty) AS Cord_Lines
, SUM(CORDLines.Qnty) AS CORD_Qty
, COUNT(CDSPLines.Qnty) AS DESP_Lines
, SUM(CDSPLines.Qnty) AS DESP_Qty
, CDSPLines.Status
, d.Status AS d_status
, d.OpenDate
, d.DateDue
, CDSPDocs.PostDate AS DESP_PostedDate
, d.DocType
, DATEDIFF(DAY, d.OpenDate, d.DateDue) AS [Lead times]
--in-full
, CASE WHEN SUM(CORDLines.Qnty) = SUM(CDSPLines.Qnty) THEN 1
ELSE 0
END AS InFxO
--On-Time by order according to Despatch SLAs
, CASE WHEN ( Clients.ClntGroup IN ( 'Local Market',
'Local Market - Pharm',
'Web Sales - Local',
'Web Sales - Export',
'Mail Order',
'Mail Order - Export' ) )
AND ( DATEDIFF(DAY, d.OpenDate, CDSPDocs.PostDate)
- ( DATEDIFF(WEEK, d.OpenDate,
CDSPDocs.PostDate) * 2 ) <= 2 )
THEN 1
WHEN ( Clients.ClntGroup = 'Export Market' )
AND ( DATEDIFF(DAY, d.OpenDate, CDSPDocs.PostDate)
- ( DATEDIFF(WEEK, d.OpenDate,
CDSPDocs.PostDate) * 2 ) <= 14 )
THEN 1
WHEN ( Clients.ClntGroup = 'Export Market' )
OR Clients.CustCateg = 'UK Transfer'
AND ( d.DateDue >= CDSPDocs.PostDate ) THEN '1'
ELSE 0
END AS OnTxO
FROM dbo.Documents AS d
INNER JOIN dbo.Clients ON d.ObjectID = dbo.Clients.ClntID
AND Clients.ClientName <> 'Samples - Free / Give-aways'
LEFT OUTER JOIN dbo.DocumentsLines AS CORDLines ON d.DocID = CORDLines.DocID
AND CORDLines.TrnType = 'L'
LEFT OUTER JOIN dbo.DocumentsLines AS CDSPLines ON CORDLines.TranID = CDSPLines.SourceID
AND CDSPLines.TrnType = 'L'
AND ( CDSPLines.Status = 'Posted'
OR CDSPLines.Status = 'Closed'
)
LEFT OUTER JOIN dbo.Documents AS CDSPDocs ON CDSPLines.DocID = CDSPDocs.DocID
WHERE ( d.DocType IN ( 'CASW', 'CORD', 'MORD' ) )
AND ( CORDLines.LneType NOT IN ( 'Fght', 'MANF', 'Stor',
'PACK', 'EXPS' ) )
AND d.DateOpn >= #StartDate
AND d.DateOpn < #EndDate
AND ( CORDLines.LneType IS NOT NULL )
AND ( d.DateDue <= CONVERT(date, GETDATE(), 101) )
GROUP BY d.DateOpn
, d.DocumentCode
, Clients.CustCateg
, CDSPDocs.DocumentCode
, d.Status
, d.DocType
, d.OpenDate
, d.DateReq
, CDSPDocs.PostDate
, CDSPLines.Status
, Clients.ClntGroup
, d.DocumentName
, d.DateDue
, d.DateOpn
) AS derived_table
ORDER BY d.DateOpn
, Week#;
END;
GO

Resources