每月从多个表中获取记录

时间:2013-04-16 05:31:14

标签: database sql-server-2008 monthcalendar

我有这样的场景:我有很多桌子,每张桌子都有日期字段。

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字段日期按月显示它。

预期输出如下:

enter image description here

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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])