我有表dbo。[sg_Attendance],其架构如上图所示。 现在使用下面的查询我得到输出只有列出的数据:
SELECT Year(SG_PROCESS_DATE) as yearname, DATENAME(MONTH,DateAdd(MONTH, MONTH(SG_PROCESS_DATE),0) - 1) AS monthname,SUM(SG_WORKTIME) as workhour from dbo.[SG_ATTENDANCE] where (SG_PROCESS_DATE BETWEEN '2013-01-01' AND '2013-05-01') Group By YEAR(SG_PROCESS_DATE), MONTH(SG_PROCESS_DATE)
我的要求是获取范围内所有月份的数据:
答案 0 :(得分:2)
最后我得到了解决方案:
SELECT Year(SG_PROCESS_DATE) AS yearname ,DATENAME(MONTH, DateAdd(MONTH, MONTH(SG_PROCESS_DATE), 0) - 1) AS monthname ,SUM(SG_WORKTIME) AS workhour FROM innovator.[SG_ATTENDANCE] WHERE ( SG_PROCESS_DATE BETWEEN '2013-01-01' AND '2014-03-01') GROUP BY YEAR(SG_PROCESS_DATE),MONTH(SG_PROCESS_DATE)
UNION
SELECT DATENAME(YEAR, DATEADD(MONTH , x.number , '2013-01-01')) as yearname, DATENAME(MONTH, DATEADD(MONTH, x.number, '2013-01-01')) AS monthname, 0 as workhour FROM master.dbo.spt_values x WHERE x.type = 'P' AND x.number <= DATEDIFF(MONTH, '2013-01-01', '2014-03-01')
EXCEPT
SELECT Year(SG_PROCESS_DATE) AS yearname ,DATENAME(MONTH, DateAdd(MONTH, MONTH(SG_PROCESS_DATE), 0) - 1) AS monthname ,0 AS workhour FROM innovator.[SG_ATTENDANCE] WHERE ( SG_PROCESS_DATE BETWEEN '2013-01-01' AND '2014-03-01') GROUP BY YEAR(SG_PROCESS_DATE),MONTH(SG_PROCESS_DATE)
答案 1 :(得分:1)
这是使用master..spt_values
表的小技巧的另一个选项,其中包含从0到2047的序号。
declare @months table (mm date)
declare @start date, @end date
set @start = '2013-01-01'
set @end = '2013-05-01'
insert into @months (mm)
select dateadd(day,v.number,@start)
from master..spt_values v
where
v.number >= 0 and
v.number <= DATEDIFF(day,@start,@end) and
v.type = 'p';
SELECT Year(m.mm) as yearname,
DATENAME(MONTH,DateAdd(MONTH, MONTH(m.mm),0) - 1) AS monthname,
SUM(SG_WORKTIME) as workhour
from @months m
left join dbo.[SG_ATTENDANCE] a on a.SG_PROCESS_DATE = m.mm
where SG_PROCESS_DATE is null or (SG_PROCESS_DATE BETWEEN @start AND @end)
Group By YEAR(m.mm), MONTH(m.mm)
答案 2 :(得分:0)
假设您正在查看1月到5月之间固定日期范围的预期输出,您可以这样做
SELECT yearname, monthname, SUM(workhour) AS workhour
FROM
(
SELECT Year(SG_PROCESS_DATE) AS yearname
,DATENAME(MONTH, DateAdd(MONTH, MONTH(SG_PROCESS_DATE), 0) - 1) AS monthname
,SUM(SG_WORKTIME) AS workhour
FROM dbo.[SG_ATTENDANCE]
WHERE (
SG_PROCESS_DATE BETWEEN '2013-01-01'
AND '2013-05-01'
)
GROUP BY YEAR(SG_PROCESS_DATE)
,MONTH(SG_PROCESS_DATE)
UNION ALL
SELECT '2013' AS yearname, 'January' AS monthname, 0 AS workhour
UNION ALL
SELECT '2013' AS yearname, 'February' AS monthname, 0 AS workhour
UNION ALL
SELECT '2013' AS yearname, 'March' AS monthname, 0 AS workhour
UNION ALL
SELECT '2013' AS yearname, 'April' AS monthname, 0 AS workhour
UNION ALL
SELECT '2013' AS yearname, 'May' AS monthname, 0 AS workhour
)
GROUP BY yearname, monthname
答案 3 :(得分:0)
与IndoKnight相同,例如
select month.monthName,strftime('%m',b.date) ,sum(b.amount) from(
select 1 as monthNumber, 'January' as monthName union
select 2 as monthNumber, 'February' as monthName union
select 3 as monthNumber, 'March' as monthName union
select 4 as monthNumber, 'April' as monthName union
select 5 as monthNumber, 'May' as monthName union
select 6 as monthNumber, 'June' as monthName union
select 7 as monthNumber, 'July' as monthName union
select 8 as monthNumber, 'August' as monthName union
select 9 as monthNumber, 'September' as monthName union
select 10 as monthNumber, 'October' as monthName union
select 11 as monthNumber, 'November' as monthName union
select 12 as monthNumber, 'December' as monthName
) as month
left join bill b on month.monthNumber+' '=strftime('%m',b.date)+' '
group by monthName
连接以将int转换为varchar数据类型比较
应该可以正常工作!!!!!
答案 4 :(得分:0)
CREATEPROCEDURE [dbo].[SP_Range_MYDLSTBind]
(
@FromMonth int=NULL,
@ToMonth int=NULL,
@FromYear int=NULL,
@ToYear int=NULL
)
AS
BEGIN
DECLARE @FromDate DATE=NULL
DECLARE @ToDate DATE=NULL
SET @FromDate=DateAdd(day,0, DateAdd(month, @FromMonth - 1,DateAdd(Year, @FromYear-1900, 0)))
SET @ToDate=DateAdd(day,-1, DateAdd(month, @ToMonth - 0,DateAdd(Year, @ToYear-1900, 0)))
SELECT DISTINCT fldMonth,fldYear, ([dbo].[GetMonthName](fldMonth)+'-'+ CONVERT(varchar,fldYear)) AS flmMonthNameY FROM tbl_Attendance_Monthly
WHERE RDate BETWEEN @FromDate AND @ToDate
ORDER BY fldYear asc,fldMonth asc
END