我有一个复杂的(对我来说)查询,我似乎无法做对。我可以单独获取我正在寻找的数据,但需要在同一查询中将它们组合在一起。我不打算使用视图或临时表。
SELECT PRODUCT_T.PRODUCTSTANDARDPRICE AS PSTANDARDPRICE
FROM PRODUCT_T, USES_T
WHERE PRODUCT_T.PRODUCTID = USES_T.PRODUCTID
此查询返回:
PSTANDARDPRICE
175
200
200
750
然后我这样做:
Select uses_t.productid, sum(rawmaterial_t.materialstandardprice*uses_t.quantityrequired) AS TOTALRAWMATERIALCOST
From rawmaterial_t, uses_t
Where rawmaterial_t.materialid = uses_t.materialid
Group by uses_t.productid
order by productid
此查询返回:
PRODUCTID TOTALRAWMATERIALCOST
1 120
2 62.4
3 307.2
我正在寻找:
我尝试使用子查询,我尝试使用两个查询的连接,我在绝望中尝试了UNION。
这让我接近但是TOTALRAWCOSTPERPRODUCT对于每一行都不一样
SELECT PRODUCT_T.PRODUCTID, PRODUCT_T.PRODUCTSTANDARDPRICE AS PSTANDARDPRICE, TOTALRAWCOSTPERPRODUCT
FROM (
Select uses_t.productid, sum(rawmaterial_t.materialstandardprice*uses_t.quantityrequired) AS TOTALRAWCOSTPERPRODUCT
From rawmaterial_t, uses_t
Where rawmaterial_t.materialid = uses_t.materialid
Group by uses_t.productid
), PRODUCT_T, USES_T
WHERE PRODUCT_T.PRODUCTID = USES_T.PRODUCTID
我怎样才能到达我需要的地方?
答案 0 :(得分:1)
SELECT
uses_t.productid,
PRODUCT_T.PRODUCTSTANDARDPRICE AS PSTANDARDPRICE,
sum(rawmaterial_t.materialstandardprice*uses_t.quantityrequired) AS TOTALRAWMATERIALCOST
FROM
uses_t
INNER JOIN rawmaterial_t
ON rawmaterial_t.materialid = uses_t.materialid
INNER JOIN PRODUCT_T
ON PRODUCT_T.PRODUCTID = USES_T.PRODUCTID
GROPUP BY
uses_t.productid,
PRODUCT_T.PRODUCTSTANDARDPRICE
ORDER BY
uses_t.productid