【问题标题】:how to add new column in runtime depending upon the values in other table.? [duplicate]如何根据其他表中的值在运行时添加新列。? [复制]
【发布时间】:2014-02-28 01:11:30
【问题描述】:

我想在选择查询中显示新列,该列将包含其他表中与 id 对应的值的总和。 我现在正在学习 Sql,所以只想知道如何去做,我的问题被进一步解释为。

【问题讨论】:

标签: sql select runtime case


【解决方案1】:

SELECT table2.t1_id,table1.name,
sum(case when month='Jan' then amount else 0 end) as jan,
sum(case when month='feb' then amount else 0 end) as feb,
sum(case when month='Mar' then amount else 0 end) as Mar
from table2 inner
join table1 on table1.t1_id=table2.t1_id group by table2.t1_id,name
order by table2.t1_id;

【讨论】:

    猜你喜欢
    • 2022-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-09
    • 1970-01-01
    相关资源
    最近更新 更多