sql server 某一列求和
SELECT 患者来源,设备类型,检查部位,设备名称,convert(char(10),STUDY_DATE,121) as 日期, count(distinct 就诊人次) as 就诊人次--SUM(就诊人数) AS 就诊人数 FROM dbo.[V_SeePeople] WHERE STUDY_DATE >= '2019-12-04 00:00:00.000' and STUDY_DATE <= '2019-12-11 23:59:59.000' and OFDEPARTMENT ='FSK' and ORGNIZATIONCODE ='0101' GROUP BY 患者来源,设备类型,检查部位,设备名称,convert(char(10),STUDY_DATE,121) --order by convert(char(10),STUDY_DATE,121) UNION all select '' ,'' ,'' ,'','合计' ,sum( convert(int,temp.就诊人次))AS 就诊人次 from ( SELECT top 100 percent 患者来源,设备类型,检查部位,设备名称,convert(char(10),STUDY_DATE,121) as 日期, count(distinct 就诊人次) as 就诊人次--SUM(就诊人数) AS 就诊人数 FROM dbo.[V_SeePeople] WHERE STUDY_DATE >= '2019-12-04 00:00:00.000' and STUDY_DATE <= '2019-12-11 23:59:59.000' and OFDEPARTMENT ='FSK' and ORGNIZATIONCODE ='0101' GROUP BY 患者来源,设备类型,检查部位,设备名称,convert(char(10),STUDY_DATE,121) --order by convert(char(10),STUDY_DATE,121) ) as temp
SELECT 患者来源,设备类型,检查部位,设备名称,convert(char(10),STUDY_DATE,121) as 日期, count(distinct 就诊人次) as 就诊人次--SUM(就诊人数) AS 就诊人数 FROM dbo.[V_SeePeople] WHERE STUDY_DATE >= '2019-12-04 00:00:00.000' and STUDY_DATE <= '2019-12-11 23:59:59.000' and OFDEPARTMENT ='FSK' and ORGNIZATIONCODE ='0101' GROUP BY 患者来源,设备类型,检查部位,设备名称,convert(char(10),STUDY_DATE,121) --order by convert(char(10),STUDY_DATE,121) UNION all select '' as 患者来源,'' as 设备类型,'' as 检查部位,''as 设备名称,'合计' as 日期,sum( convert(int,temp.就诊人次))AS 就诊人次 from ( SELECT top 100 percent 患者来源,设备类型,检查部位,设备名称,convert(char(10),STUDY_DATE,121) as 日期, count(distinct 就诊人次) as 就诊人次--SUM(就诊人数) AS 就诊人数 FROM dbo.[V_SeePeople] WHERE STUDY_DATE >= '2019-12-04 00:00:00.000' and STUDY_DATE <= '2019-12-11 23:59:59.000' and OFDEPARTMENT ='FSK' and ORGNIZATIONCODE ='0101' GROUP BY 患者来源,设备类型,检查部位,设备名称,convert(char(10),STUDY_DATE,121) --order by convert(char(10),STUDY_DATE,121) ) as temp
注意:sql 存储 中 select '' == select ''''
eg:
set @sqlstr2 = ' UNION all select '''' ,'''' ,'''' ,'''' ,''合计'',sum( convert(int,temp.就诊人数))AS 就诊人数
from (SELECT top 100 percent '+@SelectName+@sdate+' as 日期,
count(distinct 就诊人数) as 就诊人数 FROM dbo.[V_SeePeople] WHERE
'+@sDateColum+' >= '''+ @sStartDate+' 00:00:00' +''' and '+@sDateColum+' <= ''' +@sEndDate+' 23:59:59'+''' '+@sMod+'
GROUP BY '+@GroupName + @sdate + ') as temp' ;