MS SQL Server 疑難問題

2021-06-15 20:53:53 字數 4886 閱讀 4779

有一表a

id       date              name

1       2007-01-01          ccc

1       2007-01-01          ddd

1       2007-01-01          vv

1       2007-02-01          ttttt

2       2007-01-01          gg

3       2007-01-02          ccc

3       2007-01-01          ccc

4       2007-01-04          yy

輸入1月要得到

日期  ccc     ddd    vv   gg   yy

1號    2       1      1   1    0

2號    1       0      0   0    0

3號    0       0      0   0    0

4號    0       0      0   0    1

.      0       0      0   0    0

.      0       0      0   0    0

.      0       0      0   0    0

31號   0       0      0   0    0

合計   3       1      1   1    1

create table a(id int, date datetime, name varchar(10))

insert a select 1, '2007-01-01', 'ccc'

union all select 1, '2007-01-01', 'ddd'

union all select 1, '2007-01-01', 'vv'

union all select 1, '2007-02-01', 'ttttt'

union all select 2, '2007-01-01', 'gg'

union all select 3, '2007-01-02', 'ccc'

union all select 3, '2007-01-01', 'ccc'

union all select 4, '2007-01-04', 'yy'

go-- 查詢的儲存過程

create proc p_qry

@yearmonth int

asset nocount on

declare @begindate datetime, @enddate datetime, @row int

select

@begindate = convert(datetime, right(@yearmonth * 100 + 1, 8)),

@enddate = dateadd(month, 1, @begindate),

@row = day(@enddate - 1)

declare @dt table(id int identity(1, 1), date datetime)

set rowcount @row

insert @dt(date) select null from syscolumns a, syscolumns b

set rowcount 0

update @dt set date = dateadd(day, id - 1, @begindate)

select

日期 = case grouping(d.id)

when 0 then convert(varchar(10), d.id) + '號'

else '合計' end,

ccc = isnull(sum(case a.name when 'ccc' then 1 else 0 end), 0),

ddd = isnull(sum(case a.name when 'ddd' then 1 else 0 end), 0),

vv = isnull(sum(case a.name when 'vv' then 1 else 0 end), 0),

gg = isnull(sum(case a.name when 'gg' then 1 else 0 end), 0),

yy = isnull(sum(case a.name when 'yy' then 1 else 0 end), 0)

from(

select * from a

where date > @begindate

and date < @enddate

)a right join @dt d

on a.date = d.date

group by d.id with rollup

order by grouping(d.id), d.id

go-- 呼叫

exec p_qry 200601

go-- 刪除測試

drop table a

drop proc p_qry

-- 呼叫

exec p_qry 200701

-- 結果:

日期cccdddvvggyy

1號00000

2號10000

3號00000

4號00001

5號00000

6號00000

7號00000

8號00000

9號00000

10號00000

11號00000

12號00000

13號00000

14號00000

15號00000

16號00000

17號00000

18號00000

19號00000

20號00000

21號00000

22號00000

23號00000

24號00000

25號00000

26號00000

27號00000

28號00000

29號00000

30號00000

31號00000

合計10001

如果要根據滿足條件的name的值來動態確定列名, 則儲存過程改為:

-- 查詢的儲存過程

create proc p_qry

@yearmonth int

asset nocount on

declare @begindate datetime, @enddate datetime, @row int

select

@begindate = convert(datetime, right(@yearmonth * 100 + 1, 8)),

@enddate = dateadd(month, 1, @begindate),

@row = day(@enddate - 1)

declare @dt table(id int identity(1, 1), date datetime)

set rowcount @row

insert @dt(date) select null from syscolumns a, syscolumns b

set rowcount 0

update @dt set date = dateadd(day, id - 1, @begindate)

declare @s nvarchar(4000)

set @s = ''

select @s = @s + ',' + quotename(name)

+ '= isnull(sum(case a.name when ' + quotename(name, '''') + ' then 1 else 0 end), 0)'

from(

select distinct name  from a

where date > @begindate

and date < @enddate

)a if @@rowcount = 0

return

set @s = '

declare @dt table(id int identity(1, 1), date datetime)

set rowcount @row

insert @dt(date) select null from syscolumns a, syscolumns b

set rowcount 0

update @dt set date = dateadd(day, id - 1, @begindate)

select

日期 = case grouping(d.id)

when 0 then convert(varchar(10), d.id) + ''號''

else ''合計'' end' + @s + '

from(

select * from a

where date > @begindate

and date < @enddate

)a right join @dt d

on a.date = d.date

group by d.id with rollup

order by grouping(d.id), d.id

'exec sp_executesql @s, n'@row int, @begindate datetime, @enddate datetime', @row, @begindate, @enddate

gowhere date > =@begindate

and date <= @enddate

這句改了就還了,差個"="

好的給分

python疑難問題

python的列表之間有關係嗎?group queue for in range 2 各組的子佇列 queue queue 組佇列,控制組與組之間的執行順序,因為每個組的專案要一起執行 group queue 6 5 for i in range len group queue print queu...

C 疑難問題 const static

關鍵字const 指定用其宣告的內容保持不變 1.標識變數 代替 define來宣告常量 const doble pi 3.14159 2.標識方法 標識類方法,可以防止方法修改類中不可變的資料成員 class.h class a class.cpp int getvalue const 3.con...

VC疑難問題彙總

error lnk2001 無法解析的外部符號 crtdbgreport 專案屬性 c c 生成 執行時庫 0x7c921230 處未處理的異常 使用者斷點 選擇繼續n 200 次以後可以繼續 debug版直接執行 release版都沒問題 lnk2001 無法解析的外部符號 argv 在共享dll...