需要以前11个月数据的sql查询格式

时间:2014-03-10 07:59:39

标签: sql sql-server

我正在运行报告我通过了这个'Jan-13'或'May-13'或'Jun-12'的参数,它可能是任何东西。

select CardName,CardCode ,DLN1.Quantity 
from ODLN inner join DLN1 on odln.DocEntry = ODLN.DocEntry 
where ODLN.DocDate = 'Jan-13' (this doc date is the datetime format) in report they will chose Jan-13 

我需要输出过去11个月的数量与当月1月13日

Cardname    CardCode  Jan-13 Dec-12 Nov-12 Oct-12 Sep-12  aug-12  jul-12   jun-12  may-12   Apr -12  mar-12 feb-12   
EFIGLobal     A2       12     10      12      10    8       12      3       2        12       11       4      6

2 个答案:

答案 0 :(得分:0)

这可以在sqlserver-2008 +

中使用

首先创建此测试表

create table test(CardName varchar(20),CardCode int, Quantity int, DocDate datetime)
insert test values('ab', 1, 2, '2013-01-01 20:30')
insert test values('ab', 1, 2, '2012-05-01 20:30')
insert test values('abc', 3, 4, '2012-03-01 20:30')
go

这是剧本:

-- @a is your input
declare @a char(6) = 'Jan-13'
declare @to datetime = dateadd(mm, 1, convert(datetime, '01-'+@a, 16))
declare @from datetime = dateadd(yy, -1, @to)
declare @col varchar(200)

;with dates as
(
select @to-1 m
union all
select dateadd(mm, -1, m)
from dates
where m > dateadd(mm, 1, @from)
)
select @col = coalesce(@col + ',', '') + '['+right(stuff(convert(varchar(9), m, 6), 7, 1, '-'), 6)+']' from dates

declare @sql nvarchar(max) =
'
;with x as
(
select CardName,CardCode ,Quantity, right(stuff(convert(varchar(9), docdate, 6), 7, 1, ''-''), 6) dd
from 
/*
-- use this in your case remove the comments markers (/* and */)
(
select CardName,CardCode ,DLN1.Quantity 
from ODLN inner join DLN1 on odln.DocEntry = ODLN.DocEntry 
)
*/
test
where DocDate >= @from and DocDate < @to
)
select * from x
pivot (sum(Quantity) FOR [dd] IN ('+@col+')) AS pvt '

EXEC sp_executesql @sql, N'@from datetime, @to datetime', @from, @to

结果:

CardName CardCode   Jan-13 Dec-12 Nov-12 Oct-12 Sep-12 Aug-12 Jul-12 Jun-12 May-12 Apr-12 Mar-12 Feb-12
ab       1          2      NULL   NULL   NULL   NULL   NULL   NULL   NULL   2      NULL   NULL   NULL
abc      3          NULL   NULL   NULL   NULL   NULL   NULL   NULL   NULL   NULL   NULL   4      NULL

答案 1 :(得分:0)

不要在这里寻找无错误的查询,因为你没有提供足够的信息和样本数据。 所以开始指出一个是一个,

Declare @i varchar='Jan-13'
declare @upto int=11
Declare @FromDate date=convert(varchar(12),'1-'+'Jan-13',120)
Declare @Todate date =dateadd(month,@upto,@FromDate)
select convert(char(6),@FromDate,107) ,@ToDate

Declare @StringDate varchar(2000)=''

select @StringDate= stuff((select ','+'['+convert(char(6),ODLN.DocDate ,107)DocDate+']'
from ODLN inner join DLN1 on odln.DocEntry = ODLN.DocEntry 
where ODLN.DocDate between between @FromDate and @Todate
for xml path('')),1,1,'')
--@StringDate will have value like [Jan-13],[Dec-12] and so on
Declare @qry varchar(max)=''


set @qry='select CardName,'+@StringDate+'  from
(select CardName,CardCode ,DLN1.Quantity,convert(char(6),ODLN.DocDate ,107)DocDate
from ODLN inner join DLN1 on odln.DocEntry = ODLN.DocEntry 
where ODLN.DocDate between between '+@FromDate+' and '+@Todate+')tbl
pivot( max(Quantity) for DocDate in('+@StringDate+'))pvt'

exec @qry