是否可以从同一个子查询中进行多项选择?这是我想构建的示例查询,但无法弄清楚如何去做(我意识到查询不起作用,我只是写它来传达我正在尝试做的事情):
SELECT
(SUM(my_column) FROM stats WHERE condition = 404 AND my_id IN (t) as total),
(SUM(my_column) FROM stats WHERE condition = 404 AND condition2 <> 404 AND my_id IN (t) as total2),
SELECT my_id
FROM
(
SELECT id AS my_id
FROM stats
JOIN my_object ON stats.my_id = my_object.id
WHERE stats.condition = 404
AND my_object.created_at >= '2013-08-03 04:00:00.000000' AND my_object.created_at IS NOT NULL
AND condition2 <> 404 GROUP BY my_id
) AS t
我在 postgres 工作,但更喜欢 ANSI SQL 解决方案。
结果数据:
id total total2
1 5 NULL
2 NULL 6
3 6 9
4 1 1235
谢谢!