2014-06-06 63 views
0
table1 
no | date | 
J001 | 06 June | 

table2 
no | code | qty | /// AVGprice | Total 
J001 | B001 | 5 | /// 1500  | 7500 
J001 | B003 | 7 | /// 1000  | 7000 

table3         table4 
code | name  | AVGPrice   no | code | Price 
B001 | procc  | 1500    M001 | B001 | 1000 
B002 | motherboard | 2000    M001 | B002 | 2000 
B003 | VGA card | 1000    M002 | B001 | 2000 
             M002 | B003 | 1000 

我得到AVGprice从该查询如何从其他表中获取数据并将其显示出来?

select t.code, t.name, t.avg 
from (select table3.code, table3.name, (
     select avg(table4.price) 
     from table4 
     where table4.code=table3.code)as 'avg' 
     from table3 
)as t 

结果,我可以做是

no | date | Info 
J001| 06 June | ABCDEFG 

这些查询

select t.no, t.date, t.info 
from (select table1.no, table1.date, 'ABCDEFG' as info 
     from table1 
    )as t 

结果是我要的是

no | date | Info  | Total 
J001| 06 June | ABCDEFG | 14500 --> from sum of Total 

我不知道从哪里把我的平均查询,如何总结这...

回答

0

下应该添加你需要拉的平均子查询,我添加了另一个列 给你平均值的总和。

select t.no, 
t.date, 
t.info, 
(select avg(table4.price) 
     from table4 
     where table4.code=table3.code)as 'avg', 
sum(avg) 
from (select table1.no, table1.date, 'ABCDEFG' as info 
     from table1 
    )as t 
group by t.no, t.date, t.info 
+0

我添加了平均值的总和,但这可能不是你想要的。你可以把它改成你想在那里总结的内容。 –

+0

未知的列表3 ....我应该把它 - >从表4,表3?并且总和...它与第一avg 1500显示相同,不像2500其中B001和B003的avgprice的总和 – user3651460

相关问题