SQL grouping plus column plus self-numbering self-numbering qualification

Source: Internet
Author: User

Description

(1) The date is displayed in the form of: convert (varchar (7), field name, 120),

(2) Add a column

(3) Self-numbering:

Row_number () Over (Order by field name Desc) as RowID

Row_number () Over (partition by field 1 order By field 2) as RowID

(4) Self-numbering restrictions (not directly in the Where condition)


To illustrate:

Desired effect: monthly statistics of the top 5 jobs (based on the number of pieces)

Initial SQL statement:

Select SUM (sum_truenum) Number of pieces, Pickuser_name,convert (varchar (7), picktime,120) time
From V_outin_maintop
where brand_id=66 and Classreport_name= ' out of the library ' and Len (pickuser_name) >0
and picktime>= ' 2014-9-1 ' and picktime< ' 2014-12-1 '
GROUP BY Pickuser_name,convert (varchar (7), picktime,120)


First step: Add a column

SQL statements:

Select jobs = ' picking ', sum (sum_truenum), Pickuser_name pick, convert (varchar (7), picktime,120) time
From V_outin_maintop
where brand_id=66 and Classreport_name= ' out of the library ' and Len (pickuser_name) >0
and picktime>= ' 2014-9-1 ' and picktime< ' 2014-12-1 '
GROUP BY Pickuser_name,convert (varchar (7), picktime,120)


Step Two: Add self-numbering (sort by month, group by person)

SQL statements:

Select jobs = ' picking ',row_number () over (partition by convert (varchar (7), picktime,120) Order by sum (SUM_TRUENUM) desc) As RowID,
SUM (sum_truenum) Number of pieces, pickuser_name, convert (varchar (7), picktime,120) time
From V_outin_maintop
where brand_id=66 and Classreport_name= ' out of the library ' and Len (pickuser_name) >0
and picktime>= ' 2014-9-1 ' and picktime< ' 2014-12-1 '
GROUP BY Pickuser_name,convert (varchar (7), picktime,120)


Step three: Self-numbering (only the first few)

SQL statements:

With Rowjianhuo
As
(
Select jobs = ' picking ', row_number () over (partition by convert (varchar (7), picktime,120) Order by sum (SUM_TRUENUM) desc) as RowID,
SUM (sum_truenum) Number of pieces, pickuser_name, convert (varchar (7), picktime,120) time
From V_outin_maintop
where brand_id=66 and Classreport_name= ' out of the library ' and Len (pickuser_name) >0
and picktime>= ' 2014-9-1 ' and picktime< ' 2014-12-1 '
GROUP BY Pickuser_name,convert (varchar (7), picktime,120)
)
SELECT * from Rowjianhuo where rowid<6

You can also write this:

SELECT * FROM
(
Select jobs = ' picking ', row_number () over (partition by convert (varchar (7), picktime,120) Order by sum (SUM_TRUENUM) desc) as RowID,
SUM (sum_truenum) Number of pieces, pickuser_name, convert (varchar (7), picktime,120) time
From V_outin_maintop
where brand_id=66 and Classreport_name= ' out of the library ' and Len (pickuser_name) >0
and picktime>= ' 2014-9-1 ' and picktime< ' 2014-12-1 '
GROUP BY Pickuser_name,convert (varchar (7), picktime,120)
) A
where rowid<6

: (Data is a little, so the data does not change)


Note:

Two tables, the same structure, connected to a table: with

SELECT * FROM table 1

UNION ALL

SELECT * FROM Table 2


SQL grouping plus column plus self-numbering self-numbering qualification

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.