IF EXISTS (SELECT 1

FROM SYSOBJECTS

WHERE id = OBJECT_ID('Fact_SaleCar')

AND type = 'U')

BEGIN

DROP TABLE Fact_SaleCar

END

GO

CREATE TABLE [dbo].Fact_SaleCar

(

SaleCarId VARCHAR(20) NOT NULL,

SaleName VARCHAR(50) NULL,

CheckOutDate DATETIME NULL,

Price Float NULL

CONSTRAINT PK_Fact_SaleCar PRIMARY key (SaleCarId)

);

GO

BEGIN

DECLARE @NUM INT;

SET @NUM=1;

WHILE @NUM <= 100000

BEGIN

INSERT INTO dbo.Fact_SaleCar

SELECT '商店'+RTRIM(@NUM),'SSS'+RTRIM(@NUM),GETDATE(),@NUM;

SET @NUM=@NUM+1;

END;

END;

SELECT A.SaleCarId,

Sum(Price)AS Price

FROM Fact_SaleCar A

INNER JOIN (SELECT Max(CheckoutDate) AS CheckoutDate,

SaleCarId

FROM Fact_SaleCar B

GROUP BY SaleCarId) C

ON A.SaleCarId = C.SaleCarId

AND A. CheckoutDate = C.CheckoutDate

GROUP BY A.SaleCarId

SELECT A.SaleCarId,

Sum(Price)AS Price

FROM Fact_SaleCar A

LEFT JOIN (SELECT Max(CheckoutDate) AS CheckoutDate,

SaleCarId

FROM Fact_SaleCar B

GROUP BY SaleCarId) C

ON A.SaleCarId = C.SaleCarId

AND A. CheckoutDate = C.CheckoutDate

GROUP BY A.SaleCarId

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐