我只想按照t1.id分组,不想要其他的条件
时间: 2024-02-23 20:59:00 浏览: 84
如果您只想按照`t1.id`进行分组,并且不需要其他的条件,可以将查询字段和`GROUP BY`子句中只包含`t1.id`:
```
SELECT t1.id, max(t3.imagefileid) AS imagefileid
FROM uf_wdcdzsxbd t1
JOIN docdetail t2 ON t1.tm = t2.id
JOIN docimagefile t3 ON t2.id = t3.docid
WHERE t1.id IN (3, 2, 1)
GROUP BY t1.id
ORDER BY CASE t1.id
WHEN 3 THEN 1
WHEN 2 THEN 2
WHEN 1 THEN 3
END;
```
在这个查询中,我们只包含`t1.id`和`max(t3.imagefileid)`两个查询字段,并且在`GROUP BY`子句中只包含`t1.id`。这样,查询结果就只会按照`t1.id`进行分组,并且不需要其他的条件。
相关问题
SELECT t1.id, t2.DOCSUBJECT, max(t3.imagefileid) AS imagefileid FROM uf_wdcdzsxbd t1 JOIN docdetail t2 ON t1.tm = t2.id JOIN docimagefile t3 ON t2.id = t3.docid WHERE t1.id IN (3, 2, 1) GROUP BY t1.id ORDER BY CASE t1.id WHEN 3 THEN 1 WHEN 2 THEN 2 WHEN 1 THEN 3 END;这句sql会报错,原因时group by中没有包含全部的查询字段,如何修改
如果您的SQL查询语句报错,而错误的原因是因为`GROUP BY`中没有包含全部的查询字段,可以按照以下步骤进行修改:
1. 将查询字段`imagefileid`添加到`GROUP BY`子句中,以便让Oracle知道需要按照哪个字段进行分组:
```
SELECT t1.id, t2.DOCSUBJECT, max(t3.imagefileid) AS imagefileid
FROM uf_wdcdzsxbd t1
JOIN docdetail t2 ON t1.tm = t2.id
JOIN docimagefile t3 ON t2.id = t3.docid
WHERE t1.id IN (3, 2, 1)
GROUP BY t1.id, t2.DOCSUBJECT, t3.imagefileid
ORDER BY CASE t1.id
WHEN 3 THEN 1
WHEN 2 THEN 2
WHEN 1 THEN 3
END;
```
2. 如果您不希望将查询结果按照`imagefileid`进行分组,而只想按照`t1.id`进行分组,则可以使用Oracle中的聚合函数`LISTAGG`来将`imagefileid`的值按照逗号分隔符进行合并:
```
SELECT t1.id, t2.DOCSUBJECT, LISTAGG(t3.imagefileid, ',') WITHIN GROUP (ORDER BY t3.imagefileid DESC) AS imagefileid_list
FROM uf_wdcdzsxbd t1
JOIN docdetail t2 ON t1.tm = t2.id
JOIN docimagefile t3 ON t2.id = t3.docid
WHERE t1.id IN (3, 2, 1)
GROUP BY t1.id, t2.DOCSUBJECT
ORDER BY CASE t1.id
WHEN 3 THEN 1
WHEN 2 THEN 2
WHEN 1 THEN 3
END;
```
在这个查询中,我们使用`LISTAGG`函数将`imagefileid`的值按照逗号分隔符进行合并,并将结果命名为`imagefileid_list`。我们还使用`WITHIN GROUP`子句来指定按照`imagefileid`进行排序,并将排序结果合并到一个字符串中。最后,我们将`imagefileid_list`添加到`SELECT`子句中,以便让Oracle返回合并后的结果。
SELECT t1.id, t1.[requestId] AS t1_requestId, t1.[gf] AS '供方', t1.[htmc] AS '合同名称', t1.[htbh] AS '合同编号', t1.[sjbh] AS '编号', t1.[qdrq] AS '签订日期', t1.[qsrq] AS '起始日期', t1.[jzrq] AS '截止日期', t1.[zje] AS '总金额', t1.[zds] AS '总吨数', t1.[rksj] AS '入库时间', t1.[chpt] AS '撮合平台', t1.[qsfs] AS '签署方式', t1.[gfjc] AS '供方简称', t1.[cclx] AS '持仓类型', CAST ( STUFF( ( SELECT ',' + CONVERT ( VARCHAR, CASE WHEN ISNUMERIC( REPLACE( t2.hsdj, ',', '' ) ) = 1 THEN CAST ( REPLACE( t2.hsdj, ',', '' ) AS FLOAT ) END ) FROM formtable_main_43_dt1 t2 WHERE t1.id = t2.mainid FOR XML PATH ( '' ) ), 1, 1, '' ) AS FLOAT ) AS '采购单价' FROM formtable_main_43 t1 INNER JOIN formtable_main_43_dt1 t2 ON t1.id = t2.mainid GROUP BY t1.id, t1.[requestId], t1.[gf], t1.[htmc], t1.[htbh], t1.[sjbh], t1.[qdrq], t1.[qsrq], t1.[jzrq], t1.[zje], t1.[zds], t1.[rksj], t1.[chpt], t1.[qsfs], t1.[gfjc], t1.[cclx], '采购单价';提示每个 GROUP BY 表达式必须至少包含一个不是外部引用的列
根据你提供的 SQL 语句和错误提示,应该是 `GROUP BY` 子句中的问题。在 `GROUP BY` 子句中,只能引用 `SELECT` 列表中的列或者是聚合函数,而不能直接引用字符串常量。你需要将 `'采购单价'` 替换为 `t2.hsdj`,即:
```
SELECT t1.id, t1.[requestId] AS t1_requestId, t1.[gf] AS '供方', t1.[htmc] AS '合同名称', t1.[htbh] AS '合同编号', t1.[sjbh] AS '编号', t1.[qdrq] AS '签订日期', t1.[qsrq] AS '起始日期', t1.[jzrq] AS '截止日期', t1.[zje] AS '总金额', t1.[zds] AS '总吨数', t1.[rksj] AS '入库时间', t1.[chpt] AS '撮合平台', t1.[qsfs] AS '签署方式', t1.[gfjc] AS '供方简称', t1.[cclx] AS '持仓类型', CAST ( STUFF( ( SELECT ',' + CONVERT ( VARCHAR, CASE WHEN ISNUMERIC( REPLACE( t2.hsdj, ',', '' ) ) = 1 THEN CAST ( REPLACE( t2.hsdj, ',', '' ) AS FLOAT ) END ) FROM formtable_main_43_dt1 t2 WHERE t1.id = t2.mainid FOR XML PATH ( '' ) ), 1, 1, '' ) AS FLOAT ) AS '采购单价' FROM formtable_main_43 t1 INNER JOIN formtable_main_43_dt1 t2 ON t1.id = t2.mainid GROUP BY t1.id, t1.[requestId], t1.[gf], t1.[htmc], t1.[htbh], t1.[sjbh], t1.[qdrq], t1.[qsrq], t1.[jzrq], t1.[zje], t1.[zds], t1.[rksj], t1.[chpt], t1.[qsfs], t1.[gfjc], t1.[cclx], t2.hsdj;
```
这样就可以正确地对查询结果进行分组。
阅读全文