Sql server 如何在SQL中从多个设计相同的表中获取数据
我对SQL比较陌生。我有表Sql server 如何在SQL中从多个设计相同的表中获取数据,sql-server,database-design,multiple-tables,Sql Server,Database Design,Multiple Tables,我对SQL比较陌生。我有表emp010115,emp020115,emp030115。。。。截至2015年1月每个日期的emp310115[表格名称表示其所属日期]。所有桌子的设计都是一样的。让前三个表中的数据如下所示 emp tds tdate ------------------------------------- abc____________50_________2015-01-01 00:00:00 abc___________520_____
emp010115
,emp020115
,emp030115
。。。。截至2015年1月每个日期的emp310115[表格名称表示其所属日期]。所有桌子的设计都是一样的。让前三个表中的数据如下所示
emp tds tdate
-------------------------------------
abc____________50_________2015-01-01 00:00:00
abc___________520_________2015-01-02 00:00:00
abc___________640_________2015-01-03 00:00:00
bca_____________0_________2015-01-01 00:00:00
bca____________20_________2015-01-02 00:00:00
bca___________920_________2015-01-03 00:00:00
emp01012015
:
emp tds investmnt tdate arrears
------------------------------------------------------------------
abc_______50_________15000_________2015-01-01 00:00:00_______325
bca________0_________10000_________2015-01-01 00:00:00_______420
emp tds investmnt tdate arrears
------------------------------------------------------------------
abc______520_________16000_________2015-01-02 00:00:00_______600
bca_______20_________14000_________2015-01-02 00:00:00_______740
emp tds investmnt tdate arrears
------------------------------------------------------------------
abc______640_________11000_________2015-01-03 00:00:00_______850
bca______920_________13000_________2015-01-03 00:00:00_______650
EMP2012015
:
emp tds investmnt tdate arrears
------------------------------------------------------------------
abc_______50_________15000_________2015-01-01 00:00:00_______325
bca________0_________10000_________2015-01-01 00:00:00_______420
emp tds investmnt tdate arrears
------------------------------------------------------------------
abc______520_________16000_________2015-01-02 00:00:00_______600
bca_______20_________14000_________2015-01-02 00:00:00_______740
emp tds investmnt tdate arrears
------------------------------------------------------------------
abc______640_________11000_________2015-01-03 00:00:00_______850
bca______920_________13000_________2015-01-03 00:00:00_______650
emp03012015
:
emp tds investmnt tdate arrears
------------------------------------------------------------------
abc_______50_________15000_________2015-01-01 00:00:00_______325
bca________0_________10000_________2015-01-01 00:00:00_______420
emp tds investmnt tdate arrears
------------------------------------------------------------------
abc______520_________16000_________2015-01-02 00:00:00_______600
bca_______20_________14000_________2015-01-02 00:00:00_______740
emp tds investmnt tdate arrears
------------------------------------------------------------------
abc______640_________11000_________2015-01-03 00:00:00_______850
bca______920_________13000_________2015-01-03 00:00:00_______650
我希望查询的结果如下表所示
emp tds tdate
-------------------------------------
abc____________50_________2015-01-01 00:00:00
abc___________520_________2015-01-02 00:00:00
abc___________640_________2015-01-03 00:00:00
bca_____________0_________2015-01-01 00:00:00
bca____________20_________2015-01-02 00:00:00
bca___________920_________2015-01-03 00:00:00
有谁能帮我一下吗?您可能不应该像这样拆分表,而是应该将所有数据保留在同一个表中(除非有很好的理由),但也就是说,您可以这样使用
union all
查询:
select emp, tds, tdate from emp01012015
union all
select emp, tds, tdate from emp02012015
union all
select emp, tds, tdate from emp03012015
order by emp, tdate
这将创建一组单一的所有数据,然后您可以使用这些数据做您想要做的事情。不过,订购的方式与您的示例并不完全相同
停止每天创建单独的表格即可。通常没有理由这么做。