存储过程中的计数总和

时间:2017-04-19 18:11:04

标签: sql-server sql-server-2008 tsql

我需要更改此存储过程,以便为我提供列数'的计数总和。 这样做的最佳方式是什么?

ALTER procedure [dbo].[usp_report]
(
@para date = NULL               
)
as
declare @date date = coalesce(@para, cast( getdate() as date)) 

select
'ctfog' as Place,
@date as 'Date',
count(ctfog.Dassign) as number
from
(select 1 as x)x
LEFT OUTER JOIN
dbo.ctfog ON cast(Dassign as date) = @date

 union 

select     'abbup' as Place, 
        @date as 'Date',
count(abbup.Dassign) as number
from
(select 1 as x)x
LEFT OUTER JOIN
dbo.abbup ON cast(Dassign as date) = @date

union 

select     'ggiis' as Place, 
        @date as 'Date',
count(ggiis.Dassign) as number
(select 1 as x)x
LEFT OUTER JOIN
dbo.ggiis ON cast(Dassign as date) = @date

我当前的表格看起来像这样

地点|日期|编号

ctfog | 1111年1月1日| 8

abbup | 1/1/1111 | 9

ggiis | 1111年1月1日| 4

我需要这样的东西

地点|日期|编号

ctfog | 1/1/1111 | 8

abbup | 1/1/1111 | 10

ggiis | 1/1/1111 | 4

总计| 1/1/1111 | 22

2 个答案:

答案 0 :(得分:0)

我不明白你在这些查询中想要做些什么。

  

(选择1作为x)x

当没有符合查询条件的行时,COUNT将返回0,因此这是完全没必要的。

我在第一次查询后删除了列别名,因为它们无论如何都会被忽略。我还将此更改为使用UNION ALL,因为UNION将检查并删除不可能的重复项,因为每个查询中都有字符串文字。

非常确定你可以沿着这些方向使用某些东西。

ALTER procedure [dbo].[usp_report]
(
    @para date = NULL               
)
as
    declare @date date = coalesce(@para, cast( getdate() as date)) 
    with GroupedData as
    (
        select
            'ctfog' as Place,
            @date as 'Date',
            count(ctfog.Dassign) as number
        from dbo.ctfog 
        where cast(Dassign as date) = @date

        union ALL

        select
            'abbup', 
            @date,
            count(abbup.Dassign)
        from dbo.abbup
        where cast(Dassign as date) = @date

        union ALL

        select
            'ggiis', 
            @date,
            count(ggiis.Dassign)
        from dbo.ggiis
        where cast(Dassign as date) = @date
    )       

    select Place
        , Date
        , number
    from GroupedData

    UNION ALL
    select 'Total'
        , @date
        , SUM(number)
    from GroupedData

答案 1 :(得分:0)

你可以尝试这个,我没试过,但我认为它有效。

ALTER procedure [dbo].[usp_report]
(
@para date = NULL               
)
as

declare @date date = coalesce(@para, cast( getdate() as date)) 
declare @temp_table as table(place nvarchar(100),date_ datetime, number int)

insert into @temp_table
select
'ctfog' as Place,
@date as 'Date',
count(ctfog.Dassign) as number
from
(select 1 as x)x
LEFT OUTER JOIN
dbo.ctfog ON cast(Dassign as date) = @date

insert into @temp_table
select     'abbup' as Place, 
    @date as 'Date',
count(abbup.Dassign) as number
from
(select 1 as x)x
LEFT OUTER JOIN
dbo.abbup ON cast(Dassign as date) = @date

insert into @temp_table
select     'ggiis' as Place, 
    @date as 'Date',
count(ggiis.Dassign) as number
(select 1 as x)x
LEFT OUTER JOIN
dbo.ggiis ON cast(Dassign as date) = @date

select place, date_, sum(number) number
into #temp
from @temp_table
group by place, date_ with rollup

update #temp set place='TOTAL', date_ = @date where date_ is null

select *
from #temp