Question : CREATE VIEW - ;with CTE as (       SELECT *)  -

Hello Experts
I am trying to create a VIEW using the SQL statement which is starting with

;with CTE as (       SELECT * .... )

But I a getting a syntax error.

Please can someone advise how can I create a view of the attached statement

Thanks
S
Attachments:
 
 

Answer : CREATE VIEW - ;with CTE as (       SELECT *)  -

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
CREATE VIEW TestView
AS
with CTE as (
	SELECT  GCT.GiftCertificateTypeName, 
		DATEPART(day, GCH.GiftHistDateAdded) AS [day], 
		DATEPART(month, GCH.GiftHistDateAdded) AS [month], 
		DATEPART(year, GCH.GiftHistDateAdded) AS [year],
		case when b.GiftCodeID is null then 'New Laird Or Lady' else 'Old Laird or Lady' end as SideName
 
	FROM	dbo.GiftCertificateHistory AS GCH INNER JOIN
                      dbo.GiftCertificate AS GC ON GCH.GiftCodeID = GC.GiftCodeID INNER JOIN
                      dbo.GiftCertificateType AS GCT ON GC.GiftCertificateTypeID = GCT.GiftCertificateTypeID  
	left join (
		SELECT GiftCodeID from GiftCertificateHistory Where STATUSID='CODE_CREATED' AND GIFTHISTDATEADDED <='2010-05-20'
	) b on GCH.GiftCodeID = b.GiftCodeID

	WHERE     (GCH.StatusID = 'CERT_SENT')  AND 
        	  (GCH.GiftCodeID LIKE 'LAIRD%') AND
		(GCH.GiftHistDateAdded >= DATEADD(dd, - 6, CAST(CONVERT(VARCHAR(10), GETDATE(), 120) AS DATETIME))) AND 
		(GCH.GiftHistDateAdded < GETDATE())
)
select 	GiftCertificateTypeName, 
	[day],
	[month],
	[year],
	SideName,	
	count(GiftCertificateTypeName) as NumCertsSent
from CTE
group by	GiftCertificateTypeName, 
		[day],
		[month],
		[year],
		SideName
Random Solutions  
 
programming4us programming4us