Declaring variables in CTEs SQL Server 2008 - sql

How can I implement something like the following:
declare #myInt int
set #myInt=(select count(*) from x)
;with x as
(
select row_number() over(partition by c.patientid order by c.admissiondate) as rn
,c.patientid,c.admissiondate
,max(c.claimsfromdate) as maxHemiDate
,min(c.claimsfromdate) minHemiDate
,(
select max(c2.claimsfromdate)
from claims as c2
where c2.patientid=c.patientid
group by c2.patientid
) as maxClaimsDate
,p.drgCode
,datediff(dd,min(c.claimsfromdate),max(c.claimsfromdate)) /7 as weeksWithHemi
from claims as c inner join icdclaims as ci on ci.id=c.id
inner join tblicd as t on t.icd_id=ci.icd_id
inner join patient as p on p.patientid=c.patientid
and p.admissiondate = c.admissiondate
and p.dischargedate = c.dischargedate
where t.icdText like '%X%' and p.statecode='21'
group by c.patientid, c.admissiondate, p.drgCode
)
select p.patientid, count(*)
from patient as p
left join x on x.patientid=p.patientid
where x.patientid is null
group by p.patientid
The error thrown when this is executed is
invalid object name x
I kinda figured that this would happen since the variable declaration is outside of the CTE. If I move the declaration inside of the parentheses of WITH I get another error.
How can I assign a variable like this inside a CTE? Or can you not use a variable that draws data from the CTE at all?

If you are using SQL Server 2005 or later, you can put the total on each row:
select t.*, cnt, count(*) over () as NumRows
from (select p.patientid, count(*) as cnt
from patient as p left join
x
on x.patientid=p.patientid
where x.patientid is null
group by p.patientid
) t
In case you are using the total to calculate percentages, or something like that, it might be convenient to have the value on each row.

To get in #myInt number of rows, you can do:
declare #myInt int
;with x as
(
__your query__
)
select p.patientid, count(*)
from patient as p
left join x on x.patientid=p.patientid
where x.patientid is null
group by p.patientid
set #myInt=##ROWCOUNT
EDITED (Due Jon Egerton comment) If you need to count x rows, then a temporay table is the way:
declare #myInt int
;with x as
(
__your query__
)
select *
into #tmp_x
from x;
set #myInt=(select count(*) from #tmp_x)
select p.patientid, count(*)
from patient as p
left join #tmp_x x on x.patientid=p.patientid
where x.patientid is null
group by p.patientid
Thanks Jon Egerton

You can't use a single CTE for two distinct statements - they go out of scope.
So either you need to remove the need for the variable, or you can just declare a table variable and then keep selecting on that as many times as you like.
You can declare table var as follows:
declare #x table (rowno int, patientid varchar...
Then you can fill that using the select that you've currently got in your CTE.

The problem is with this line:
set #myInt=(select count(*) from x)
You are trying to do the select on x before it has been declared!
UPDATE
In this case it would be best to use a temporary table or table variable rather than a CTE. For example:
declare #myInt int
select row_number() over(partition by c.patientid order by c.admissiondate) as rn
,c.patientid,c.admissiondate
,max(c.claimsfromdate) as maxHemiDate
,min(c.claimsfromdate) minHemiDate
,(
select max(c2.claimsfromdate)
from claims as c2
where c2.patientid=c.patientid
group by c2.patientid
) as maxClaimsDate
,p.drgCode
,datediff(dd,min(c.claimsfromdate),max(c.claimsfromdate)) /7 as weeksWithHemi
INTO #XTable
from claims as c inner join icdclaims as ci on ci.id=c.id
inner join tblicd as t on t.icd_id=ci.icd_id
inner join patient as p on p.patientid=c.patientid
and p.admissiondate = c.admissiondate
and p.dischargedate = c.dischargedate
where t.icdText like '%X%' and p.statecode='21'
group by c.patientid, c.admissiondate, p.drgCode
set #myInt=(select count(*) from #XTable)
select p.patientid, count(*)
from patient as p
left join #XTable x on x.patientid=p.patientid
where x.patientid is null
group by p.patientid
This is the quick and dirty method, but you could obviously declare your table earlier in the script.

A CTE may be plural. It doesn't solve your stated problem, but it is often clearer to break a complex CTE into several steps, each building on the prior definitions. In this example there is a recursive CTE and a second CTE that computes statistics based on the first. The final SELECT combines the results:
with CTE
as (
select 1 as Number
union all
select Number + 1
from CTE
where Number < 10
),
Summary
as (
select Count( 42 ) as HowMany, Min( Number ) as Least, Max( Number ) as Most
from CTE
)
select *
from CTE cross join
Summary

Related

CTE values need to insert into another table

Below query i am not able to insert common table expression select values into another table.
;WITH cte AS
(
SELECT
rd.reorderhistoryId,
rd.dateCreated,
rd.personId,
rd.reorderId,
rd.statusNo,
rd.createdBy,
r.OrganizationId
FROM
(SELECT
MIN(reorderhistoryId) AS reorderhistoryId,
MIN(personId) AS personId,
reorderId
FROM
reorderHistoryDetails
GROUP BY
reorderId) rh
INNER JOIN
reorderHistoryDetails rd ON rd.reorderhistoryId = rh.reorderhistoryId
INNER JOIN
reorderDetails r ON r.personId = rd.personId
WHERE
rd.statusNo = 1059
)
SELECT DISTINCT TOP 5
rlst.personId AS personId,
'Start Reorder Process' AS reorderStatusType,
1 AS productId,
3 AS amountPaid,
rlst.reorderId,
rlst.OrganizationId AS orgId,
rlst.createdBy,
rlst.dateCreated,
rlst.dateCreated AS timeStamp
FROM
cte rlst
INNER JOIN
doctororderall d ON rlst.personId = d.personId
AND d.productId = 1
AND YEAR(rlst.dateCreated) = YEAR(d.dateCreated)
INNER JOIN
patientdetails pd ON pd.personId = d.personId
WHERE
rlst.datecreated > '2020-09-01'
AND rlst.dateCreated <= '2021-10-06'
INSERT INTO rouletteTracking (personId, reorderStatusType, productId,
amountPaid, reorderId, orgId,
createdBy, dateCreated, timeStamp)
SELECT *
FROM cte
I'm getting this error:
Msg 208, Level 16, State 1, Line 14
Invalid object name 'cte'.
Both myself (in the comments) and Grant (in their answer) has already covered this, however, to repeat this a Common Table Expression is an expression. It's scope is limited to the scope of the statement it is define in (like other expressions). If you properly terminate your statements you'll see you have two statements above; a SELECT and an INSERT, and thus the CTE isn't defined in the second:
WITH cte AS
(SELECT rd.reorderhistoryId,
rd.dateCreated,
rd.personId,
rd.reorderId,
rd.statusNo,
rd.createdBy,
r.OrganizationId
FROM (SELECT MIN(reorderhistoryId) AS reorderhistoryId,
MIN(personId) AS personId,
reorderId
FROM reorderHistoryDetails
GROUP BY reorderId) rh --on d.personId=rh.personId
INNER JOIN reorderHistoryDetails rd ON rd.reorderhistoryId = rh.reorderhistoryId
INNER JOIN reorderDetails r ON r.personId = rd.personId
WHERE rd.statusNo = 1059)
SELECT DISTINCT TOP 5
rlst.personId AS personId,
'Start Reorder Process' AS reorderStatusType,
1 AS productId,
3 AS amountPaid,
rlst.reorderId,
rlst.OrganizationId AS orgId,
rlst.createdBy,
rlst.dateCreated,
rlst.dateCreated AS timeStamp
FROM cte rlst
INNER JOIN doctororderall d ON rlst.personId = d.personId
AND d.productId = 1
AND YEAR(rlst.dateCreated) = YEAR(d.dateCreated)
INNER JOIN patientdetails pd ON pd.personId = d.personId
WHERE rlst.datecreated > '2020-09-01'
AND rlst.dateCreated <= '2021-10-06'; --This statement ends HERE
--New statement starts here. The CTE cte has no context here.
INSERT INTO rouletteTracking (personId,
reorderStatusType,
productId,
amountPaid,
reorderId,
orgId,
createdBy,
dateCreated,
timeStamp)
SELECT *
FROM cte;
Assuming you want to SELECT the TOP (5) (arbitrary) rows first from the CTE first, and then INSERT the entire result set from the CTE into your table afterwards, I would INSERT the data into a temporary table first, and then SELECT and INSERT from that:
WITH cte AS
(SELECT rd.reorderhistoryId,
rd.dateCreated,
rd.personId,
rd.reorderId,
rd.statusNo,
rd.createdBy,
r.OrganizationId
FROM (SELECT MIN(reorderhistoryId) AS reorderhistoryId,
MIN(personId) AS personId,
reorderId
FROM reorderHistoryDetails
GROUP BY reorderId) rh --on d.personId=rh.personId
INNER JOIN reorderHistoryDetails rd ON rd.reorderhistoryId = rh.reorderhistoryId
INNER JOIN reorderDetails r ON r.personId = rd.personId
WHERE rd.statusNo = 1059)
SELECT *
INTO #Temp
FROM cte;
SELECT DISTINCT TOP 5
rlst.personId AS personId,
'Start Reorder Process' AS reorderStatusType,
1 AS productId,
3 AS amountPaid,
rlst.reorderId,
rlst.OrganizationId AS orgId,
rlst.createdBy,
rlst.dateCreated,
rlst.dateCreated AS timeStamp
FROM #Temp rlst
INNER JOIN doctororderall d ON rlst.personId = d.personId
AND d.productId = 1
AND YEAR(rlst.dateCreated) = YEAR(d.dateCreated)
INNER JOIN patientdetails pd ON pd.personId = d.personId
WHERE rlst.datecreated > '2020-09-01'
AND rlst.dateCreated <= '2021-10-06'
ORDER BY {The Column to order by}; --This statement ends HERE
--New statement starts here. The CTE cte has no context here.
INSERT INTO rouletteTracking (personId,
reorderStatusType,
productId,
amountPaid,
reorderId,
orgId,
createdBy,
dateCreated,
timeStamp)
SELECT *
FROM #Temp;
Note: I expect the INSERT statement at the end to still fail. You attempt to insert into a timestamp column (a deprecated synonym for rowversion); that isn't allowed.
The name Common Table Expression is a bit misleading. Many people read it and see the word "TABLE" very prominently. They then assume that they're dealing with a new kind of temporary table or table variable. However, the word to focus on is "EXPRESSION".
A CTE is only useable within the statement that defines it. It's not, in any way, temporary storage. It's simply a query. It's like having a sub-query to define a table like this:
SELECT *
FROM (SELECT * FROM dbo.MyTable) as NotaCTEButActsLikeOne
In order to do your process, you need to move the entire SELECT statement including your CTE into a single statement with the INSERT process.
EDIT:
Here. You just move everything so that it's a single statement:
WITH
cte AS
(SELECT rd.reorderhistoryId,
rd.dateCreated,
rd.personId,
rd.reorderId,
rd.statusNo,
rd.createdBy,
r.OrganizationId
FROM (SELECT MIN(reorderhistoryId) AS reorderhistoryId,
MIN(personId) AS personId,
reorderId
FROM reorderHistoryDetails
GROUP BY reorderId) rh --on d.personId=rh.personId
INNER JOIN reorderHistoryDetails rd ON rd.reorderhistoryId = rh.reorderhistoryId
INNER JOIN reorderDetails r ON r.personId = rd.personId
WHERE rd.statusNo = 1059)
INSERT INTO rouletteTracking (personId,
reorderStatusType,
productId,
amountPaid,
reorderId,
orgId,
createdBy,
dateCreated,
timeStamp)
SELECT DISTINCT TOP 5
rlst.personId AS personId,
'Start Reorder Process' AS reorderStatusType,
1 AS productId,
3 AS amountPaid,
rlst.reorderId,
rlst.OrganizationId AS orgId,
rlst.createdBy,
rlst.dateCreated,
rlst.dateCreated AS timeStamp
FROM cte rlst
INNER JOIN doctororderall d ON rlst.personId = d.personId
AND d.productId = 1
AND YEAR(rlst.dateCreated) = YEAR(d.dateCreated)
INNER JOIN patientdetails pd ON pd.personId = d.personId
--inner join (select min(reorderhistoryId) as reorderhistoryId,min(personId) as personId,reorderId from reorderHistoryDetails where statusNo=1059 and personId=226020 group by reorderId )as rh on d.personId=rh.personId
--inner join reorderHistoryDetails rd on rd.reorderHistoryId=rh.reorderhistoryId and rd.statusNo=1059
WHERE rlst.datecreated > '2020-09-01'
AND rlst.dateCreated <= '2021-10-06';
Also, please, the semi-colon is a statement terminator, not something you put in front of the WITH clause. Examples on line do that so that people can copy the code and it will compile (a righteous approach). However, just putting it at the end of your statements, everything will work and the code won't look awful.

How to add a temp table in SQL server

I am trying to add a temp table to my query so that I can query that temp table, I have searched the internet but I couldn't get a solution.
this is my query
;WITH cte AS (
SELECT ID, g.Name
FROM game.Game g WITH(NOLOCK
WHERE ID IN (SELECT Data FROM system.Split(1, ','))
UNION ALL
SELECT g.ID, g.Name
FROM game.Game g WITH(NOLOCK)
JOIN cte ON g.ParentID = cte.ID
)
SELECT c.ID,
c.Name
FROM cte c
INNER JOIN list.Type gt WITH(NOLOCK) ON c.TypeId = gt.TypeID
WHERE c.ID NOT IN (SELECT Data FROM system.Split(1, ','))
AND c.ID IN (SELECT ID FROM game.code WITH(NOLOCK)
WHERE ID = c.ID
AND StatusCode IN ('OP', 'CL', 'SU')
AND isDisplay = 'True'
AND GETDATE() BETWEEN DisplayStart AND DisplayEnd
AND GETDATE() < ISNULL(ResultDateTime, ResultExpected)
)
which gives me the following when I run it
ID | Name
1111 | BaseBall
2222 |BasketBall
45896 |Relay
now I tried to create a temp table as follows
Create Table #temp(
ID int,
Name varchar
)
;WITH cte AS (
SELECT ID, g.Name
FROM game.Game g WITH(NOLOCK)
WHERE ID IN (SELECT Data FROM system.Split(1, ','))
UNION ALL
SELECT g.ID, g.Name
FROM game.Game g WITH(NOLOCK)
JOIN cte ON g.ParentID = cte.ID
)
insert into #temp // i wanted to set these values in the temp table
SELECT c.ID,
c.Name
FROM cte c
INNER JOIN list.Type gt WITH(NOLOCK) ON c.TypeId = gt.TypeID
WHERE c.ID NOT IN (SELECT Data FROM system.Split(1, ','))
AND c.ID IN (SELECT ID FROM game.code WITH(NOLOCK)
WHERE ID = c.ID
AND StatusCode IN ('OP', 'CL', 'SU')
AND isDisplay = 'True'
AND GETDATE() BETWEEN DisplayStart AND DisplayEnd
AND GETDATE() < ISNULL(ResultDateTime, ResultExpected)
)
every time I try to store this information in the temp table it gives me an error 'Column name or number of supplied values does not match table definition.' But I only have two values in. What am I doing wrong that I cant see?
First, why not just use select into?
IF OBJECT_ID('TempDB..#temp') IS NOT NULL
BEGIN
DROP TABLE #temp
END
select c.ID, c.Name
into #temp
from . . .
Then you don't need to define #temp as a table.
Next, your definition is bad, because Name has only one character. This would be fixed with select into.
However, I don't know why you are getting the particular error you are getting. The numbers of columns appears to match.

Count with row_number function SQL CTE

I have the below CTEs that work perfectly, but I want to count the "cl.memb_dim_id" by "cl.post_date" but I am not sure how to do that? When adding in the count function I get an error that highlights the ' row number' so I am assuming I cant have both order and group together ????
WITH
DATES AS
(
select to_date('01-jan-2017') as startdate,to_date('02-jan-2017') as enddate
from dual
),
Claims as (select distinct
cl.memb_dim_id,
row_number () over (partition by cl.Claim_number order by cl.post_date desc) as uniquerow,
cl.Claim_number,
cl.post_date,
ct.claim_type,
ap.claim_status_desc,
dc.company_desc,
dff.io_flag_desc,
pr.product_desc,
cl.prov_dim_id,
cl.prov_type_dim_id
from dw.fact_claim cl
inner join dates d
on 1=1
and cl.post_date >= d.startdate
and cl.post_date <= d.enddate
and cl.provider_par_dim_id in ('2')
and cl.processing_status_dim_id = '1'
and cl.company_dim_id in ('581','585','586','589','590','591','588','592','594','601','602','603','606','596','598','597','579','599','578','577','573','574','576','575')
left join dw.DIM_CLAIM_STATUS ap
on cl.claim_status_dim_id = ap.claim_status_dim_id
left join dw.dim_claim_type ct
on cl.claim_type_dim_id = ct.claim_type_dim_id
and cl.claim_type_dim_id in ('1','2','6','7')
left join dw.DIM_COMPANY dc
on cl.company_dim_id = dc.company_dim_id
left join dw.DIM_IO_FLAG dff
on cl.io_flag_dim_id = dff.io_flag_dim_id
left join dw.dim_product pr
on cl.product_dim_id = pr.product_dim_id
)
Select * from claims where uniquerow ='1'
First, does this work?
count(cl.memb_dim_id) over (partition by cl.Claim_number, cl.post_date) as cnt,
Second, it is strange to be using analytic functions with select distinct.

Join tables showing all records for each week

I was hoping someone would be able to help me. I have two tables, one with student names and the other with the the amount of homework each student has done, each week (not a real example). Only one student has done any work. I would like to see a table showing all the students and how much work they have done (even if it is null, for each week.
CREATE TABLE #NAME (Name VARCHAR(20))
INSERT INTO #NAME VALUES
('John'),('Tom'),('Jack')
CREATE TABLE #TIME (Name VARCHAR(20), Week INT, Year INT, Total INT)
INSERT INTO #TIME VALUES
('John',1,2017,34),('John',2,2017,24),('John',3,2017,65),('John',4,2017,22),('John',5,2017,45)
I thought a left outer join would work - but it only references the join between names and not weeks
SELECT
#Name.Name,
#Time.Week,
#Time.Year,
#Time.Total
FROM #NAME LEFT OUTER JOIN #Time ON #NAME.Name = #Time.Name
I tried a outer apply - but essentially get the same thing -
SELECT
#Name.Name,
A.Week,
A.Year,
A.Total
FROM #NAME OUTER APPLY (SELECT * FROM #TIme WHERE #Name.Name = #Time.Name) A
The output from the above two queries is shown below - alongside what I am trying to get - repeating for each week, showing all students regardless of whether they have any values associated or not.
I'd be really appreciative if someone could help me with this.
You need to get all combinations of Name, Week, and Year and then do a LEFT JOIN on #TIME to get the desired result:
WITH CteNameWeekYear(Name, [Week], [Year]) AS(
SELECT
n.*, t.*
FROM(
SELECT DISTINCT [Week], [Year] FROM #TIME
) t
CROSS JOIN #Name n
)
SELECT
c.Name,
c.[Week],
c.[Year],
t.Total
FROM CteNameWeekYear c
LEFT JOIN #TIME t
ON c.Name = t.Name
AND c.[Year] = t.[Year]
AND c.[Week] = t.[Week]
ORDER BY c.[Year], c.[Week], c.Name;
ONLINE DEMO
Try this
WITH cte AS (
SELECT DISTINCT t.Week, t.Year
FROM #TIME t
), cte1 AS (
SELECT n.Name, cte.Week, cte.Year
FROM #NAME n
CROSS JOIN cte
)
SELECT
n.Name, n.Week, n.Year, t.Total
FROM
cte1 n
LEFT JOIN #TIME t ON n.NAME = t.NAME AND n.Week = t.Week AND n.Year = t.Year

If Exists inside a CTE in SQl Server

I just want to know that How to write a CTE containing If Exists in SQl Server ?
I had tried to write a CTE below where i am Using If Exists Statement to select weather the record exist or not .In case if the record does not exist then i am assigning default value but i am getting error
'Incorrect syntax near the keyword 'if'
.' Please help me to fix this error and guide me to write this CTE.
Please find below the CTE which i had written:-
Alter procedure St_Proc_GetTeamProductionReport
#mindate DateTime,
#maxdate DateTIme,
#userID varchar(50)
as
Begin
set NoCount on;
with
ProductionCTE(CalendarDate,RoleID,UserID,UserECode,UserName,ImmediateSupervisor,NatureOfWorkName,RegionProjectName,CountyName,WorkTypeName,TaskName,VolumneProcessed,TimeSpent,Comment)
as
(
if exists
(
select P.CalendarDate,U.RoleID,U.UserID,U.UserECode,U.UserName,U.ImmediateSupervisor,N.NatureofWorkName,
R.RegionProjectName,C.Countyname,W.WorktypeName,T.TaskName,P.VolumeProcessed,P.Timespent,P.Comment
from production P inner join NatureOfWork N
on N.NatureofWorkID=P.natureofworkid
inner join dbo.RegionAndProjectInfo R
on R.RegionProjectID=P.RegionProjectID
inner join county C
on C.countyid=P.countyid
inner join worktype W
on W.Worktypeid=P.worktypeID
inner join task T
on T.taskid=P.TaskID
inner join UserInfo U
on U.Userid=P.userid
where P.userid=#userID and ( convert(varchar, P.CalendarDate, 101) ) between (
convert(varchar, #mindate, 101) ) and ( convert(varchar, #maxdate, 101) )
)
else
(
Select '2012-09-14 13:41:52' as CalendarDate,
2 as RoleID,'938' as UserID,
(select Userecode from Userinfo where userid=#userID) as UserECode,
(select UserName from Userinfo where userid=#userID)as UserName,
(select ImmediateSupervisor from Userinfo where userid=#userID)as ImmediateSupervisor,
'BP' as NatureOfWorkName,
'CO Processing' as RegionProjectName,
'Adams' as CountyName,
'Quality' as WorkTypeName,
'Corrections ' as TaskName,
5 as VolumneProcessed,
'01:00' as TimeSpent,
'test' as Comment
)
union all
select P.CalendarDate,U.RoleID,U.UserID,U.UserECode,U.UserName,U.ImmediateSupervisor,N.NatureofWorkName,
R.RegionProjectName,C.Countyname,W.WorktypeName,T.TaskName,P.VolumeProcessed,P.Timespent,P.Comment
from production P inner join NatureOfWork N
on N.NatureofWorkID=P.natureofworkid
inner join dbo.RegionAndProjectInfo R
on R.RegionProjectID=P.RegionProjectID
inner join county C
on C.countyid=P.countyid
inner join worktype W
on W.Worktypeid=P.worktypeID
inner join task T
on T.taskid=P.TaskID
inner join UserInfo U
on U.Userid=P.userid
inner join ProductionCTE
on U.ImmediateSupervisor=ProductionCTE.UserECode
where P.IsTaskCompleted=1 and ( convert(varchar, P.CalendarDate, 101) ) between (
convert(varchar, #mindate, 101) ) and ( convert(varchar, #maxdate, 101) )
)
select distinct CONVERT(VARCHAR,CalendarDate,20) as CalendarDate,UserECode,UserName,NatureOfWorkName,RegionProjectName,CountyName,WorkTypeName,TaskName,VolumneProcessed,TimeSpent,Comment from ProductionCTE where RoleID=1
end
GO
When i am removing this If Exist statement then the CTE is working fine but after adding the IF Exists statement it is having error.
You can't use IF EXISTS in this way. A common table expression must only contain a single select statement, it's not possible to say if condition SELECT THIS else SELECT THAT.
It looks like you are trying to add an additional created row to a resultset returned by a query. You could achieve this by implementing the CTE as a table-valued function that would return the single new row.
http://msdn.microsoft.com/en-gb/library/ms191165(v=sql.105).aspx
If you want to construct a UNION where you only get a result from the second SELECT if the first SELECT returns no rows, you can achieve this using RANK(). So, you can place your real query in the first SELECT and the desired default values in the second, and achieve the results you want.
I don't have your tables and data, so I'm not going to attempt to re-write your query. But I'll illustrate with this:
;With WithDefaults as (
select name,0 as Rank from sys.objects
union all
select 'abc',1 --Default if no results
), Ranked as (
select *,RANK() OVER (ORDER BY Rank) as Rnk from WithDefaults
)
select * from Ranked where Rnk = 1
Returns (on a mostly empty DB I tried it on) 98 results, of which none had the name abc. If we force the first SELECT to return no results:
select name,0 as Rank from sys.objects where 1 = 2
We now get a single row result with the name abc.
Hopefully, you can see how this could apply to your original query.
You can't use IF EXISTS in CTE. But you can modify logic of function
Example:
alter procedure St_Proc_GetTeamProductionReport
#mindate DateTime,
#maxdate DateTIme,
#userID varchar(50)
as
Begin
set NoCount on;
;with
ProductionCTE(CalendarDate,RoleID,UserID,UserECode,UserName,ImmediateSupervisor,NatureOfWorkName,RegionProjectName,CountyName,WorkTypeName,TaskName,VolumneProcessed,TimeSpent,Comment)
as
(
select P.CalendarDate,U.RoleID,U.UserID,U.UserECode,U.UserName,U.ImmediateSupervisor,N.NatureofWorkName,
R.RegionProjectName,C.Countyname,W.WorktypeName,T.TaskName,P.VolumeProcessed,P.Timespent,P.Comment, 1 AS cnt
from production P inner join NatureOfWork N
on N.NatureofWorkID=P.natureofworkid
inner join dbo.RegionAndProjectInfo R
on R.RegionProjectID=P.RegionProjectID
inner join county C
on C.countyid=P.countyid
inner join worktype W
on W.Worktypeid=P.worktypeID
inner join task T
on T.taskid=P.TaskID
inner join UserInfo U
on U.Userid=P.userid
where P.userid=#userID and ( convert(varchar, P.CalendarDate, 101) ) between (
convert(varchar, #mindate, 101) ) and ( convert(varchar, #maxdate, 101) )
UNION ALL
Select '2012-09-14 13:41:52' as CalendarDate,
2 as RoleID,'938' as UserID,
(select Userecode from Userinfo where userid=#userID) as UserECode,
(select UserName from Userinfo where userid=#userID)as UserName,
(select ImmediateSupervisor from Userinfo where userid=#userID)as ImmediateSupervisor,
'BP' as NatureOfWorkName,
'CO Processing' as RegionProjectName,
'Adams' as CountyName,
'Quality' as WorkTypeName,
'Corrections ' as TaskName,
5 as VolumneProcessed,
'01:00' as TimeSpent,
'test' as Comment, 0
), ProductionCTE2 AS
(
SELECT TOP(SELECT CASE WHEN COUNT(*) = 0 THEN 1 ELSE COUNT(*) END FROM ProductionCTE WHERE cnt = 1)
CalendarDate,RoleID,UserID,UserECode,UserName,ImmediateSupervisor,NatureofWorkName,
RegionProjectName,Countyname,WorktypeName,TaskName,VolumeProcessed,Timespent,Comment
FROM ProductionCTE2
union all
select P.CalendarDate,U.RoleID,U.UserID,U.UserECode,U.UserName,U.ImmediateSupervisor,N.NatureofWorkName,
R.RegionProjectName,C.Countyname,W.WorktypeName,T.TaskName,P.VolumeProcessed,P.Timespent,P.Comment
from production P inner join NatureOfWork N
on N.NatureofWorkID=P.natureofworkid
inner join dbo.RegionAndProjectInfo R
on R.RegionProjectID=P.RegionProjectID
inner join county C
on C.countyid=P.countyid
inner join worktype W
on W.Worktypeid=P.worktypeID
inner join task T
on T.taskid=P.TaskID
inner join UserInfo U
on U.Userid=P.userid
inner join ProductionCTE
on U.ImmediateSupervisor=ProductionCTE.UserECode
where P.IsTaskCompleted=1 and ( convert(varchar, P.CalendarDate, 101) ) between (
convert(varchar, #mindate, 101) ) and ( convert(varchar, #maxdate, 101) )
)
select distinct CONVERT(VARCHAR,CalendarDate,20) as CalendarDate,UserECode,UserName,NatureOfWorkName,RegionProjectName,CountyName,WorkTypeName,TaskName,VolumneProcessed,TimeSpent,Comment
from ProductionCTE2
where RoleID=1
end