Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
sql查询获取从一月到本月的所有数据,即使没有记录_Sql_Sql Server - Fatal编程技术网

sql查询获取从一月到本月的所有数据,即使没有记录

sql查询获取从一月到本月的所有数据,即使没有记录,sql,sql-server,Sql,Sql Server,我不擅长sql,所以任何帮助都会很棒 我有一个SQL查询,它获取从一月到本月已注册的记录 我的代码示例 SELECT DatePart(YEAR, p.createStamp) as TheYear, DatePart(MONTH, p.createStamp) as TheMonth , COUNT(p.pId) AS TOTALCOUNT FROM profile p with(nolock) where DatePart(YEAR, p.createStamp) = DATEPART(

我不擅长sql,所以任何帮助都会很棒

我有一个SQL查询,它获取从一月到本月已注册的记录

我的代码示例

SELECT DatePart(YEAR, p.createStamp) as TheYear, DatePart(MONTH, p.createStamp) as TheMonth ,  COUNT(p.pId) AS TOTALCOUNT 
FROM profile p with(nolock)
where DatePart(YEAR, p.createStamp) = DATEPART(YEAR, GETDATE())
GROUP BY YEAR(p.createStamp), MONTH(p.createStamp)
ORDER BY YEAR(p.createStamp), MONTH(p.createStamp)
查询将如何返回

二月=2,三月=3,四月=4,五月=5

我想让它返回总计数为0的Jan=1和总计数为0的June=6,以及如何执行此操作的想法


谢谢。

您无法选择不存在的内容,因此我建议创建一个查找表:

CREATE TABLE #Months (Year_ INT, Month_ INT)
GO
SET NOCOUNT ON
DECLARE @intFlag INT
SET @intFlag = 1
WHILE (@intFlag <=20)
BEGIN
--Do Stuff
INSERT INTO #Months
SELECT YEAR(DATEADD(MONTH,@intflag,'20121201')),MONTH(DATEADD(MONTH,@intflag,'20121201'))
SET @intFlag = @intFlag + 1
END
GO

下面是一个循环,用于创建月/年组合并将其用作查询的基础:

declare @startDate as datetime
set @startDate = '1/1/13'

declare @currentDate as datetime
set @currentDate = '6/6/13'

select
     month(@currentDate) as monthOfDate
    ,year(@currentDate) as yearOfDate
into #allDates
where 1=0

while (@startDate <= @currentDate)
begin
    insert into #allDates values (month(@startDate),year(@startDate))
    set @startDate = dateadd(m,1,@startDate)
end

select 
     _monthYear.yearofDate
    ,_monthYear.monthOfDate
    , COUNT(p.pId) as total
from #allDates _monthYear
left join profile p with(nolock)
    on month(p.createStamp) = _monthYear.monthOfDate
    and year(p.createStamp) = _monthYear.yearOfDate
group by
     _monthYear.yearofDate
    ,_monthYear.montOfDate

drop table #allDates
将@startDate声明为datetime
设置@startDate='1/1/13'
将@currentDate声明为datetime
设置@currentDate='6/6/13'
选择
月份(@currentDate)作为monthOfDate
,年(@currentDate)作为年月日
进入所有日期
其中1=0

虽然(@startDate可以模拟表月份,而不必在一个db内实际创建它。例如,请参阅模拟天数。有人知道计数时是否需要ISNULL吗?无法立即回忆。@meewoK good call,可能也可以使用CTE,我最终使用日历表进行大量分析,因此希望它们是永久性的。请输入sql server版本
declare @startDate as datetime
set @startDate = '1/1/13'

declare @currentDate as datetime
set @currentDate = '6/6/13'

select
     month(@currentDate) as monthOfDate
    ,year(@currentDate) as yearOfDate
into #allDates
where 1=0

while (@startDate <= @currentDate)
begin
    insert into #allDates values (month(@startDate),year(@startDate))
    set @startDate = dateadd(m,1,@startDate)
end

select 
     _monthYear.yearofDate
    ,_monthYear.monthOfDate
    , COUNT(p.pId) as total
from #allDates _monthYear
left join profile p with(nolock)
    on month(p.createStamp) = _monthYear.monthOfDate
    and year(p.createStamp) = _monthYear.yearOfDate
group by
     _monthYear.yearofDate
    ,_monthYear.montOfDate

drop table #allDates