| |

VerySource

 Forgot password?
 Register
Search
View: 801|Reply: 8

Find a SQL statement to get the result set

[Copy link]

2

Threads

9

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 Unknown

Post time: 2020-1-11 15:00:01
| Show all posts |Read mode
There is a table with the following contents:
     field1 field2 field3
        a 1 dd
        a 2 ee
        a 3 ee
        a 4 ee
        a 1 c
        a 2 e
        a 3 null
        a 1 8
        a 2 5
        a 2 4

The content of field3 doesn't matter.
I want it to be divided into three result sets according to the field field2. The first result set is
        a 1 dd
        a 2 ee
        a 3 ee
        a 4 ee
The second result set is:
        a 1 c
        a 2 e
        a 3 null
The third result set is
        a 1 8
        a 2 5
        a 2 4
How do I achieve it? Or how do I achieve one of the result sets?
Reply

Use magic Report

2

Threads

9

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 Macau

 Author| Post time: 2020-1-15 17:45:01
| Show all posts
Sorry to correct it:
There is a table with the following contents:
     field1 field2 field3
        a 1 dd
        a 2 ee
        a 3 ee
        a 4 ee
        a 1 c
        a 2 e
        a 3 null
        a 1 8
        a 2 5
        a 3 4

The content of field3 doesn't matter.
I want it to be divided into three result sets according to the field field2. The first result set is
        a 1 dd
        a 2 ee
        a 3 ee
        a 4 ee
The second result set is:
        a 1 c
        a 2 e
        a 3 null
The third result set is
        a 1 8
        a 2 5
        a 3 4
How do I achieve it? Or how do I achieve one of the result sets?
Reply

Use magic Report

0

Threads

211

Posts

108.00

Credits

Newbie

Rank: 1

Credits
108.00

 China

Post time: 2020-1-15 19:54:01
| Show all posts
What are the conditions that separate each result set?
Reply

Use magic Report

2

Threads

9

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 Macau

 Author| Post time: 2020-1-16 19:36:01
| Show all posts
The condition is field2, which is to divide the group by field2.
For example, the above three result sets are three groups.
Reply

Use magic Report

0

Threads

6

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-1-17 11:36:01
| Show all posts
Inadequate conditions
Reply

Use magic Report

0

Threads

6

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-1-17 12:45:01
| Show all posts
----------- quote --------------
The condition is field2, which is to divide the group by field2.
For example, the above three result sets are three groups.

If so, you get 4 sets
a 1 dd
a 1 c
a 1 8

a 2 ee
a 2 e
a 2 5

a 3 ee
a 3 null
a 3 4

a 4 ee
Reply

Use magic Report

2

Threads

9

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 Macau

 Author| Post time: 2020-1-17 14:27:02
| Show all posts
Oh. This is not the result set I want
Reply

Use magic Report

0

Threads

66

Posts

27.00

Credits

Newbie

Rank: 1

Credits
27.00

 China

Post time: 2020-1-17 20:54:01
| Show all posts
--- Create environment, temporary table #, content omitted

-Establish a unique incremental ID, if any, do not need to establish
select id = identity (int, 1,1), * into ## from #

--Group by increasing sequence
select *, (select isnull (max (id), 0) from ## where id <t.id and field2> t.field2) as page into ### from ## t

--Inquire
declare @page int
set @ page = 1-set the page number
exec ('select * from ### where page = (select top 1 page from (select distinct top' + @ page + 'page from ### order by page) tt order by page desc)')


--result
1 a 1 dd 0
2 a 2 ee 0
3 a 3 ee 0
4 a 4 ee 0


-When set @ page = 2 results
5 a 1 c 4
6 a 2 e 4
7 a 3 NULL 4

-When set @ page = 3 results
8 a 1 8 7
9 a 2 5 7
10 a 2 4 7
Reply

Use magic Report

0

Threads

4

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 Invalid IP Address

Post time: 2020-1-18 06:18:01
| Show all posts
If the result is
    field1 field2 field3
        a 1 dd
        a 2 ee
        a 3 ee
        a 1 c
        a 2 e
        a 3 null
        a 1 8
        a 3 4
        a 2 5
        a 4 ee
What did the result look like?
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