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
|