1

我有这样的场景:我有很多表,每个表都有日期字段。

Table: FA

Id  | Created_Date  |
------------------------
1   | 1/12/2012 |
2   | 2/15/2012 |
3   | 2/25/2012 |

Table: TPA

Id  | Created_Date  |
------------------------
1   | 3/10/2012 |
2   | 4/25/2012 |
3   | 5/20/2012 |
4   | 5/21/2012 |

Table: Gift

Id  | Created_Date  |
------------------------
1   | 6/10/2012 |
2   | 7/25/2012 |
3   | 8/10/2012 |
3   | 7/11/2012 |

我想要输出,就像我想要每个表中的记录总数一样,并通过使用 Created_Date 字段日期按月显示它。

预期输出如下:

在此处输入图像描述

4

2 回答 2

3

试试这个解决方案,

SELECT  MonthName, 
        COALESCE(FA, 0) FA, 
        COALESCE(TPA, 0) TPA, 
        COALESCE(GIFT, 0) GIFT
FROM
        (
            SELECT  monthList.ordby,
                    monthList.MonthName,
                    org.TableName,
                    org.TotalCount
            FROM    
                    (
                        SELECT 1 ordby, 'January' MonthName UNION SELECT 2, 'February' UNION
                        SELECT 3, 'March' UNION SELECT 4, 'April' UNION
                        SELECT 5,'May' UNION SELECT 6,'June' UNION
                        SELECT 7,'July' UNION SELECT 8,'August' UNION 
                        SELECT 9,'September' UNION SELECT 10,'October' UNION
                        SELECT 11,'November' UNION SELECT 12,'December'
                    ) monthList
                    LEFT JOIN
                    (
                        SELECT  'FA' TableName, 
                                DATENAME(mm,Created_Date) MonthName, 
                                COUNT(*) TotalCount
                        FROM    FA
                        GROUP   BY DATENAME(mm,Created_Date)
                        UNION
                        SELECT  'TPA' TableName, 
                                DATENAME(mm,Created_Date) MonthName, 
                                COUNT(*) TotalCount
                        FROM    TPA
                        GROUP   BY DATENAME(mm,Created_Date)
                        UNION
                        SELECT  'Gift' TableName, 
                                DATENAME(mm,Created_Date) MonthName, 
                                COUNT(*) TotalCount
                        FROM    Gift
                        GROUP   BY DATENAME(mm,Created_Date)
                    ) org ON monthList.MonthName = org.MonthName
        ) data
        PIVOT
        (
            MAX(TotalCount)
            FOR TableName IN ([FA], [TPA],[GIFT])
        ) head
ORDER   BY ordby

http://www.sqlfiddle.com/#!3/dc613/14

于 2013-04-16T06:06:21.410 回答
1

自过去4 小时以来,我一直在尝试找到解决方案

与上述答案相比,我认为我的答案性能低下,

create table #month
(
    id [int] IDENTITY(1,1) NOT NULL,
    [month] varchar(3)
)

DBCC CHECKIDENT(#month, RESEED, 1)

insert into #month
values
('Jan'),
('Feb'),
('Mar'),
('Apr'),
('May'),
('Jun'),
('Jul'),
('Aug'),
('Sep'),
('Oct'),
('Nov'),
('Dec')


declare @id int
set @id=1

declare @month varchar(3)

declare @sql nvarchar(max)
set @sql=''
declare @order varchar(3)


while (@id<13)
BEGIN
    set @month=(select [month] from #month where id=@id)
    set @order=CONVERT(varchar(3),@id)
    set @sql=@sql+'insert into #display
(
    [Month],
    [COUNT(FA)],
    [COUNT(TPA)],
    [COUNT(Gift)],
    [ORDER]
)
    select '''+ @month +''' AS [Month],
    (select 
    COUNT(CONVERT(VARCHAR(3), DATENAME(MM,Created_Date), 100)) 
    from FA 
    where CONVERT(VARCHAR(3), DATENAME(MM,Created_Date), 100)='''+ @month +''') [COUNT(FA)],
    (select 
    COUNT(CONVERT(VARCHAR(3), DATENAME(MM,Created_Date), 100)) 
    from TPA 
    where CONVERT(VARCHAR(3), DATENAME(MM,Created_Date), 100)='''+ @month +''') [COUNT(TPA)],
    (select 
    COUNT(CONVERT(VARCHAR(3), DATENAME(MM,Created_Date), 100)) 
    from Gift 
    where CONVERT(VARCHAR(3), DATENAME(MM,Created_Date), 100)='''+ @month +''') [COUNT(Gift)],
    '''+ @order + ''' [Order]
    '


    set @id=@id+1   --Incrementing the month
END
exec sp_executesql @sql
print @sql


select [MONTH],
        [COUNT(FA)],
        [COUNT(TPA)],
        [COUNT(Gift)]
from #display
order by convert(int,[ORDER])
于 2013-04-16T10:16:37.443 回答