Application of subquery in SQL statements

Source: Internet
Author: User

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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.