我正在撰写SQL
查询,以检查Gross Profit Percentage
数据库中的一组销售商品。
SELECT T0.[itmsgrpcod],
T0.sell,
T0.cost,
( T0.sell / T0.cost ) AS "GP"
FROM (SELECT T0.[itmsgrpcod],
Sum(T1.[price]) AS "Sell",
Sum(T1.[stockprice]) AS "Cost"
FROM inv1 T1
LEFT OUTER JOIN oitm T0
ON T1.[itemcode] = T0.[itemcode]
GROUP BY T0.[itmsgrpcod]) T0
当我将SELECT
语句作为:
SELECT T0.[ItmsGrpCod], T0.Sell, T0.Cost
它返回96行 - 正确的金额,并填写了卖出和成本数据。
当我添加列时:
(T0.Sell / T0.Cost) as "GP"
只返回查询的第一行,并正确计算GP。
答案 0 :(得分:1)
事实证明,SAP Business One的查询生成器不会报告除零。当我在SQL Server Management Studio中尝试查询时,它提供了一个正确的除零错误,我修复了问题。
完整查询,感兴趣的人:
SELECT T0.ItmsGrpCod,
SUM(T1.Price) As "Sell",
ISNULL(SUM(T1.StockPrice), 0) As "Cost",
CASE WHEN SUM(T1.StockPrice) = 0 THEN 100
ELSE (SUM(T1.Price) - SUM(T1.StockPrice)) / SUM(T1.Price) * 100
END As "GP"
FROM INV1 T1 LEFT OUTER JOIN OITM T0 ON T1.ItemCode = T0.ItemCode
GROUP BY T0.ItmsGrpCod
ORDER BY T0.ItmsGrpCod
答案 1 :(得分:0)
你可以尝试一下吗,我做的不多。确保将卖出和成本转换为十进制。
SELECT T0.[ItmsGrpCod],
T0.Sell,
T0.Cost,
(T0.Sell / T0.Cost) as GP
FROM (SELECT T0.[ItmsGrpCod],
CAST(SUM(T1.[Price]) AS DECIMAL(12,2)) as Sell,
CAST(SUM(T1.[StockPrice]) AS DECIMAL(12,2)) as Cost
FROM INV1 T1
LEFT JOIN OITM T0
ON T1.[ItemCode] = T0.[ItemCode]
GROUP BY T0.[ItmsGrpCod]) T0
答案 2 :(得分:0)
我认为你对alias
试试这个
SELECT T.[ItmsGrpCod],T.Sell,T.Cost, CAST(T.Sell AS Decimal) / T.Cost As GP
FROM
(
SELECT T0.[ItmsGrpCod],SUM(T1.[Price]) As Sell,SUM(T1.[StockPrice]) As Cost
FROM INV1 T1 LEFT OUTER JOIN OITM T0 ON T1.[ItemCode] = T0.[ItemCode]
GROUP BY T0.[ItmsGrpCod]
)T
<强>更新强>
SELECT T.[ItmsGrpCod],T.Sell,T.Cost,T.GP
FROM
(
SELECT T0.[ItmsGrpCod],
SUM(T1.[Price]) As Sell,
SUM(T1.[StockPrice]) As Cost,
SUM(T1.[Price]) / SUM(T1.[StockPrice]) As GP
FROM INV1 T1 LEFT OUTER JOIN OITM T0 ON T1.[ItemCode] = T0.[ItemCode]
GROUP BY T0.[ItmsGrpCod]
)T
答案 3 :(得分:0)
因为这个帖子中的每一个人都对我感到困惑和好奇。我添加了一些测试数据以查看最新情况但是我无法复制您的场景。
IF OBJECT_ID(N'TempINV1') > 0
BEGIN
DROP TABLE TempINV1
END
IF OBJECT_ID(N'TempOITM') > 0
BEGIN
DROP TABLE TempOITM
END
CREATE TABLE TempINV1 (ItemCode INT,
Price DECIMAL(8, 2),
StockPrice DECIMAL(8, 2))
CREATE TABLE TempOITM (ItemCode INT,
ItmsGrpCod VARCHAR(10))
INSERT INTO TempINV1
VALUES
(1, '3.21', '2.34'),
(2, '4.32', '3.45'),
(3, '5.43', '4.56')
INSERT INTO TempOITM
VALUES
(1, 'Product1'),
(2, 'Product2'),
(3, 'Product3')
两个sacripts(表名以Temp为前缀)输出相同数量的行项
SELECT T0.[ItmsGrpCod], T0.Sell, T0.Cost, (T0.Sell / T0.Cost) as "GP"
FROM (
SELECT T0.[ItmsGrpCod], SUM(T1.[Price]) as "Sell", SUM(T1.[StockPrice]) as "Cost"
FROM TempINV1 T1
LEFT OUTER JOIN TempOITM T0
ON T1.[ItemCode] = T0.[ItemCode]
GROUP BY T0.[ItmsGrpCod]) T0
--Or
SELECT T0.[ItmsGrpCod], T0.Sell, T0.Cost
FROM (
SELECT T0.[ItmsGrpCod], SUM(T1.[Price]) as "Sell", SUM(T1.[StockPrice]) as "Cost"
FROM TempINV1 T1
LEFT OUTER JOIN TempOITM T0
ON T1.[ItemCode] = T0.[ItemCode]
GROUP BY T0.[ItmsGrpCod]) T0
两个查询的输出相同(返回的数字或行)。
请您在机器上运行脚本并确认是否属实。另外为了帮助我们更好地理解,如果您能为我们提供一些样本日期,那就太棒了。
补偿除以零:
SELECT T0.ItmsGrpCod,
T0.Sell,
T0.Cost,
T0.Sell / CASE
WHEN ISNULL(T0.Cost, '0') = 0 THEN T0.Sell
ELSE T0.Cost
END AS "GP"
FROM(SELECT T0.ItmsGrpCod,
SUM(T1.Price)AS "Sell",
SUM(T1.StockPrice)AS "Cost"
FROM TempINV1 AS T1
LEFT OUTER JOIN TempOITM AS T0
ON T1.ItemCode = T0.ItemCode
GROUP BY T0.ItmsGrpCod)AS T0