在select中复制group by子句

时间:2015-03-25 21:22:29

标签: sql sql-server tsql

当它包含在select?

中时,有没有办法不复制group by子句
For example, one can do:
Select x,y,z from mytable order  by 1,2,3
If the clauses of x,y, and z are long complicated functions, this saves typing and mistakes.
However, I am not aware of a typing saver for:
Select f(x),g(y),avg(z) from MyTable group by f(x),g(y)

有什么想法吗?

更大的例子:

SELECT 
     DATEADD(HOUR,datepart(hour,inquirydate),cast(cast(inquirydate as date) as datetime)) as dayhour,
     COUNT(*) as qty,
     AVG(workms+queuems+0.0) as avgTimeMs
    FROM datalog 
  WHERE inquirydate>'1/1/2014'
  GROUP BY DATEADD(HOUR,datepart(hour,inquirydate),cast(cast(inquirydate as date) as datetime))

注意上面的大部分内容被重复了。当一个人重复自己时,他们往往会犯错误。

2 个答案:

答案 0 :(得分:0)

1)如果这是常见的计算,请使用用户定义的函数 OR
2)正如Aaron Bertrand建议将其包装在子查询中

答案 1 :(得分:0)

如前所述,方法很少:subqueryCTEOUTER APPLY

- 解决方案#1:子查询

SELECT 
    h.dayhour,
    COUNT(*) as qty,
    AVG(workms+queuems+0.0) as avgTimeMs
FROM (
    SELECT 
        DATEADD(HOUR,datepart(hour,inquirydate),cast(cast(inquirydate as date) as datetime)) as dayhour,
        workms, queuems
    FROM    datalog 
    WHERE   inquirydate>'20140101' -- It's better to use a date literal (DDMMYYY for DATETIME, DD-MM-YYYY for DATE) which is independent of DATEFORMAT / LANGUAGE settings
) h -- hours subquery
GROUP BY h.dayhour;

- 解决方案#2:通用表格表达式

;WITH CteHours
AS (
    SELECT 
        DATEADD(HOUR,datepart(hour,inquirydate),cast(cast(inquirydate as date) as datetime)) as dayhour,
        workms, queuems
    FROM    datalog 
    WHERE   inquirydate>'20140101' 
)
SELECT h.dayhour,
    COUNT(*) as qty,
    AVG(workms+queuems+0.0) as avgTimeMs
FROM CteHours h -- hours subquery
GROUP BY h.dayhour;

- 解决方案#2:外部申请

SELECT 
    expr.dayhour, 
    COUNT(*) as qty,
    AVG(workms+queuems+0.0) as avgTimeMs
FROM    datalog d
OUTER APPLY (
    SELECT DATEADD(HOUR,datepart(hour,d.inquirydate),cast(cast(d.inquirydate as date) as datetime)) as dayhour
) AS expr
WHERE   inquirydate>'20140101' 
GROUP BY expr.dayhour;

通常,我使用解决方案#1,有时(当查询很复杂时),解决方案#3。