如果年份存在,即使在某个月份不存在账单,也要填写四个学期
if year exists fill with the four trimester even if not exists bills in some month
正如我在标题中所说,我想显示按学期和年份分组的所有总和(账单),但如果一个学期中没有账单,则显示为:Trimester 2 amount = 0
我的查询(我正在尝试的):
select * from (
select "year",'1er Trimestre' as "Trimestre",COALESCE(sum("base"),0) as "base",COALESCE(sum("iva"),0) as "iva",COALESCE(sum("total"),0) as "total" from (Select Year("p_fpagado") as "year",month("p_fpagado") as "month",COALESCE(sum("im_base"),0) as "base",COALESCE(sum("im_calculado"),0) as "iva",COALESCE(sum("im_total"),0) as "total" from "Facturas" where "p_pagado" = True and month("p_fpagado") between 1 and 3 group by Year("p_fpagado"),month("p_fpagado")) group by "year"
union all
select "year",'2º Trimestre' as "Trimestre",COALESCE(sum("base"),0) as "base",COALESCE(sum("iva"),0) as "iva",COALESCE(sum("total"),0) as "total" from (Select Year("p_fpagado") as "year",month("p_fpagado") as "month",COALESCE(sum("im_base"),0) as "base",COALESCE(sum("im_calculado"),0) as "iva",COALESCE(sum("im_total"),0) as "total" from "Facturas" where "p_pagado" = True and month("p_fpagado") between 4 and 6 group by Year("p_fpagado"),month("p_fpagado")) group by "year"
union all
select "year",'3er Trimestre' as "Trimestre",COALESCE(sum("base"),0) as "base",COALESCE(sum("iva"),0) as "iva",COALESCE(sum("total"),0) as "total" from (Select Year("p_fpagado") as "year",month("p_fpagado") as "month",COALESCE(sum("im_base"),0) as "base",COALESCE(sum("im_calculado"),0) as "iva",COALESCE(sum("im_total"),0) as "total" from "Facturas" where "p_pagado" = True and month("p_fpagado") between 7 and 9 group by Year("p_fpagado"),month("p_fpagado")) group by "year"
union all
select "year",'4º Trimestre' as "Trimestre",COALESCE(sum("base"),0) as "base",COALESCE(sum("iva"),0) as "iva",COALESCE(sum("total"),0) as "total" from (Select Year("p_fpagado") as "year",month("p_fpagado") as "month",COALESCE(sum("im_base"),0) as "base",COALESCE(sum("im_calculado"),0) as "iva",COALESCE(sum("im_total"),0) as "total" from "Facturas" where "p_pagado" = True and month("p_fpagado") between 10 and 12 group by Year("p_fpagado"),month("p_fpagado")) group by "year"
)
order by 1 desc, 2 asc
我得到的结果是这样的:
year Trimestre base iva total
2017 1er Trimestre 101 23 124
2016 1er Trimestre 10 2.1 12.1
2016 2º Trimestre 30 6.3 36.3
2016 3er Trimestre 10 2.1 12.1
2016 4º Trimestre 20 4.2 24.1
而我真正想要的是:
year Trimestre base iva total
2017 1er Trimestre 101 23 124
2017 2º Trimestre 0 0 0
2017 3er Trimestre 0 0 0
2017 4º Trimestre 0 0 0
2016 1er Trimestre 10 2.1 12.1
2016 2º Trimestre 30 6.3 36.3
2016 3er Trimestre 10 2.1 12.1
2016 4º Trimestre 20 4.2 24.1
感谢阅读,期待您的帮助:)
提示:1er Trimestre = 第一个三个月,2º Trimestre = 第二个三个月,3er Trimestre = 第三个三个月和 4º Trimestre = 第四个三个月。
也试过这个:
select * from (
select "year",'1er Trimestre' as "Trimestre",COALESCE(sum("base"),0) as "base",COALESCE(sum("iva"),0) as "iva",COALESCE(sum("total"),0) as "total" from (Select Year("p_fpagado") as "year",month("p_fpagado") as "month",COALESCE(sum("im_base"),0) as "base",COALESCE(sum("im_calculado"),0) as "iva",COALESCE(sum("im_total"),0) as "total" from "Facturas" where "p_pagado" = True and month("p_fpagado") between 1 and 3 group by Year("p_fpagado"),month("p_fpagado")) group by "year"
union all
select "year",'2º Trimestre' as "Trimestre",COALESCE(sum("base"),0) as "base",COALESCE(sum("iva"),0) as "iva",COALESCE(sum("total"),0) as "total" from (Select Year("p_fpagado") as "year",month("p_fpagado") as "month",COALESCE(sum("im_base"),0) as "base",COALESCE(sum("im_calculado"),0) as "iva",COALESCE(sum("im_total"),0) as "total" from "Facturas" where "p_pagado" = True and month("p_fpagado") between 4 and 6 group by Year("p_fpagado"),month("p_fpagado")) group by "year"
union all
select "year",'3er Trimestre' as "Trimestre",COALESCE(sum("base"),0) as "base",COALESCE(sum("iva"),0) as "iva",COALESCE(sum("total"),0) as "total" from (Select Year("p_fpagado") as "year",month("p_fpagado") as "month",COALESCE(sum("im_base"),0) as "base",COALESCE(sum("im_calculado"),0) as "iva",COALESCE(sum("im_total"),0) as "total" from "Facturas" where "p_pagado" = True and month("p_fpagado") between 7 and 9 group by Year("p_fpagado"),month("p_fpagado")) group by "year"
union all
select "year",'4º Trimestre' as "Trimestre",COALESCE(sum("base"),0) as "base",COALESCE(sum("iva"),0) as "iva",COALESCE(sum("total"),0) as "total" from (Select Year("p_fpagado") as "year",month("p_fpagado") as "month",COALESCE(sum("im_base"),0) as "base",COALESCE(sum("im_calculado"),0) as "iva",COALESCE(sum("im_total"),0) as "total" from "Facturas" where "p_pagado" = True and month("p_fpagado") between 10 and 12 group by Year("p_fpagado"),month("p_fpagado")) group by "year"
) as "datos" right outer join (select distinct '1er Trimestre' as "Trimestre" from "Facturas"
union all
select distinct '2º Trimestre' as "Trimestre" from "Facturas"
union all
select distinct '3er Trimestre' as "Trimestre" from "Facturas"
union all
select distinct '4º Trimestre' as "Trimestre" from "Facturas" ) as "trimestres"
on "datos"."Trimestre" = "trimestres"."Trimestre"
order by 1 desc, 2 asc
试试这个...
;WITH cteYears
AS
(
SELECT [year] = 2016
UNION ALL SELECT [year] = 2017
),
cteTrimestres
AS
(
SELECT TID = 1, Trimestre = '1er Trimestre'
UNION ALL SELECT 2, '2º Trimestre'
UNION ALL SELECT 3, '3er Trimestre'
UNION ALL SELECT 4, '4º Trimestre'
),
cteDataToAggregate
AS
(
SELECT [year] = 2017, TID = 1, base = 101, iva = 23, total = 124
UNION ALL SELECT 2016, TID = 1, 10, 2.1, 12.1
UNION ALL SELECT 2016, TID = 2, 30, 6.3, 36.3
UNION ALL SELECT 2016, TID = 3, 10, 2.1, 12.1
UNION ALL SELECT 2016, TID = 4, 20, 4.2, 24.1
),
cteDateRange
AS
(
SELECT TID, [year], Trimestre
FROM cteYears
CROSS JOIN cteTrimestres
)
SELECT dr.[year],
dr.Trimestre,
base = ISNULL(SUM(a.base), 0),
iva = ISNULL(SUM(a.iva), 0),
total = ISNULL(SUM(a.total), 0)
FROM cteDateRange AS dr
LEFT OUTER JOIN cteDataToAggregate AS a
ON dr.[year] = a.[year]
AND dr.TID = a.TID
GROUP BY dr.[year],
dr.Trimestre
ORDER BY [year] DESC,
Trimestre;
这里有相同的逻辑,但没有 Common Table 表达式...
SELECT cteDateRange.[year],
cteDateRange.Trimestre,
base = ISNULL(SUM(cteDataToAggregate.base), 0),
iva = ISNULL(SUM(cteDataToAggregate.iva), 0),
total = ISNULL(SUM(cteDataToAggregate.total), 0)
FROM (
SELECT TID, [year], Trimestre
FROM (
SELECT [year] = 2016
UNION ALL SELECT [year] = 2017
) AS cteYears
CROSS JOIN (
SELECT TID = 1, Trimestre = '1er Trimestre'
UNION ALL SELECT 2, '2º Trimestre'
UNION ALL SELECT 3, '3er Trimestre'
UNION ALL SELECT 4, '4º Trimestre'
) AS cteTrimestres
) AS cteDateRange
LEFT OUTER JOIN (
SELECT [year] = 2017, TID = 1, base = 101, iva = 23, total = 124
UNION ALL SELECT 2016, TID = 1, 10, 2.1, 12.1
UNION ALL SELECT 2016, TID = 2, 30, 6.3, 36.3
UNION ALL SELECT 2016, TID = 3, 10, 2.1, 12.1
UNION ALL SELECT 2016, TID = 4, 20, 4.2, 24.1
) AS cteDataToAggregate
ON cteDateRange.[year] = cteDataToAggregate.[year]
AND cteDateRange.TID = cteDataToAggregate.TID
GROUP BY cteDateRange.[year],
cteDateRange.Trimestre
ORDER BY [year] DESC,
Trimestre
终于成功了!:
select "year","Trimestre",COALESCE(sum("base"),0) as "base",COALESCE(sum("iva"),0) as "iva",COALESCE(sum("total"),0) as "total" from (
select "year",'1er Trimestre' as "Trimestre",COALESCE(sum("base"),0) as "base",COALESCE(sum("iva"),0) as "iva",COALESCE(sum("total"),0) as "total" from (Select Year("p_fpagado") as "year",month("p_fpagado") as "month",COALESCE(sum("im_base"),0) as "base",COALESCE(sum("im_calculado"),0) as "iva",COALESCE(sum("im_total"),0) as "total" from "Facturas" where "p_pagado" = True and month("p_fpagado") between 1 and 3 group by Year("p_fpagado"),month("p_fpagado")) group by "year"
union all
select "year",'2º Trimestre' as "Trimestre",COALESCE(sum("base"),0) as "base",COALESCE(sum("iva"),0) as "iva",COALESCE(sum("total"),0) as "total" from (Select Year("p_fpagado") as "year",month("p_fpagado") as "month",COALESCE(sum("im_base"),0) as "base",COALESCE(sum("im_calculado"),0) as "iva",COALESCE(sum("im_total"),0) as "total" from "Facturas" where "p_pagado" = True and month("p_fpagado") between 4 and 6 group by Year("p_fpagado"),month("p_fpagado")) group by "year"
union all
select "year",'3er Trimestre' as "Trimestre",COALESCE(sum("base"),0) as "base",COALESCE(sum("iva"),0) as "iva",COALESCE(sum("total"),0) as "total" from (Select Year("p_fpagado") as "year",month("p_fpagado") as "month",COALESCE(sum("im_base"),0) as "base",COALESCE(sum("im_calculado"),0) as "iva",COALESCE(sum("im_total"),0) as "total" from "Facturas" where "p_pagado" = True and month("p_fpagado") between 7 and 9 group by Year("p_fpagado"),month("p_fpagado")) group by "year"
union all
select "year",'4º Trimestre' as "Trimestre",COALESCE(sum("base"),0) as "base",COALESCE(sum("iva"),0) as "iva",COALESCE(sum("total"),0) as "total" from (Select Year("p_fpagado") as "year",month("p_fpagado") as "month",COALESCE(sum("im_base"),0) as "base",COALESCE(sum("im_calculado"),0) as "iva",COALESCE(sum("im_total"),0) as "total" from "Facturas" where "p_pagado" = True and month("p_fpagado") between 10 and 12 group by Year("p_fpagado"),month("p_fpagado")) group by "year"
union all
(select distinct year("p_fpagado") as "year",'1er Trimestre' as "Trimestre",0 as "base",0 as "iva",0 as "total" from "Facturas" having year("p_fpagado") != 0
union all
select distinct year("p_fpagado") as "year",'2º Trimestre' as "Trimestre",0 as "base",0 as "iva",0 as "total" from "Facturas" having year("p_fpagado") != 0
union all
select distinct year("p_fpagado") as "year",'3er Trimestre' as "Trimestre",0 as "base",0 as "iva",0 as "total" from "Facturas" having year("p_fpagado") != 0
union all
select distinct year("p_fpagado") as "year",'4º Trimestre' as "Trimestre",0 as "base",0 as "iva",0 as "total" from "Facturas" having year("p_fpagado") != 0)
) group by "year","Trimestre" order by "year" desc,"Trimestre" asc
我又做了一个 "Union all" 然后是:
(select distinct year("p_fpagado") as "year",'1er Trimestre' as "Trimestre",0 as "base",0 as "iva",0 as "total" from "Facturas" having year("p_fpagado") != 0
union all
select distinct year("p_fpagado") as "year",'2º Trimestre' as "Trimestre",0 as "base",0 as "iva",0 as "total" from "Facturas" having year("p_fpagado") != 0
union all
select distinct year("p_fpagado") as "year",'3er Trimestre' as "Trimestre",0 as "base",0 as "iva",0 as "total" from "Facturas" having year("p_fpagado") != 0
union all
select distinct year("p_fpagado") as "year",'4º Trimestre' as "Trimestre",0 as "base",0 as "iva",0 as "total" from "Facturas" having year("p_fpagado") != 0)
)
这是 table 中存在的所有年份的 table 我想要获取值的地方。
然后我用“()”将所有内容分组,并从那里制作为 table 到 select --> “()” 并 select 编辑了以下行:
select "year","Trimestre",COALESCE(sum("base"),0) as "base",COALESCE(sum("iva"),0) as "iva",COALESCE(sum("total"),0) as "total"
我明白了。感谢大家的帮助!
希望这个回答对遇到类似问题的人有所帮助。祝你有美好的一天:)
这不是一个完整的答案,因为您没有提供示例 table 数据。您可以自己制定详细信息。 HSQLDB 具有许多功能,您可以使用它们来避免冗长而复杂的查询。
YEAR(date) 和QUARTER(date) 函数可用于提取日期的各个部分以进行分组。
如果数据为空,可以使用 UNNEST 关键字和 SEQUENCE_ARRAY(start, end, interval) 函数来生成季度。
第一部分是 table 宿舍列表:
SELECT YEAR(D) "y", QUARTER(D) "q", CASE QUARTER(D) WHEN 1 THEN '1st' WHEN 2 THEN '2nd' WHEN 3 THEN '3rd' WHEN 4 THEN '4th' END CASE "qname"
FROM UNNEST(SEQUENCE_ARRAY(DATE'2016-01-01', DATE'2017-12-31' , 3 MONTH)) AS T(D)
第二部分是一个更简单的查询,用于获取没有标签的所有总数和计数:
SELECT YEAR("p_fpagado") as "year", QUARTER("p_fpagado") as "quarter",
COALESCE(sum("im_base"),0) as "base",COALESCE(sum("im_calculado"),0) as "iva",
COALESCE(sum("im_total"),0) as "total" from "Facturas" WHERE "p_pagado" = TRUE
GROUP BY YEAR("p_fpagado"),QUARTER("p_fpagado")
然后您需要将您的 table 左联接到宿舍列表中,并将所需的列添加到 SELECT 列表中:
WITH list_of_quarters AS (SELECT ....), calculated_data AS (SELECT ....)
SELECT "y", "qname", .... FROM list_of_quarters LEFT JOIN calculated_data ON (list_of_quarters."y" = calculated_data."year" AND list_of_quarters."q" = calculated_data."quarter")
正如我在标题中所说,我想显示按学期和年份分组的所有总和(账单),但如果一个学期中没有账单,则显示为:Trimester 2 amount = 0
我的查询(我正在尝试的):
select * from (
select "year",'1er Trimestre' as "Trimestre",COALESCE(sum("base"),0) as "base",COALESCE(sum("iva"),0) as "iva",COALESCE(sum("total"),0) as "total" from (Select Year("p_fpagado") as "year",month("p_fpagado") as "month",COALESCE(sum("im_base"),0) as "base",COALESCE(sum("im_calculado"),0) as "iva",COALESCE(sum("im_total"),0) as "total" from "Facturas" where "p_pagado" = True and month("p_fpagado") between 1 and 3 group by Year("p_fpagado"),month("p_fpagado")) group by "year"
union all
select "year",'2º Trimestre' as "Trimestre",COALESCE(sum("base"),0) as "base",COALESCE(sum("iva"),0) as "iva",COALESCE(sum("total"),0) as "total" from (Select Year("p_fpagado") as "year",month("p_fpagado") as "month",COALESCE(sum("im_base"),0) as "base",COALESCE(sum("im_calculado"),0) as "iva",COALESCE(sum("im_total"),0) as "total" from "Facturas" where "p_pagado" = True and month("p_fpagado") between 4 and 6 group by Year("p_fpagado"),month("p_fpagado")) group by "year"
union all
select "year",'3er Trimestre' as "Trimestre",COALESCE(sum("base"),0) as "base",COALESCE(sum("iva"),0) as "iva",COALESCE(sum("total"),0) as "total" from (Select Year("p_fpagado") as "year",month("p_fpagado") as "month",COALESCE(sum("im_base"),0) as "base",COALESCE(sum("im_calculado"),0) as "iva",COALESCE(sum("im_total"),0) as "total" from "Facturas" where "p_pagado" = True and month("p_fpagado") between 7 and 9 group by Year("p_fpagado"),month("p_fpagado")) group by "year"
union all
select "year",'4º Trimestre' as "Trimestre",COALESCE(sum("base"),0) as "base",COALESCE(sum("iva"),0) as "iva",COALESCE(sum("total"),0) as "total" from (Select Year("p_fpagado") as "year",month("p_fpagado") as "month",COALESCE(sum("im_base"),0) as "base",COALESCE(sum("im_calculado"),0) as "iva",COALESCE(sum("im_total"),0) as "total" from "Facturas" where "p_pagado" = True and month("p_fpagado") between 10 and 12 group by Year("p_fpagado"),month("p_fpagado")) group by "year"
)
order by 1 desc, 2 asc
我得到的结果是这样的:
year Trimestre base iva total
2017 1er Trimestre 101 23 124
2016 1er Trimestre 10 2.1 12.1
2016 2º Trimestre 30 6.3 36.3
2016 3er Trimestre 10 2.1 12.1
2016 4º Trimestre 20 4.2 24.1
而我真正想要的是:
year Trimestre base iva total
2017 1er Trimestre 101 23 124
2017 2º Trimestre 0 0 0
2017 3er Trimestre 0 0 0
2017 4º Trimestre 0 0 0
2016 1er Trimestre 10 2.1 12.1
2016 2º Trimestre 30 6.3 36.3
2016 3er Trimestre 10 2.1 12.1
2016 4º Trimestre 20 4.2 24.1
感谢阅读,期待您的帮助:)
提示:1er Trimestre = 第一个三个月,2º Trimestre = 第二个三个月,3er Trimestre = 第三个三个月和 4º Trimestre = 第四个三个月。
也试过这个:
select * from (
select "year",'1er Trimestre' as "Trimestre",COALESCE(sum("base"),0) as "base",COALESCE(sum("iva"),0) as "iva",COALESCE(sum("total"),0) as "total" from (Select Year("p_fpagado") as "year",month("p_fpagado") as "month",COALESCE(sum("im_base"),0) as "base",COALESCE(sum("im_calculado"),0) as "iva",COALESCE(sum("im_total"),0) as "total" from "Facturas" where "p_pagado" = True and month("p_fpagado") between 1 and 3 group by Year("p_fpagado"),month("p_fpagado")) group by "year"
union all
select "year",'2º Trimestre' as "Trimestre",COALESCE(sum("base"),0) as "base",COALESCE(sum("iva"),0) as "iva",COALESCE(sum("total"),0) as "total" from (Select Year("p_fpagado") as "year",month("p_fpagado") as "month",COALESCE(sum("im_base"),0) as "base",COALESCE(sum("im_calculado"),0) as "iva",COALESCE(sum("im_total"),0) as "total" from "Facturas" where "p_pagado" = True and month("p_fpagado") between 4 and 6 group by Year("p_fpagado"),month("p_fpagado")) group by "year"
union all
select "year",'3er Trimestre' as "Trimestre",COALESCE(sum("base"),0) as "base",COALESCE(sum("iva"),0) as "iva",COALESCE(sum("total"),0) as "total" from (Select Year("p_fpagado") as "year",month("p_fpagado") as "month",COALESCE(sum("im_base"),0) as "base",COALESCE(sum("im_calculado"),0) as "iva",COALESCE(sum("im_total"),0) as "total" from "Facturas" where "p_pagado" = True and month("p_fpagado") between 7 and 9 group by Year("p_fpagado"),month("p_fpagado")) group by "year"
union all
select "year",'4º Trimestre' as "Trimestre",COALESCE(sum("base"),0) as "base",COALESCE(sum("iva"),0) as "iva",COALESCE(sum("total"),0) as "total" from (Select Year("p_fpagado") as "year",month("p_fpagado") as "month",COALESCE(sum("im_base"),0) as "base",COALESCE(sum("im_calculado"),0) as "iva",COALESCE(sum("im_total"),0) as "total" from "Facturas" where "p_pagado" = True and month("p_fpagado") between 10 and 12 group by Year("p_fpagado"),month("p_fpagado")) group by "year"
) as "datos" right outer join (select distinct '1er Trimestre' as "Trimestre" from "Facturas"
union all
select distinct '2º Trimestre' as "Trimestre" from "Facturas"
union all
select distinct '3er Trimestre' as "Trimestre" from "Facturas"
union all
select distinct '4º Trimestre' as "Trimestre" from "Facturas" ) as "trimestres"
on "datos"."Trimestre" = "trimestres"."Trimestre"
order by 1 desc, 2 asc
试试这个...
;WITH cteYears
AS
(
SELECT [year] = 2016
UNION ALL SELECT [year] = 2017
),
cteTrimestres
AS
(
SELECT TID = 1, Trimestre = '1er Trimestre'
UNION ALL SELECT 2, '2º Trimestre'
UNION ALL SELECT 3, '3er Trimestre'
UNION ALL SELECT 4, '4º Trimestre'
),
cteDataToAggregate
AS
(
SELECT [year] = 2017, TID = 1, base = 101, iva = 23, total = 124
UNION ALL SELECT 2016, TID = 1, 10, 2.1, 12.1
UNION ALL SELECT 2016, TID = 2, 30, 6.3, 36.3
UNION ALL SELECT 2016, TID = 3, 10, 2.1, 12.1
UNION ALL SELECT 2016, TID = 4, 20, 4.2, 24.1
),
cteDateRange
AS
(
SELECT TID, [year], Trimestre
FROM cteYears
CROSS JOIN cteTrimestres
)
SELECT dr.[year],
dr.Trimestre,
base = ISNULL(SUM(a.base), 0),
iva = ISNULL(SUM(a.iva), 0),
total = ISNULL(SUM(a.total), 0)
FROM cteDateRange AS dr
LEFT OUTER JOIN cteDataToAggregate AS a
ON dr.[year] = a.[year]
AND dr.TID = a.TID
GROUP BY dr.[year],
dr.Trimestre
ORDER BY [year] DESC,
Trimestre;
这里有相同的逻辑,但没有 Common Table 表达式...
SELECT cteDateRange.[year],
cteDateRange.Trimestre,
base = ISNULL(SUM(cteDataToAggregate.base), 0),
iva = ISNULL(SUM(cteDataToAggregate.iva), 0),
total = ISNULL(SUM(cteDataToAggregate.total), 0)
FROM (
SELECT TID, [year], Trimestre
FROM (
SELECT [year] = 2016
UNION ALL SELECT [year] = 2017
) AS cteYears
CROSS JOIN (
SELECT TID = 1, Trimestre = '1er Trimestre'
UNION ALL SELECT 2, '2º Trimestre'
UNION ALL SELECT 3, '3er Trimestre'
UNION ALL SELECT 4, '4º Trimestre'
) AS cteTrimestres
) AS cteDateRange
LEFT OUTER JOIN (
SELECT [year] = 2017, TID = 1, base = 101, iva = 23, total = 124
UNION ALL SELECT 2016, TID = 1, 10, 2.1, 12.1
UNION ALL SELECT 2016, TID = 2, 30, 6.3, 36.3
UNION ALL SELECT 2016, TID = 3, 10, 2.1, 12.1
UNION ALL SELECT 2016, TID = 4, 20, 4.2, 24.1
) AS cteDataToAggregate
ON cteDateRange.[year] = cteDataToAggregate.[year]
AND cteDateRange.TID = cteDataToAggregate.TID
GROUP BY cteDateRange.[year],
cteDateRange.Trimestre
ORDER BY [year] DESC,
Trimestre
终于成功了!:
select "year","Trimestre",COALESCE(sum("base"),0) as "base",COALESCE(sum("iva"),0) as "iva",COALESCE(sum("total"),0) as "total" from (
select "year",'1er Trimestre' as "Trimestre",COALESCE(sum("base"),0) as "base",COALESCE(sum("iva"),0) as "iva",COALESCE(sum("total"),0) as "total" from (Select Year("p_fpagado") as "year",month("p_fpagado") as "month",COALESCE(sum("im_base"),0) as "base",COALESCE(sum("im_calculado"),0) as "iva",COALESCE(sum("im_total"),0) as "total" from "Facturas" where "p_pagado" = True and month("p_fpagado") between 1 and 3 group by Year("p_fpagado"),month("p_fpagado")) group by "year"
union all
select "year",'2º Trimestre' as "Trimestre",COALESCE(sum("base"),0) as "base",COALESCE(sum("iva"),0) as "iva",COALESCE(sum("total"),0) as "total" from (Select Year("p_fpagado") as "year",month("p_fpagado") as "month",COALESCE(sum("im_base"),0) as "base",COALESCE(sum("im_calculado"),0) as "iva",COALESCE(sum("im_total"),0) as "total" from "Facturas" where "p_pagado" = True and month("p_fpagado") between 4 and 6 group by Year("p_fpagado"),month("p_fpagado")) group by "year"
union all
select "year",'3er Trimestre' as "Trimestre",COALESCE(sum("base"),0) as "base",COALESCE(sum("iva"),0) as "iva",COALESCE(sum("total"),0) as "total" from (Select Year("p_fpagado") as "year",month("p_fpagado") as "month",COALESCE(sum("im_base"),0) as "base",COALESCE(sum("im_calculado"),0) as "iva",COALESCE(sum("im_total"),0) as "total" from "Facturas" where "p_pagado" = True and month("p_fpagado") between 7 and 9 group by Year("p_fpagado"),month("p_fpagado")) group by "year"
union all
select "year",'4º Trimestre' as "Trimestre",COALESCE(sum("base"),0) as "base",COALESCE(sum("iva"),0) as "iva",COALESCE(sum("total"),0) as "total" from (Select Year("p_fpagado") as "year",month("p_fpagado") as "month",COALESCE(sum("im_base"),0) as "base",COALESCE(sum("im_calculado"),0) as "iva",COALESCE(sum("im_total"),0) as "total" from "Facturas" where "p_pagado" = True and month("p_fpagado") between 10 and 12 group by Year("p_fpagado"),month("p_fpagado")) group by "year"
union all
(select distinct year("p_fpagado") as "year",'1er Trimestre' as "Trimestre",0 as "base",0 as "iva",0 as "total" from "Facturas" having year("p_fpagado") != 0
union all
select distinct year("p_fpagado") as "year",'2º Trimestre' as "Trimestre",0 as "base",0 as "iva",0 as "total" from "Facturas" having year("p_fpagado") != 0
union all
select distinct year("p_fpagado") as "year",'3er Trimestre' as "Trimestre",0 as "base",0 as "iva",0 as "total" from "Facturas" having year("p_fpagado") != 0
union all
select distinct year("p_fpagado") as "year",'4º Trimestre' as "Trimestre",0 as "base",0 as "iva",0 as "total" from "Facturas" having year("p_fpagado") != 0)
) group by "year","Trimestre" order by "year" desc,"Trimestre" asc
我又做了一个 "Union all" 然后是:
(select distinct year("p_fpagado") as "year",'1er Trimestre' as "Trimestre",0 as "base",0 as "iva",0 as "total" from "Facturas" having year("p_fpagado") != 0
union all
select distinct year("p_fpagado") as "year",'2º Trimestre' as "Trimestre",0 as "base",0 as "iva",0 as "total" from "Facturas" having year("p_fpagado") != 0
union all
select distinct year("p_fpagado") as "year",'3er Trimestre' as "Trimestre",0 as "base",0 as "iva",0 as "total" from "Facturas" having year("p_fpagado") != 0
union all
select distinct year("p_fpagado") as "year",'4º Trimestre' as "Trimestre",0 as "base",0 as "iva",0 as "total" from "Facturas" having year("p_fpagado") != 0)
)
这是 table 中存在的所有年份的 table 我想要获取值的地方。
然后我用“()”将所有内容分组,并从那里制作为 table 到 select --> “()” 并 select 编辑了以下行:
select "year","Trimestre",COALESCE(sum("base"),0) as "base",COALESCE(sum("iva"),0) as "iva",COALESCE(sum("total"),0) as "total"
我明白了。感谢大家的帮助!
希望这个回答对遇到类似问题的人有所帮助。祝你有美好的一天:)
这不是一个完整的答案,因为您没有提供示例 table 数据。您可以自己制定详细信息。 HSQLDB 具有许多功能,您可以使用它们来避免冗长而复杂的查询。
YEAR(date) 和QUARTER(date) 函数可用于提取日期的各个部分以进行分组。
如果数据为空,可以使用 UNNEST 关键字和 SEQUENCE_ARRAY(start, end, interval) 函数来生成季度。
第一部分是 table 宿舍列表:
SELECT YEAR(D) "y", QUARTER(D) "q", CASE QUARTER(D) WHEN 1 THEN '1st' WHEN 2 THEN '2nd' WHEN 3 THEN '3rd' WHEN 4 THEN '4th' END CASE "qname"
FROM UNNEST(SEQUENCE_ARRAY(DATE'2016-01-01', DATE'2017-12-31' , 3 MONTH)) AS T(D)
第二部分是一个更简单的查询,用于获取没有标签的所有总数和计数:
SELECT YEAR("p_fpagado") as "year", QUARTER("p_fpagado") as "quarter",
COALESCE(sum("im_base"),0) as "base",COALESCE(sum("im_calculado"),0) as "iva",
COALESCE(sum("im_total"),0) as "total" from "Facturas" WHERE "p_pagado" = TRUE
GROUP BY YEAR("p_fpagado"),QUARTER("p_fpagado")
然后您需要将您的 table 左联接到宿舍列表中,并将所需的列添加到 SELECT 列表中:
WITH list_of_quarters AS (SELECT ....), calculated_data AS (SELECT ....)
SELECT "y", "qname", .... FROM list_of_quarters LEFT JOIN calculated_data ON (list_of_quarters."y" = calculated_data."year" AND list_of_quarters."q" = calculated_data."quarter")