【问题标题】:how to get data from other table to one table and show it?如何从另一张表中获取数据到一张表中并显示出来?
【发布时间】:2014-06-06 18:11:04
【问题描述】:
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

我不知道将我的平均查询放在哪里以及如何对其求和...

【问题讨论】:

    标签: select sum case average


    【解决方案1】:

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

    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
    

    【讨论】:

    • 我添加了平均值的总和,但这可能不是您想要的。您可以将其更改为您想要总结的内容。
    • 未知列 table3....我应该把它放在 --> 来自 table4,table3 吗?和总和...它与第一个 avg 1500 显示相同,不像 2500,其中 B001 和 B003 的 avgprice 总和
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多