如果年份存在,则即使在某个月份不存在帐单,也要填充四个三个月

阿尔贝托

就像我在标题中说的那样,我想显示按三个月和年份分组的所有总和(账单),但是如果一个月中没有任何账单,则显示为:第三学期的金额= 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 = 3学期,4ºTrimestre = 4学期。

还尝试了以下方法:

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
阿尔贝托

终于成功了!:

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

我制作了另一个“全部联盟”,其后是:

(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"

我知道了。谢谢大家为我提供的帮助!

我希望这个答案对有类似问题的人有所帮助。祝你今天过得愉快 :)

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何从输入的月份年份中获取上一个月的第三个月?

来自分类Dev

如何从输入的月份年份中获取上一个月的第三个月?

来自分类Dev

SQL获取本月前三个月的月份的第一天

来自分类Dev

尝试获取最近三个月的数据

来自分类Dev

动态获取最近三个月

来自分类Dev

前三个月的值的总和

来自分类Dev

SQL最近三个月

来自分类Dev

三个月内每五个月滚动一次

来自分类Dev

Pyspark:每个月累计前三个月的总和

来自分类Dev

如何在当前月份的下三个月的列表视图中显示计划金额

来自分类Dev

PHP检查两个日期是否至少相差三个月

来自分类Dev

根据最近三个月的销售额计算不同的客户

来自分类Dev

PowerBI DAX:前三个月的日期期限标识公式发布

来自分类Dev

此数据集最近三个月的总计数是多少?

来自分类Dev

用户使用Twitter API如何获取最近三个月以来的推文

来自分类Dev

获取最近三个月按商店销售的发票数

来自分类Dev

过去三个月在mdx查询中的最小值

来自分类Dev

如何在单个查询中获取最近三个月的数据

来自分类Dev

如何获取SQL Server中前三个月的日期列?

来自分类Dev

如何从日期参数中选择过去三个月的数据

来自分类Dev

每三个月自动编辑查询 MySQL 的 WHERE 条件

来自分类Dev

用Java计算每三个月的第三个星期一?

来自分类Dev

按三个月分组

来自分类Dev

按三个月分组

来自分类Dev

每年过去四个月的颜色与其他月份的php不同

来自分类Dev

Oracle:需要计算过去三个月的滚动平均值,其中我们每个月有多个提交

来自分类Dev

如何对一个数据帧进行三个月的平均值,并使用这些值构建一个新的?

来自分类Dev

选择区间中不存在的地方?四个日期的不同

来自分类Dev

将年月字符串转换为带有间隔的三个月垃圾箱-如何分配连续的升序值?

Related 相关文章

  1. 1

    如何从输入的月份年份中获取上一个月的第三个月?

  2. 2

    如何从输入的月份年份中获取上一个月的第三个月?

  3. 3

    SQL获取本月前三个月的月份的第一天

  4. 4

    尝试获取最近三个月的数据

  5. 5

    动态获取最近三个月

  6. 6

    前三个月的值的总和

  7. 7

    SQL最近三个月

  8. 8

    三个月内每五个月滚动一次

  9. 9

    Pyspark:每个月累计前三个月的总和

  10. 10

    如何在当前月份的下三个月的列表视图中显示计划金额

  11. 11

    PHP检查两个日期是否至少相差三个月

  12. 12

    根据最近三个月的销售额计算不同的客户

  13. 13

    PowerBI DAX:前三个月的日期期限标识公式发布

  14. 14

    此数据集最近三个月的总计数是多少?

  15. 15

    用户使用Twitter API如何获取最近三个月以来的推文

  16. 16

    获取最近三个月按商店销售的发票数

  17. 17

    过去三个月在mdx查询中的最小值

  18. 18

    如何在单个查询中获取最近三个月的数据

  19. 19

    如何获取SQL Server中前三个月的日期列?

  20. 20

    如何从日期参数中选择过去三个月的数据

  21. 21

    每三个月自动编辑查询 MySQL 的 WHERE 条件

  22. 22

    用Java计算每三个月的第三个星期一?

  23. 23

    按三个月分组

  24. 24

    按三个月分组

  25. 25

    每年过去四个月的颜色与其他月份的php不同

  26. 26

    Oracle:需要计算过去三个月的滚动平均值,其中我们每个月有多个提交

  27. 27

    如何对一个数据帧进行三个月的平均值,并使用这些值构建一个新的?

  28. 28

    选择区间中不存在的地方?四个日期的不同

  29. 29

    将年月字符串转换为带有间隔的三个月垃圾箱-如何分配连续的升序值?

热门标签

归档