如何获取分组值

时间:2013-07-02 08:35:19

标签: sql-server sql-server-2008

SELECT 
       DATEPART(YEAR, a.spdate) AS 'Year',
       DATEPART(wk,a.spdate) AS 'WeekNo',
       MONTH(a.spdate) As 'Month',
       MIN(DATEADD(wk, DATEDIFF(wk,0,a.date), 0)) AS 'WeekDate',
       SUM(COALESCE(a.value,0)) AS 'Value'
    FROM patient a 
    JOIN dp account ON (a.id = d.id)
WHERE 
 a.id NOT IN (SELECT id FROM doctor)
GROUP BY DATEPART(YEAR,a.spdate),MONTH(a.spdate)

1 个答案:

答案 0 :(得分:0)

试试这个

 SELECT 
   DATEPART(YEAR, a.spdate) AS 'Year',
   MONTH(a.spdate) As 'Month',      
   SUM(COALESCE(a.value,0)) AS 'Value'
FROM patient a 
JOIN dp account ON (a.id = d.id)
WHERE 
 a.id NOT IN (SELECT id FROM doctor)
 GROUP BY DATEPART(YEAR,a.spdate),MONTH(a.spdate)