A question about SQL query statements

Source: Internet
Author: User

The following fields are available in a table:
Assume that the table name is manage and the field name is Dan.
The Dan field has the following values:

Double
Single
Single
Single
Double
Double
Single

Question: How can I query the single-row first several times in the Dan field, then the dual-keys separated several times, and then the single

 

I used a subquery to simulate the cursor ~
After the following results are generated, a single order is generated several times in a row, and then a pair is generated several times. If a single order is displayed, you can simply summarize it, perform the following query as a derived table ~

SQL code
   Declare @ t table (ID int, Dan varchar (20) insert @ t select 1, 'dual' Union all select 2, 'singles' Union all select 3, 'Single 'Union all select 4, 'double' Union all select 5, 'double' Union all select 6, 'singles' select Dan, count1 = (select count (1) + 1 from @ t B where Dan =. dan and ID = A.ID-1) from @ T

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.