SQL group by having clause query

时间:2018-01-11 10:36:02

标签: tsql sql-server-2012 group-by

我有下表和查询。正如您在运行时从代码中看到的那样,它除以MAX(NewBalance),其中5号收据为155.

收据没有例子

但是我希望它使用NewBalance作为Max(Receiptno2)值,即125而不是MAX(NewBalance)155.

所以当RecieptNo = 5且receiptNo2 = 5时,它应该使用NewBalance值

CREATE TABLE [dbo].[Y](
  [Item No_] [nvarchar](20) NULL,
  [ReceiptNo] [bigint] NULL,
  [ReceiptNo2] [bigint] NULL,
  [NewBalance] [decimal](38, 20) NULL,
  [InventoryBalance] [decimal](38, 20) NULL,
  [PurchaseQty] [decimal](38, 20) NULL,
  [IntakeSellingPrice] [decimal](38, 20) NULL
) ON [PRIMARY]
GO

INSERT [dbo].[Y] ([Item No_], [ReceiptNo], [ReceiptNo2], [NewBalance], [InventoryBalance], [PurchaseQty], [IntakeSellingPrice]) 
VALUES (N'1000001', 1, 1, CAST(100.00000000000000000000 AS Decimal(38, 20)), CAST(0.00000000000000000000 AS Decimal(38, 20)), CAST(100.00000000000000000000 AS Decimal(38, 20)), CAST(10.00000000000000000000 AS Decimal(38, 20)))
, (N'1000001', 2, 2, CAST(100.00000000000000000000 AS Decimal(38, 20)), CAST(50.00000000000000000000 AS Decimal(38, 20)), CAST(50.00000000000000000000 AS Decimal(38, 20)), CAST(5.00000000000000000000 AS Decimal(38, 20)))
, (N'1000001', 3, 3, CAST(110.00000000000000000000 AS Decimal(38, 20)), CAST(60.00000000000000000000 AS Decimal(38, 20)), CAST(50.00000000000000000000 AS Decimal(38, 20)), CAST(12.50000000000000000000 AS Decimal(38, 20)))
, (N'1000001', 4, 4, CAST(155.00000000000000000000 AS Decimal(38, 20)), CAST(105.00000000000000000000 AS Decimal(38, 20)), CAST(50.00000000000000000000 AS Decimal(38, 20)), CAST(15.00000000000000000000 AS Decimal(38, 20)))
, (N'1000001', 5, 5, CAST(125.00000000000000000000 AS Decimal(38, 20)), CAST(75.00000000000000000000 AS Decimal(38, 20)), CAST(50.00000000000000000000 AS Decimal(38, 20)), CAST(20.00000000000000000000 AS Decimal(38, 20)))
, (N'1000001', 5, 4, CAST(155.00000000000000000000 AS Decimal(38, 20)), CAST(25.00000000000000000000 AS Decimal(38, 20)), CAST(50.00000000000000000000 AS Decimal(38, 20)), CAST(15.00000000000000000000 AS Decimal(38, 20)))
, (N'1000001', 5, 3, CAST(110.00000000000000000000 AS Decimal(38, 20)), CAST(-25.00000000000000000000 AS Decimal(38, 20)), CAST(25.00000000000000000000 AS Decimal(38, 20)), CAST(12.50000000000000000000 AS Decimal(38, 20)))
, (N'1000001', 5, 2, CAST(100.00000000000000000000 AS Decimal(38, 20)), CAST(-75.00000000000000000000 AS Decimal(38, 20)), CAST(-25.00000000000000000000 AS Decimal(38, 20)), CAST(5.00000000000000000000 AS Decimal(38, 20)))
, (N'1000001', 5, 1, CAST(100.00000000000000000000 AS Decimal(38, 20)), CAST(-175.00000000000000000000 AS Decimal(38, 20)), CAST(-75.00000000000000000000 AS Decimal(38, 20)), CAST(10.00000000000000000000 AS Decimal(38, 20)))
, (N'1000001', 4, 3, CAST(110.00000000000000000000 AS Decimal(38, 20)), CAST(55.00000000000000000000 AS Decimal(38, 20)), CAST(50.00000000000000000000 AS Decimal(38, 20)), CAST(12.50000000000000000000 AS Decimal(38, 20)))
, (N'1000001', 4, 2, CAST(100.00000000000000000000 AS Decimal(38, 20)), CAST(5.00000000000000000000 AS Decimal(38, 20)), CAST(50.00000000000000000000 AS Decimal(38, 20)), CAST(5.00000000000000000000 AS Decimal(38, 20)))
, (N'1000001', 4, 1, CAST(100.00000000000000000000 AS Decimal(38, 20)), CAST(-95.00000000000000000000 AS Decimal(38, 20)), CAST(5.00000000000000000000 AS Decimal(38, 20)), CAST(10.00000000000000000000 AS Decimal(38, 20)))
, (N'1000001', 3, 2, CAST(100.00000000000000000000 AS Decimal(38, 20)), CAST(10.00000000000000000000 AS Decimal(38, 20)), CAST(50.00000000000000000000 AS Decimal(38, 20)), CAST(5.00000000000000000000 AS Decimal(38, 20)))
, (N'1000001', 3, 1, CAST(100.00000000000000000000 AS Decimal(38, 20)), CAST(-90.00000000000000000000 AS Decimal(38, 20)), CAST(10.00000000000000000000 AS Decimal(38, 20)), CAST(10.00000000000000000000 AS Decimal(38, 20)))
, (N'1000001', 2, 1, CAST(100.00000000000000000000 AS Decimal(38, 20)), CAST(-50.00000000000000000000 AS Decimal(38, 20)), CAST(50.00000000000000000000 AS Decimal(38, 20)), CAST(10.00000000000000000000 AS Decimal(38, 20)))
GO

SELECT [Item No_],
    ReceiptNo,
    SUM(CASE WHEN PurchaseQty <= 0 THEN 0 ELSE PurchaseQty  END  * IntakeSellingPrice) / MAX(NewBalance) AS avg_price
FROM Y 
GROUP BY [Item No_], ReceiptNo

1 个答案:

答案 0 :(得分:0)

您可以通过对公用表表达式进行分组,然后在MAX(ReceiptNo2)语句中加入它来访问该行的所有值,从而获得SELECT

;WITH Y_AGGREGATED([Item No_], ReceiptNo, MAX_ReceiptNo2)
AS
(
    SELECT
        [Item No_],
        ReceiptNo,
        MAX(ReceiptNo2) AS MAX_ReceiptNo2
    FROM [Y]
    GROUP BY [Item No_], ReceiptNo
)
SELECT
    Y.*
FROM [Y]
JOIN [Y_AGGREGATED] AS A
    ON A.[Item No_] = Y.[Item No_]
    AND A.ReceiptNo = Y.ReceiptNo
    AND A.MAX_ReceiptNo2 = Y.ReceiptNo2