Interview Questions of a company:
Sales info table tbSaleInfo
DeviceNo device No.
GoodNo goods track no. (1-24)
CardNo
Money consumption amount
Balance card Balance
SaleFlowNo sales serial number
SaleTime sales time
CollectionTime collection time
Mode Sales type
Mode = 0 indicates cash consumption. The card number is 0 and the card balance is 0.
Mode = 4 indicates card consumption. The card number is valid and the card balance is valid.
Requirements:
Query by device number: Device number, start date, and end date
Output Information Field: Device number, sales date, cash sales quantity, cash sales amount, IC card sales amount, IC card sales amount, total sales amount, total sales amount
Cash: sales log Mode = 0
IC Card: sales log Mode = 4
Sales Information per day within the specified date range, sorted by date
My method:
@ DeviceNo, @ SaleTime, and @ SaleTime2 must have three parameters: Device number, start date, and end date.
Code
SELECT
@ DeviceNo AS device number,
CONVERT (varchar (10), SaleTime, 120) AS sales date,
(Select count (Id) AS Expr1
FROM tbSaleInfo AS tbSaleInfo_1
WHERE (DeviceNo = @ DeviceNo) AND (Mode = 0) AND (CONVERT (varchar (10), SaleTime, 120) = CONVERT (varchar (10), tbSaleInfo. saleTime, 120 ))
Group by convert (varchar (10), salestime, 120 ))
AS cash sales volume,
(Select sum ([Money]) AS Expr1
FROM tbSaleInfo AS tbSaleInfo_1
WHERE (DeviceNo = @ DeviceNo) AND (Mode = 0) AND (CONVERT (varchar (10), SaleTime, 120) = CONVERT (varchar (10), tbSaleInfo. saleTime, 120 ))
Group by convert (varchar (10), salestime, 120 ))
AS cash sales amount,
(Select count (Id) AS Expr1
FROM tbSaleInfo AS tbSaleInfo_1
WHERE (DeviceNo = @ DeviceNo) AND (Mode = 4) AND (CONVERT (varchar (10), SaleTime, 120) = CONVERT (varchar (10), tbSaleInfo. saleTime, 120 ))
Group by convert (varchar (10), salestime, 120 ))
As ic card sales volume,
(Select sum ([Money]) AS Expr1
FROM tbSaleInfo AS tbSaleInfo_1
WHERE (DeviceNo = @ DeviceNo) AND (Mode = 4) AND (CONVERT (varchar (10), SaleTime, 120) = CONVERT (varchar (10), tbSaleInfo. saleTime, 120 ))
Group by convert (varchar (10), salestime, 120 ))
As ic card sales amount,
Count (ID) as total sales volume,
Sum ([money]) as total sales amount
From tbsaleinfo
Where (deviceno = @ deviceno) and (saletime between @ saletime and @ saletime2)
Group by convert (varchar (10), saletime, 120)
Order by sales date
I only think of subqueries. I am very grateful to my predecessors!