正如我在标题中所说的那样,我想显示按照三个月和一年分组的所有金额(账单),但如果在一个三个月的节目中没有账单:三个月金额= 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
答案 0 :(得分:1)
由于您未提供样本表数据,因此这不是一个完整的答案。您可以自己制定详细信息。 HSQLDB具有许多功能,可用于避免长而复杂的查询。
YEAR(日期)和QUARTER(日期)功能可用于提取分组日期的各个部分。
UNNEST关键字和SEQUENCE_ARRAY(开始,结束,间隔)功能可用于在数据为空的情况下生成季度。
第一部分是包含季度列表的表格:
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")
然后你需要将你的表左键连接到宿舍列表并将所需的列添加到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")
答案 1 :(得分:0)
试试这个......
;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;
答案 2 :(得分:0)
这是没有公用表格表达式的相同逻辑......
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
答案 3 :(得分:0)
这终于奏效了!:
(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)
)
我做了另一个“全联盟”,然后是:
select "year","Trimestre",COALESCE(sum("base"),0) as "base",COALESCE(sum("iva"),0) as "iva",COALESCE(sum("total"),0) as "total"
这是表格中存在的所有年份的表格,我想要获取值。
然后我用“()”分组并作为一个表从那里选择 - > “()”并选择以下行:
@Override
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()) {
// Respond to the action bar's Up/Home/back button
case android.R.id.home:
finish();
break;
}
return super.onOptionsItemSelected(item);
}
我明白了。谢谢所有人试图帮助我!
我希望这个答案对任何有类似问题的人都有帮助。祝你有愉快的一天:)