2017-01-04 37 views
0

正如我在標題中所說,我想顯示所有按期和按年分組的總額(賬單),但如果在一個三個月的期間沒有賬單如:第二學期量= 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 = 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 
+0

哪些dbms是你在用嗎? – jarlh

+0

@jarlh我正在使用HSQL(與OpenOffice基地) –

+0

我會創建一個4行的trimestre表,其值爲1到4.外部加入該表! – jarlh

回答

0

這終於摸索!:

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) 
) 

這是表存在於表,我想所有值年。

然後,我已經列出所有以「()」,並製作成表,從中選擇 - >「()」,並選擇以下行:

select "year","Trimestre",COALESCE(sum("base"),0) as "base",COALESCE(sum("iva"),0) as "iva",COALESCE(sum("total"),0) as "total" 

而且我得到了它。感謝所有試圖幫助我!

我希望這個答案對任何有類似問題的人都有幫助。祝你有美好的一天:)

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; 
+0

嗨,我試圖把這個代碼放在我的結尾和程序給我錯誤「意外的令牌與」。你可以把所有的代碼,應該工作嗎? (對不起,我之前沒有用「with」從句工作過) –

+0

當然。看到我的第二篇文章。順便說一下,我正在使用MS SQL Server測試此代碼,所以我不確定它在HSQL中的工作方式。 –

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 
+0

無法使用ISNULL - >在語句ISNULL中拒絕訪問 –

+0

嘗試用COALESCE替換ISNULL。 –

+0

已修復,但現在給這個:意外的聯盟,要求從聲明[(完整查詢)] –

1

這不是一個完整的答案,因爲你不提供樣品表數據。你可以自己弄清楚細節。 HSQLDB有許多功能可以用來避免漫長而複雜的查詢。

YEAR(日期)和QUARTER(日期)函數可用於提取分組的日期部分。

UNNEST關鍵字連同SEQUENCE_ARRAY(start,end,interval)函數可用於在數據爲空時生成宿舍。

第一部分是宿舍的名單表:

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

然後,你需要LEFT將表格添加到宿舍列表並將所需列添加到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")