更正子查询时遇到麻烦

时间:2019-07-10 02:24:29

标签: sql database postgresql

我有一个子查询被用作报表中的参数。此特定查询已在另一个报告中使用,因此我试图变得聪明,然后再次将其用于此报告。客户希望这样做的方式,他们需要相应项目的当年和去年以及当年的年初至今。我想我已经确定了当前月份,但是我的问题是交易日期或类别已被替换。下面是示例

(
  customer,
  fiscalperiod,
  transactiondate,
  workcenter,
  projectid,
  eventname,
  sales,
  directlabor,
  materialscost,
  othercost,
  subcontractor,
  cogs,
  projectprofit,
  projectprofitmargin
)
AS 
 SELECT b.customer, b.fiscalperiod, b.workcenter AS transactiondate, b.projectid AS workcenter, b.eventname AS projectid, b.sales AS eventname, b.directlabor AS sales, b.materialscost AS directlabor, b.othercost AS materialscost, b.subcontractor AS othercost, b.transactiondate AS subcontractor, 
        CASE
            WHEN b.cogs > 1::double precision THEN b.cogs * (- 1::double precision)
            ELSE b.cogs
        END AS cogs, b.projectprofit, b.projectprofitmargin
   FROM ( SELECT table1.customer, table1.fiscalperiod, table1.workcenter, table1.projectid, table1.eventname, sum(table1.revenue) AS sales, sum(table1.directlabor) AS directlabor, sum(table1.materialscost) AS materialscost, sum(table1.othercost) AS othercost, sum(table1.subcontractor) AS subcontractor, table1.transactiondate, 
                CASE
                    WHEN sum(table1.expense) < 0::double precision THEN sum(table1.expense) * (- 1::double precision)
                    ELSE sum(table1.expense)
                END AS cogs, sum(table1.revenue) - 
                CASE
                    WHEN sum(table1.expense) < 0::double precision THEN sum(table1.expense) * (- 1::double precision)
                    ELSE sum(table1.expense)
                END AS projectprofit, 
                CASE
                    WHEN sum(table1.revenue) = NULL::double precision THEN 100::double precision
                    WHEN sum(table1.revenue) = 0::double precision THEN 100::double precision
                    ELSE 100::double precision * (sum(table1.revenue) + sum(table1.expense)) / sum(table1.revenue)
                END AS projectprofitmargin
           FROM ( SELECT p.customer, p.projectid, p.eventname, t.fiscalperiod, w.workcenter, t.transactiondate, 
                        CASE
                            WHEN c."type"::text = 'RV'::character varying::text THEN sum(
                            CASE
                                WHEN t.namount = 0.00::double precision THEN 0.00::double precision
                                ELSE t.namount
                            END)
                            ELSE 0.00::double precision
                        END AS revenue, 
                        CASE
                            WHEN c."type"::text = 'EX'::character varying::text THEN sum(
                            CASE
                                WHEN t.namount = 0.00::double precision THEN 0.00::double precision
                                ELSE t.namount
                            END)
                            ELSE 0.00::double precision
                        END AS expense, 
                        CASE
                            WHEN t.glaccount::text = '40020'::character varying::text THEN sum(
                            CASE
                                WHEN t.namount = 0.00::double precision THEN 0.00::double precision
                                ELSE t.namount
                            END)
                            ELSE 0.00::double precision
                        END AS directlabor, 
                        CASE
                            WHEN t.glaccount::text = '40000'::character varying::text THEN sum(
                            CASE
                                WHEN t.namount = 0.00::double precision THEN 0.00::double precision
                                ELSE t.namount
                            END)
                            ELSE 0.00::double precision
                        END AS materialscost, 
                        CASE
                            WHEN t.glaccount::text = '46200'::character varying::text THEN sum(
                            CASE
                                WHEN t.namount = 0.00::double precision THEN 0.00::double precision
                                ELSE t.namount
                            END)
                            WHEN t.glaccount::text = '40112'::character varying::text THEN sum(
                            CASE
                                WHEN t.namount = 0.00::double precision THEN 0.00::double precision
                                ELSE t.namount
                            END)
                            ELSE 0.00::double precision
                        END AS othercost, 
                        CASE
                            WHEN t.glaccount::text = '40030'::character varying::text THEN sum(
                            CASE
                                WHEN t.namount = 0.00::double precision THEN 0.00::double precision
                                ELSE t.namount
                            END)
                            ELSE 0.00::double precision
                        END AS subcontractor
                   FROM hargrade_projectlink.twprojecttransaction t
              LEFT JOIN hargrade_projectlink.vsproject p ON t.projectid::text = p.projectid::text
         LEFT JOIN hargrade_projectlink.vsworkcenter w ON t.workcenterid::text = w.id::text
    LEFT JOIN hargrade_projectlink.vscategory c ON t.categoryid::text = c.id::text
   WHERE p.customer::text <> ''::character varying::text AND t.batchtype::text <> 'BDGT'::character varying::text
   GROUP BY p.customer, p.eventname, p.projectid, t.fiscalperiod, w.workcenter, c."type", t.glaccount, t.transactiondate) table1
          GROUP BY table1.customer, table1.fiscalperiod, table1.workcenter, table1.eventname, table1.projectid, table1.transactiondate) b
  WHERE b.sales > 0::double precision;


Select sales,transactiondate
from public.vw_ytdprojectdetailfinal_copy
where transactiondate >= date_trunc('month', current_date - interval '1' month)
  and transactiondate < date_trunc('month', current_date

the desired:                  the actual:
sales  transactiondate        sales  transactiondate 
num      2018-05-08             0     account services
num      2018-05-08             0     sales
num      2018-05-08             0     other department
num      2018-05-08
num      2018-05-08
num      2018-05-08


any ideas on what I'm doing wrong?




1 个答案:

答案 0 :(得分:0)

什么是b.workcenter?我看到您在这里将别名作为交易日期,这似乎是工作中心/部门名称。

SELECT b.customer, b.fiscalperiod, b.workcenter AS transactiondate