0
SELECT BM.PUser, 
   BM.Desc, BM.Price, 
   BM.Info,
   CAST (CASE 
      WHEN BI.Closed = 'E' then BQ.Qty-BQ.Consign-(SUM(BD.QtySold))
      ELSE BQ.Qty-BQ.Consign
   END as int) as Stock
FROM BINVOICE BI
 , BDETAIL BD
 , BQTY BQ
 , BMASTER BM
WHERE (BD.User = BI.User)
   AND 
  (BQ.PartNo = BD.PartNo)
   AND
  (BQ.PartNo = BM.PartNo)
   AND
  (BM.Price > 0.01)
   AND
  (BM.Active = 'Y')
GROUP BY BM.PUser, BM.Price,
     BM.Desc, BM.Info, 
     BQ.Consign, BQ.Qty,
     BI.Closed

我的问题是我希望每个 PUser 只显示一个,但其中一些出现多次。我相信我所做的 CASE 就是它们的来源。我不太确定如何解决这个问题。使用 SELECT DISTINCT 对我不起作用。任何指导将不胜感激。它用于 NexusDB。

4

1 回答 1

0

使用不同的 SELECT DISTINCT column_name,column_name FROM table_name;

SELECT Distinct  BM.PUser, 
   BM.Desc, BM.Price, 
   BM.Info,
   CAST (CASE 
      WHEN BI.Closed = 'E' then BQ.Qty-BQ.Consign-(SUM(BD.QtySold))
      ELSE BQ.Qty-BQ.Consign
   END as int) as Stock
FROM BINVOICE BI
 , BDETAIL BD
 , BQTY BQ
 , BMASTER BM
WHERE (BD.User = BI.User)
   AND 
  (BQ.PartNo = BD.PartNo)
   AND
  (BQ.PartNo = BM.PartNo)
   AND
  (BM.Price > 0.01)
   AND
  (BM.Active = 'Y')
GROUP BY BM.PUser, BM.Price,
     BM.Desc, BM.Info, 
     BQ.Consign, BQ.Qty,
     BI.Closed
于 2015-03-21T04:56:19.747 回答