【问题标题】:how to add Columns to a table by performing aggregate function on the field of other function?如何通过对其他函数的字段执行聚合函数来将列添加到表中?
【发布时间】:2013-05-01 04:47:21
【问题描述】:

表的表结构如下:
1) MainTable(日期,CC,BU,金额,模式)
2) 表2(日期,CC,BU,column1,column2,column3,column4)

在这里,
表 2

column1 = 从 MainTable WHERE CC= 'value1' 和 Mode LIKE '011' 中选择 SUM(Amount);
column2 = 从 MainTable WHERE CC= 'value1' 和 Mode LIKE '012' 中选择 SUM(Amount);
column3 = 从 MainTable WHERE CC= 'value1' 和 Mode LIKE '10_' 中选择 SUM(Amount);
column4 = 从 MainTable WHERE CC= 'value1' 和 Mode LIKE '03_' 中选择 SUM(Amount);
日期 = MainTable 中的日期值

我设法获得 column1,column2,column3,column4 的值
但是如何为那些聚合函数获取 Date1 的值。

请帮帮我!!!提前谢谢你!!!

【问题讨论】:

  • 你想要哪条记录的日期?你能说得更具体一些,以便我回答你的问题
  • 作为评论 - 我不肯定,但如果您要进行完全匹配,请使用等号 (=) 而不是喜欢。当你做 Like '011' 时,它本质上是在说 = '011',我认为 equals 更快。

标签: c# database sql-server-2008 aggregate-functions


【解决方案1】:

您可以按日期分组...

示例: column1 = select Date, SUM(Amount) from MainTable GROUP BY Date HAVING CC= 'value1' and Mode LIKE '011';

【讨论】:

    猜你喜欢
    • 2020-09-27
    • 1970-01-01
    • 2021-12-20
    • 2021-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多