简体   繁体   中英

How to make a group with multiple column

I want to repeat first row first column value in every row in a particular column like a group. Please let me know how i will do it?

Table

    Id - Title 
    1  - ABC1
    2  - ABC2
    3  - ABC3
    4  - ABC4
    5  - ABC5

I want result like this in a simple select query, Please dont use variable for it means storing first row value in a variable,

Resultant Table

Table

Id - Title - Group
1  - ABC1  - 1
2  - ABC2  - 1
3  - ABC3  - 1
4  - ABC4  - 1
5  - ABC5  - 1

该查询将有助于获得预期的结果。

SELECT Id, Title, (SELECT TOP 1 Id FROm Table ORDER BY ID) AS [Group]  FROM Table

USE THE QUERY: (ORDER BY ID ASC will make sure that the first ID will selected always even if there is a change of order in ID.)

SELECT Id, Title,
       (SELECT TOP 1 ID FROM Table ORDER BY ID ASC) 
AS [Group]  FROM Table

Something like:

SELECT Id, Title, 1 AS [Group] FROM Table

If the Group value is different, then we need to know where that value is coming from.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM