Question

I am having scenario like this : I am having many tables and each table has date fields.

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 |

I want output like I want total count of records from each table and display it respective to month wise by using Created_Date field dates.

Expected Output like this:

enter image description here

Was it helpful?

Solution

Give this solution a try,

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

OTHER TIPS

I was trying to get the Find the solution since last 4 Hours

I think my answer is Low in Performance as Compare to above Answer,

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])
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top