【问题标题】:How to create a view from these 2 tables?如何从这 2 个表中创建视图?
【发布时间】:2014-11-24 17:11:53
【问题描述】:
Table 1 

bln thn qty1    
1   2014    10  
1   2014    20  
2   2014    30  
3   2014    40  
2   2014    50  
4   2014    60  



Table 2

bln thn qty2    
3   2014    200 
5   2014    400 
2   2014    100 
2   2014    500 
4   2014    300 
6   2014    600 

New View

bln thn qty1    qty2
1   2014    30  0
2   2014    80  600
3   2014    40  200
4   2014    60  300
5   2014    0   400
6   2014    0   600

从顶部的 2 个表中,我想在底部创建一个类似于表的视图。

有人愿意帮忙吗? :D 谢谢

【问题讨论】:

  • 4年前解决;看看这个:stackoverflow.com/questions/3267360/…
  • 我尝试过使用 Union,但结果是这样的:bln thn qty1 1 2014 10 1 2014 20 2 2014 30 3 2014 40 2 2014 50 4 2014 60 3 2014 200 5 2014 400 2 201 100 2 2014 500 4 2014 300 6 2014 600 与我需要的不同:)

标签: sql-server create-view


【解决方案1】:

我做了与@JohnBevan 相同的选择,但做了一些更改(没有在主选择上使用分组,他也忘记命名内部选择列)

DECLARE @table1 TABLE (bln INT, thn INT, qty1 INT)

INSERT INTO @table1 SELECT 1,2014,10
INSERT INTO @table1 SELECT 1,2014,20
INSERT INTO @table1 SELECT 2,2014,30
INSERT INTO @table1 SELECT 3,2014,40
INSERT INTO @table1 SELECT 2,2014,50
INSERT INTO @table1 SELECT 4,2014,60

DECLARE @table2 TABLE (bln INT, thn INT, qty2 INT)

INSERT INTO @table2 SELECT 3,2014,200
INSERT INTO @table2 SELECT 5,2014,400
INSERT INTO @table2 SELECT 2,2014,100
INSERT INTO @table2 SELECT 2,2014,500
INSERT INTO @table2 SELECT 4,2014,300
INSERT INTO @table2 SELECT 6,2014,600

CREATE VIEW NewView AS
SELECT COALESCE(T1.bln, T2.bln) AS bln
    , COALESCE(T1.thn, T2.thn) AS thn
    , COALESCE(T1.qty1, 0) AS qty1
    , COALESCE(T2.qty2, 0) AS qty2
FROM (
    SELECT bln, thn, SUM(qty1) AS qty1
    FROM @table1
    GROUP BY bln, thn
) AS T1
FULL JOIN (
    SELECT bln, thn, SUM(qty2) AS qty2
    FROM @table2
    GROUP BY bln, thn
) AS T2
    ON T1.bln = T2.bln
    AND T1.thn = T2.thn

SQLFiddle:http://sqlfiddle.com/#!6/a3e2b/1

【讨论】:

  • 好答案;绝对比我的好。
【解决方案2】:
--create a new view called newview
create view NewView as
--if table1 has a record for this bln use it; otherwise take table 2's value
select coalesce(t1.bln,t2.bln) bln
--same for thn
, coalesce(t1.thn,t2.thn) thn
--take the sum of qty1 calculated below (max used just because we need an aggregate - they're all the same
, max(t1.qty1) qty1
--same for qty2
, max(t2.qty2) qty2
--select from the tables summing the quantities here (so we have a 1:1 match on the join / don't have to sum for every match)
from (select bln, thn, sum(qty1) as qty1 from table1 group by bln, thn) t1
full outer join (select bln, thn, sum(qty2) as qty2  from table2 group by bln, thn) t2
    on t1.bln = t2.bln
    and t1.thn = t2.thn
--group by the common fields so we get 1 record per value combination
group by t1.bln, t2.bln, t1.thn, t2.thn 

SQLFiddle:http://sqlfiddle.com/#!6/708da/1

【讨论】:

  • 可能的错字:以“full outer”开头的行; table2 应该是 t2 吗?以“from”开头的行也是如此
猜你喜欢
  • 1970-01-01
  • 2013-11-30
  • 1970-01-01
  • 1970-01-01
  • 2021-04-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多