postgresql 遇到数据库查询问题[已关闭]

3duebb1j  于 2022-11-29  发布在  PostgreSQL
关注(0)|答案(2)|浏览(130)

已关闭。此问题需要details or clarity。当前不接受答案。
**想要改进此问题吗?**通过editing this post添加详细信息并阐明问题。

3天前关闭。
Improve this question
如果我运行此查询:

select date, sum(orders) as "Total orders" 
from tbl 
where date >=1 and date <= 4 
group by date

如何获得屏幕截图中所示的预期输出?

k5ifujac

k5ifujac1#

对generate_series()使用左对齐:

select g.date, 
       sum(tbl.orders) as "Total orders" 
from generate_series(1,4) as g(date)
  left join tbl on tbl.date = g.date 
group by g.date
q5lcpyga

q5lcpyga2#

WITH cte as (

SELECT date, orders
FROM dbo.Orders

),

cte2 as (

select top (SELECT MAX(date) FROM cte) ROW_NUMBER() over(order by a.name) 
as date
from sys.all_objects a

) 

SELECT cte2.date, SUM(ISNULL(orders, 0))
FROM cte2 LEFT JOIN cte ON cte2.date = cte.date
GROUP BY cte2.date

相关问题