明确工作

时间:2011-12-14 15:21:57

标签: sql

我正在使用这个

SELECT TOP (100) PERCENT 
       CONVERT(char(10), [Reg Date1], 103) AS [Reg Date], 
       Regs
FROM   (SELECT CAST(SetupDateTime AS datetime) AS [Reg Date1], 
               COUNT(DISTINCT ID) AS Regs
        FROM  dbo.tbl_User
        WHERE (CAST(SetupDateTime AS datetime) BETWEEN 
                   CAST(DATEADD(dd, - 7, GETDATE()) AS datetime) AND 
                   CAST(DATEADD(dd, - 1, GETDATE()) AS datetime))
        GROUP BY CAST(SetupDateTime AS datetime)) AS a
ORDER BY [Reg Date1]

然后产生

Reg Date    Regs
07/12/2011  1
07/12/2011  1
07/12/2011  1
08/12/2011  1
08/12/2011  1
09/12/2011  1
09/12/2011  1
10/12/2011  1
10/12/2011  1
10/12/2011  1

但我想要它

Reg Date    Regs
07/12/2011  10
08/12/2011  12
09/12/2011  15
10/12/2011  11
11/12/2011  10
12/12/2011  17

我似乎无法以这种方式进行分组

2 个答案:

答案 0 :(得分:2)

试试这个:

SELECT     TOP (100) PERCENT CONVERT(char(10), [Reg Date1], 103) AS [Reg Date], Sum(Regs)
FROM         (SELECT     CAST(SetupDateTime AS datetime) AS [Reg Date1], COUNT(DISTINCT ID) AS Regs
                       FROM          dbo.tbl_User
                       WHERE      (CAST(SetupDateTime AS datetime) BETWEEN CAST(DATEADD(dd, - 7, CAST(convert(varchar, GETDATE(), 112) AS datetime)) AS datetime) AND CAST(convert(varchar, GETDATE(), 112) 
                                              AS datetime))
      GROUP BY CAST(SetupDateTime AS datetime)) AS a
group by CONVERT(char(10), [Reg Date1], 103)
ORDER BY CONVERT(char(10), [Reg Date1], 103)

答案 1 :(得分:0)

试试这个:

SELECT TOP (100) PERCENT CONVERT(char(10), [Reg Date1], 103) AS [Reg Date], COUNT(Regs)

请注意COUNT的使用,我怀疑你正在尝试做什么。