每月从多个表中获取记录
Posted
技术标签:
【中文标题】每月从多个表中获取记录【英文标题】:Fetching Records from multiple tables month wise 【发布时间】:2013-04-16 05:31:14 【问题描述】:我有这样的场景:我有很多表,每个表都有日期字段。
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 字段日期按月显示它。
预期输出如下:
【问题讨论】:
请更改问题中的表格名称。 为什么这样??????????????? 查看您的输出并检查表名。 我只是为了让用户清楚地知道该字段显示表记录的总数。所以我只好写了。否则我只想显示表名!好的 【参考方案1】:试试这个解决方案,
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
【讨论】:
哦,我明白了!此查询有效!但是如果我只想显示那些只有记录的月份名称意味着..如果所有表在 12 月份都没有记录,那么.. 不应该显示 12 月份。 @INFOhUb 只需将连接类型从 Skinny 的查询从LEFT JOIN
更改为 INNER JOIN
sqlfiddle.com/#!3/dc613/18【参考方案2】:
自上个 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])
【讨论】:
以上是关于每月从多个表中获取记录的主要内容,如果未能解决你的问题,请参考以下文章