如何在sql中乘以count中的数字字段

时间:2017-04-12 09:58:51

标签: sql count

我尝试像这样的查询

Select node.weight * count(ans.cn_gender) as count, ans.cn_gender , poll.pollname
From report.cm_marital_satisfaction ans Join
     poll_management.POLLES poll
     on ans.cn_pollId = poll.id Join
     poll_management.POSITIONS node
     on ans.cn_positionId = node.id
Where ans.cn_pollId in (113)
group by ans.cn_gender, poll.pollname

但是我有错误:ERROR:column" node.weight"必须出现在GROUP BY子句中或用于聚合函数 第1行:选择node.weight * count(ans.cn_gender)作为count,ans.cn_g ...                ^ **********错误**********

错误:列" node.weight"必须出现在GROUP BY子句中或用于聚合函数 SQL状态:42803 性格:8

但我不想在GROUP BY中使用node.weight 请帮帮我

3 个答案:

答案 0 :(得分:1)

使用具有输出的功能,例如“ MAX()”或“ MIN()”:

//TarGzWrite function same as above....
h := new(tar.Header)
//New code after this..
lastIndex := strings.LastIndex(_path, "/tmp")
fmt.Println("String is ", _path, "Last index is", lastIndex)
var name string
if lastIndex > 0 {
    name = _path[lastIndex+len("/tmp")+1:]
    fmt.Println("Got name:", name)
} else {
    //This would not be needed, but was there just for testing my code
    name = _path
}
// h.Name = _path
h.Name = name
h.Size = fi.Size()
h.Mode = int64(fi.Mode())

答案 1 :(得分:0)

使用派生表/临时表来获取带有外键的COUNT(ans.cn_gender),然后将其连接到该表以进行乘法运算。这样,您将乘以数字而不是聚合函数。

答案 2 :(得分:0)

使用sum()代替count()

Select sum(node.weight) as count, ans.cn_gender, poll.pollname
From report.cm_marital_satisfaction ans Join
     poll_management.POLLES poll
     on ans.cn_pollId = poll.id Join
     poll_management.POSITIONS node
     on ans.cn_positionId = node.id
Where ans.cn_pollId in (113)
group by ans.cn_gender, poll.pollname
相关问题