Tsql T-SQL视图-CTE+UNPIVOT与工会与其他技术

Tsql T-SQL视图-CTE+UNPIVOT与工会与其他技术,tsql,view,union,common-table-expression,unpivot,Tsql,View,Union,Common Table Expression,Unpivot,我不知道哪种解决办法更好。我必须在视图中声明一些变量,这些变量是使用T-SQL日期函数DATEADD、DATEPART、GETDATE等计算的 经过一些研究,我写了以下内容: WITH DatePeriods(ThisWeek,LastWeek,MonthToDate,QuarterToDate,YearToDate) AS ( SELECT "...date functions..." AS ThisWeek "...date functions..." AS

我不知道哪种解决办法更好。我必须在视图中声明一些变量,这些变量是使用T-SQL日期函数DATEADD、DATEPART、GETDATE等计算的

经过一些研究,我写了以下内容:

WITH DatePeriods(ThisWeek,LastWeek,MonthToDate,QuarterToDate,YearToDate) AS
(
    SELECT  "...date functions..." AS ThisWeek
            "...date functions..." AS LastWeek
            "...date functions..." AS MonthToDate
            "...date functions..." AS QuarterToDate
            "...date functions..." AS YearToDate
)
SELECT Desciption,Value
FROM DatePeriods
UNPIVOT
(
    Value FOR Desciption IN (ThisWeek,LastWeek,MonthToDate,QuarterToDate,YearToDate)
) AS Source
它看起来很酷,因为我有cte和unpivot。如果我想添加其他日期变量,我只应该在CTE的select中插入

另一种解决方案是使用普通接头:

SELECT  'ThisWeek',"...date functions..." AS ThisWeek
UNION
SELECT  'LastWeek',"...date functions..." AS LastWeek
UNION
SELECT  'MonthToDate',"...date functions..." AS MonthToDate
UNION
SELECT  'QuarterToDate',"...date functions..." AS QuarterToDate
UNION
SELECT  'YearToDate',"...date functions..." AS YearToDate
我认为这不太好,因为一个新的日期变量意味着新的并集,但毕竟只有几个变量之间的并集

有谁能告诉我,在这种情况下,哪种技术是最好的做法,甚至提供了其他解决方案

提前谢谢

编辑:

这是我想要的输出:

Desciption      Value
ThisWeek        2012-08-05 08:55:23.013
LastWeek        2012-07-29 08:55:23.013
MonthToDate     2012-07-08 08:55:23.013
QuarterToDate   2012-05-08 08:55:23.013
YearToDate      2011-08-08 08:55:23.013

如果您查看一下查询计划,就会发现您的union版本的成本要比unpivot版本高得多。但是,如果你改为联合所有人,那将比取消联合要好

如果您使用的是SQL Server 2008或更高版本,则可以改用值,并且根据执行计划,成本与union all相同

价值观版本:

select Description, Value
from (values ('ThisWeek',      getdate()+1),
             ('LastWeek',      getdate()+2),
             ('MonthToDate',   getdate()+3),
             ('QuarterToDate', getdate()+4),
             ('YearToDate',    getdate()+5)
     ) as T(Description, Value)
联合所有版本:

SELECT  'ThisWeek' AS Description, getdate()+1 AS Value
UNION ALL
SELECT  'LastWeek', getdate()+2
UNION ALL
SELECT  'MonthToDate', getdate()+3 
UNION ALL
SELECT  'QuarterToDate', getdate()+4 
UNION ALL
SELECT  'YearToDate', getdate()+5

union比union all慢的原因是它试图从union all合并所有行的结果集中删除重复项,而不考虑值。

如果查看查询计划,您会发现union版本的成本比unpivot版本高得多。但是,如果你改为联合所有人,那将比取消联合要好

如果您使用的是SQL Server 2008或更高版本,则可以改用值,并且根据执行计划,成本与union all相同

价值观版本:

select Description, Value
from (values ('ThisWeek',      getdate()+1),
             ('LastWeek',      getdate()+2),
             ('MonthToDate',   getdate()+3),
             ('QuarterToDate', getdate()+4),
             ('YearToDate',    getdate()+5)
     ) as T(Description, Value)
联合所有版本:

SELECT  'ThisWeek' AS Description, getdate()+1 AS Value
UNION ALL
SELECT  'LastWeek', getdate()+2
UNION ALL
SELECT  'MonthToDate', getdate()+3 
UNION ALL
SELECT  'QuarterToDate', getdate()+4 
UNION ALL
SELECT  'YearToDate', getdate()+5

union比union all慢的原因是,它试图从结果集中删除重复项,其中union all合并所有行,而不考虑值。

您检查过这两个查询的执行计划吗?@BlueFoots是的,我检查过,但在比较执行计划时,我不是一个真正的专家。更重要的是,在执行每个查询的过程中会发生很多事情,但它们都会执行0秒。计划中几乎每一项的成本都为0%。您在两个查询中都检查了执行计划吗?@BlueFoots是的,我检查过,但在比较执行计划时,我并不是一个真正的专家。更重要的是,在执行每个查询的过程中会发生很多事情,但它们都会执行0秒。计划中的每一项几乎都花费了0%。