表的关联(100分)

萍儿

Unregistered / Unconfirmed
GUEST, unregistred user!
我分别查出了两个语句
统计销售收入
with
temp1 as (select wlmc A.xssr bywc from biao A),
temp2 as (select wlmc b.xssr bnwc from biao B)
select '销售收入' xm,sum(bywc) bywc,sum(bnwc) bnwc
from temp1 a left jion temp2 b on a.wlmc=b.wlmc
和统计销售额
with
temp1 as (select wlmc A.xse bywc from biao A),
temp2 as (select wlmc b.xse bnwc from biao B)
select '销售额 xm,sum(bywc) bywc,sum(bnwc) bnwc
from temp1 a left jion temp2 b on a.wlmc=b.wlmc

我想把以上的两个统计结果显示到一个表中,结果如下:
xm bywc bnwc
销售收入
销售额

请问怎么写??



 
在线等待,请问有人回答吗?
 
两个查询 UNION 不就可以了么
 
不行,报错!!
 
inner join

try it
 
select * from (
with
temp1 as (select wlmc A.xssr bywc from biao A),
temp2 as (select wlmc b.xssr bnwc from biao B)
select '销售收入' xm,sum(bywc) bywc,sum(bnwc) bnwc
from temp1 a left jion temp2 b on a.wlmc=b.wlmc
) as xxx(xm, bywc, bnwc)
union
select * from
(
with
temp1 as (select wlmc A.xse bywc from biao A),
temp2 as (select wlmc b.xse bnwc from biao B)
select '销售额 xm,sum(bywc) bywc,sum(bnwc) bnwc
from temp1 a left jion temp2 b on a.wlmc=b.wlmc
)as yyy(xm, bywc, bnwc)

 
left join等我不熟悉,因为完全可以用另外的方法行通。
这个很简单的,自己努力想想了,如果是SQL Server2000,
不访试试函数,可以返回表的,比较方便。
 
用的是db2,其实在此懂不懂left join没有关系,只是这两部分相同的列的结果我如何
放到一个表里面
 
忘了,DB2好像只能有一个With
 
那怎么办啊??
 
不用with 直接用 Select你没有必要用外连接,看你的逻辑用内连接就可以了
select a.xxx, b.xxx
from biao a, biao b
where a.yyy=b.yyy
这样可以不用with

 
统计销售收入
with
temp1 as (select wlmc A.xssr bywc from biao A),
temp2 as (select wlmc b.xssr bnwc from biao B)
select '销售收入' xm,sum(bywc) bywc,sum(bnwc) bnwc
from temp1 a left jion temp2 b on a.wlmc=b.wlmc
和统计销售额
with
temp1 as (select wlmc A.xse bywc from biao A),
temp2 as (select wlmc b.xse bnwc from biao B)
select '销售额 xm,sum(bywc) bywc,sum(bnwc) bnwc
from temp1 a left jion temp2 b on a.wlmc=b.wlmc

我想把以上的两个统计结果显示到一个表中,结果如下:
xm bywc bnwc
 
多人接受答案了。
 
顶部