| |

VerySource

 Forgot password?
 Register
Search
View: 1592|Reply: 12

Ask a few super simple SQL statements:

[Copy link]

3

Threads

12

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 China

Post time: 2020-9-25 18:30:01
| Show all posts |Read mode
1. There is a table, name, quantity... already sorted in descending order.
Find the sum of the names of the first 20% of it.
Similar to:

select top 20 percent quantity from table name //This sentence is OK..
//The following will not work.
declare @departtotal int
select @departtotal = sum (select top 20 percent quantity from table name)
Reply

Use magic Report

0

Threads

211

Posts

108.00

Credits

Newbie

Rank: 1

Credits
108.00

 China

Post time: 2020-9-25 18:45:01
| Show all posts
--try

declare @departtotal int
select top 20 percent @departtotal=sum(number) from table name
select @departtotal
Reply

Use magic Report

0

Threads

100

Posts

53.00

Credits

Newbie

Rank: 1

Credits
53.00

 China

Post time: 2020-9-25 19:00:01
| Show all posts
Try:
declare @departtotal int
select @departtotal = sum(quantity) from (select top 20 percent quantity from table name) t
Reply

Use magic Report

3

Threads

12

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 China

 Author| Post time: 2020-9-25 23:00:01
| Show all posts
fanfan1980I don't know why, what you asked for is the total..
unkmassOh, you can. After thinking for a while, I understand why a "t" is needed in the back
Reply

Use magic Report

2

Threads

6

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-9-26 00:00:01
| Show all posts
Learn
Reply

Use magic Report

1

Threads

13

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 China

Post time: 2020-9-26 13:00:02
| Show all posts
Try:
declare @departtotal int
select @departtotal = sum(quantity) from (select top 20 percent quantity from table name) t

-------------
Leave a mark
I would like to ask what is the difference between @departtotal = sum(quantity) and removing sum. Because you come from a table t that only has quantity, then you select the top 20% quantity result and select top 20 percent quantity from table name query result It’s the same, don’t understand, hope to explain
Reply

Use magic Report

0

Threads

126

Posts

73.00

Credits

Newbie

Rank: 1

Credits
73.00

 China

Post time: 2020-9-26 13:30:01
| Show all posts
Without SUM(), the detailed data will come out
With SUM(), the result is the summary data (sum)
Reply

Use magic Report

0

Threads

126

Posts

73.00

Credits

Newbie

Rank: 1

Credits
73.00

 China

Post time: 2020-9-26 13:45:01
| Show all posts
Another point is that it uses variables to store the results. If you don't use SUM() and return multiple records, select @departtotal = sum(number) will cause an error.

Using SUM(), the result is to ensure that there is only one summary data, and the above error will not occur.
Reply

Use magic Report

1

Threads

13

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 China

Post time: 2020-9-26 14:00:01
| Show all posts
wrong
Something happened just now
Sorry
Reply

Use magic Report

1

Threads

13

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 China

Post time: 2020-9-26 14:15:01
| Show all posts
Yep
Who knows why the first sentence
select top 20 percent @departtotal=sum(number) from table name
with
select sum (number) from table name
The result is the same
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list