SQL是否可以在“ WITH”语句中使用变量?

时间:2019-12-24 19:59:28

标签: sql sql-server

我有这个'WITH'语句,它提取一周中每一天的数据。代码可以工作,但是在修改变量时更改代码变得很麻烦,因为我必须遍历每个代码块。许多代码重复了。我想知道是否有一种方法可以将重复的代码放入一个变量中,然后重复该变量吗?

编辑:非常感谢您的回答!我没想到会得到一个。

WITH 
  cteMonday (CTEdate, CTEtime, DayAverage) AS
  (
select CONVERT(VARCHAR(10), [Datetime], 120) AS Date --extract only the time portion
            ,CONVERT(VARCHAR(5), [Datetime], 108) AS Time  --Extract only the date portion
            ,[High] - [Low] AS hilow 
         from [Trading].[dbo].MicroES
        where DATEPART(dw, Datetime) = 2
        AND CONVERT(VARCHAR(5), [Datetime], 108) = '14:00'
        --AND [Date] >= '2016-07-27'
  ),
    cteTuesday (CTEdate, CTEtime,DayAverage) AS
  (
select CONVERT(VARCHAR(10), [Datetime], 120) AS Date
            ,CONVERT(VARCHAR(5), [Datetime], 108) AS Time --extract only the time portion
            ,[High] - [Low] AS hilow --Extract only the date portion
         from [Trading].[dbo].MicroES
where DATEPART(dw, Datetime) = 3
--AND [Date] >= '2018-07-27'
--AND [Date] >= '2016-07-27'
  ),
      cteWednesday (CTEdate, CTEtime, DayAverage) AS
  (
select CONVERT(VARCHAR(10), [Datetime], 120) AS Date
            ,CONVERT(VARCHAR(5), [Datetime], 108) --extract only the time portion
            ,[High] - [Low] AS hilow --Extract only the date portion
         from [Trading].[dbo].MicroES
where DATEPART(dw, Datetime) = 4
--AND [Date] >= '2018-07-27'
--AND [Date] >= '2016-07-27'
  ),
      cteThursday (CTEdate, CTEtime, DayAverage) AS
  (
select CONVERT(VARCHAR(10), [Datetime], 120) AS Date
            ,CONVERT(VARCHAR(5), [Datetime], 108) AS Time --extract only the time portion
            ,[High] - [Low] AS hilow --Extract only the date portion
         from [Trading].[dbo].MicroES
where DATEPART(dw, Datetime) = 5
--AND [Date] >= '2018-07-27'
--AND [Date] >= '2016-07-27'
  ),
      cteFriday (CTEdate, CTEtime, DayAverage) AS
  (
select CONVERT(VARCHAR(10), [Datetime], 120) AS Date
            ,CONVERT(VARCHAR(5), [Datetime], 108) AS Time --extract only the time portion
            ,[High] - [Low] AS hilow --Extract only the date portion
         from [Trading].[dbo].MicroES
where DATEPART(dw, Datetime) = 6
AND CONVERT(VARCHAR(5), [Datetime], 108) = '14:00'
--AND [Date] >= '2016-07-27'
  )

  select 
  'Monday' AS 'Day',
AVG(DayAverage) AS 'DayAverage',
    MIN(DayAverage) as min_day,
  MAX(DayAverage) as max_day,
  COUNT(DayAverage) as 'Days'  
   from cteMonday

   UNION ALL 
     select 
  'Tuesday' AS 'Day',
  AVG(DayAverage) AS DayAverage,
    MIN(DayAverage) as min_day,
  MAX(DayAverage) as max_day,
  COUNT(DayAverage) as 'Days'  
   from cteTuesday

   UNION ALL
     select 
  'Wednesday' AS 'Day',
  AVG(DayAverage) AS 'DayAverage',
    MIN(DayAverage) as min_day,
  MAX(DayAverage) as max_day,
  COUNT(DayAverage) as 'Days'  
   from cteWednesday

      UNION ALL
     select 
  'Thursday' AS 'Day',
  AVG(DayAverage) AS 'DayAverage',
    MIN(DayAverage) as min_day,
  MAX(DayAverage) as max_day,
  COUNT(DayAverage) as 'Days'  
   from cteThursday

      UNION ALL
     select 
  'Friday' AS 'Day',
  AVG(DayAverage) AS 'DayAverage',
    MIN(DayAverage) as min_day,
  MAX(DayAverage) as max_day,
  COUNT(DayAverage) as 'Days'  
   from cteFriday

1 个答案:

答案 0 :(得分:2)

这个评论太长了,但是您不能如下重写整个语句吗?

USE Trading;
GO

SELECT DATENAME(WEEKDAY, M.[DateTime]),
       AVG(M.[High] - M.[Low]) AS DayAverage,
       MIN(M.[High] - M.[Low]) as min_day,
       MAX(M.[High] - M.[Low]) as max_day,
       COUNT(M.[High] - M.[Low]) as Days  
FROM dbo.MicroES M
WHERE (DATEPART(weekday,M.[Datetime]) IN (3,4,5)
   OR (DATEPART(weekday,M.[Datetime]) IN (2,6) AND CONVERT(time, M.[DateTime]) = '14:00')) --Courtesy of LukStorm's correction
GROUP BY DATENAME(WEEKDAY, M.[DateTime]);

由于[Datetime]WHERE上的功能,这样做仍然会很差,因此我为它们创建了计算列。但肯定比使用相同的非SARGable子句对表MicroES进行5次扫描要好。