在postgresql中,内部联接在单行中给出id的结果
这是我的疑问:在postgresql中,内部联接在单行中给出id的结果,sql,postgresql,Sql,Postgresql,这是我的疑问: SELECT (sc.contract_number) AS id, to_char(sc.contract_date, 'DD/MM/YYYY') AS "Contract Date", (sc.cust_name) AS "Customer Name", ( SELECT pt.product_desc FROM product_produ
SELECT (sc.contract_number) AS id,
to_char(sc.contract_date, 'DD/MM/YYYY') AS "Contract Date",
(sc.cust_name) AS "Customer Name",
(
SELECT pt.product_desc
FROM product_product pp,
product_template pt
WHERE pt.id = pp.product_tmpl_id AND
type = 'service' AND
pt.name LIKE sc.pms_lines
) AS "PMS",
CASE WHEN sc.parent_ref IS NULL THEN ROUND(sc.basic_amount)
ELSE 0
END AS "New Amount",
CASE WHEN sc.parent_ref IS NULL THEN 0
ELSE ROUND(sc.basic_amount)
END AS "Renew Amount",
ROUND(sc.service_tax_amount + sc.edu_tax_amount +
sc.hs_edu_tax_amount) AS "Service Tax",
ROUND(sc.grand_total_amount) AS "Total"
FROM sale_contract sc
INNER JOIN inspection_costing_line icl ON (sc.id = icl.id)
WHERE sc.contract_date BETWEEN '2015-03-02'
AND '2015-03-10'
ORDER BY sc.contract_number ASC;
我得到的结果是:
id | Contract Date | Customer Name | PMS | New Amount | Renew Amount | Service Tax | Total
--------------------|---------------|-----------------------------------|-----------------|------------|--------------|-------------|-----------
P114CO15004025 | 03/03/2015 | Farhan Farooqui | GSS | 0 | 2200 | 272 | 2472
P114CO15004026 | 03/03/2015 | R K Builders | TSPO | 3000 | 0 | 371 | 3371
P114CO15004027 | 03/03/2015 | Jivesh Terrace | PSS , IMM , PPS | 0 | 60000 | 7416 | 67416
预期结果
id | Contract Date | Customer Name | PMS | New Amount | Renew Amount | Service Tax | Total
--------------------|---------------|-----------------------------------|-----------------|------------|--------------|-------------|-----------
P114CO15004025 | 03/03/2015 | Farhan Farooqui | GSS | 0 | 2200 | 272 | 2472
P114CO15004026 | 03/03/2015 | R K Builders | TSPO | 3000 | 0 | 371 | 3371
P114CO15004027 | 03/03/2015 | Jivesh Terrace | PSS | 0 | 60000 | 7416 | 67416
P114CO15004027 | 03/03/2015 | Jivesh Terrace | IMM | 0 | 60000 | 7416 | 67416
P114CO15004027 | 03/03/2015 | Jivesh Terrace | PPS | 0 | 60000 | 7416 | 67416
您正在使用select pt…的输出。。。。作为列,请尝试在from子句中移动select,如下所示:
SELECT (sc.contract_number) AS id,
to_char(sc.contract_date, 'DD/MM/YYYY') AS "Contract Date",
(sc.cust_name) AS "Customer Name",
pt.product_desc AS "PMS",
CASE WHEN sc.parent_ref IS NULL THEN ROUND(sc.basic_amount)
ELSE 0
END AS "New Amount",
CASE WHEN sc.parent_ref IS NULL THEN 0
ELSE ROUND(sc.basic_amount)
END AS "Renew Amount",
ROUND(sc.service_tax_amount + sc.edu_tax_amount +
sc.hs_edu_tax_amount) AS "Service Tax",
ROUND(sc.grand_total_amount) AS "Total"
FROM product_product pp, product_template pt, sale_contract sc
INNER JOIN inspection_costing_line icl ON (sc.id = icl.id)
WHERE sc.contract_date BETWEEN '2015-03-02'
AND '2015-03-10' and
pt.id = pp.product_tmpl_id AND
type = 'service' AND
pt.name LIKE sc.pms_lines
ORDER BY sc.contract_number ASC;
我无法对其进行测试,但可以将其用作起点您可以演示如何加入销售和发票表吗?这是我的查询。。从sc.id=icl.id上的销售合同sc内部连接检验成本计算行icl中选择列名称,其中sc.contract日期在“2015-03-02”和“2015-03-10”之间,由sc.contract\U编号ASC确定@abhijeetmote。使用您正在使用的真实查询编辑您的问题。评论中的版本不是您正在运行的版本-例如,它只会返回一列。我从您的问题中删除了所有的和HTML,但却被还原回:“很抱歉,这是问题中的最终更改…使用您的解决方案,将跳过包含多个PM的记录,据我所知,子查询工作正常。。。问题在于连接。