且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在SQL Server中分组几行?

更新时间:2023-02-14 13:00:31

如果您可以依靠ID列来排序组(或其他行的组合,如code1 ,code2),那么你可以用几种不同的方式。



如果你的服务器是2012+,那么你可以使用LAG()窗口函数访问以前的行和如果前面的行Code1与当前行相同,Code1将其替换为null(或者如果一个空字符串更适合你,则为空字符串)。但是,如果您使用的是版本< 2012,那么你可以使用自联接完成它。



这种格式化可能更好地处理客户端(或报告层),如果可以。 p>

下面的查询包括这两个版本,但我注释掉了自连接的东西:

  SELECT 
Table1.ID,

- CASE WHEN Table1.Code1 = t1.Code1 THEN NULL ELSE Table1.Code1 END AS Code1,
CASE WHEN LAG Table1.Code1)OVER(ORDER BY Table1.ID)= Table1.Code1 THEN NULL ELSE Table1.Code1 END AS Code1,

Table1.Code2,Table1.Details,Table1.IDS,

- CASE WHEN Table1.Name = t1.Name THEN NULL ELSE Table1.Name END AS名称,
CASE WHEN LAG(Table2.Name)OVER(ORDER BY Table1.ID)= Table2.Name THEN NULL ELSE Table2.Name END AS名称

FROM
表1
INNER JOIN
Table2 ON Table1.Code1 = Table2.Code1
- LEFT JOIN Table1 t1 ON Table1.ID = t1.ID + 1
WHERE
Table1.IDS = 1
ORDER BY
Table1.Code1,Table1.Code2

示例SQL Fiddle


I have this query:

SELECT 
    Table1.ID, Table1.Code1, Table1.Code2, Table1.Details, 
    Table1.IDS, Table2.Name 
FROM 
    Table1 
INNER JOIN 
    Table2 ON Table1.Code1 = Table2.Code1 
WHERE 
    Table1.IDS = 1 
ORDER BY 
    Table1.Code1, Table1.Code2

This is my result for query:

ID    Code1   Code2   Details   IDS    Name
1     1001     01       D1       1      N1
2     1001     01       D2       1      N1
3     1001     02       D3       1      N1
4     1001     05       D4       1      N1
5     1002     11       D5       1      N2
6     1002     12       D6       1      N2
7     1005     21       D7       1      N3
8     1005     21       D8       1      N3

But I want this result:

ID    Code1   Code2   Details   IDS    Name
1     1001     01       D1       1      N1
2              01       D2       1
3              02       D3       1
4              05       D4       1
5     1002     11       D5       1      N2
6              12       D6       1
7     1005     21       D7       1      N3
8              21       D8       1

How do I get this result? Please help me. Thanks a lot

If you can rely on the ID column for ordering the groups (or a combination of other rows, like code1,code2) then you can do this in a few different ways.

If your server is 2012+ then you can use the LAG() window function to access previous rows and if the previous rows Code1 is the same as the current rows Code1 replace it with null (or an empty string if that suits you better). However, if you're using a version < 2012 then you can accomplish it using a self join.

This kind of formatting might be better to handle on the client side (or reporting layer) though if can.

The query below includes both versions, but I commented out the self-join stuff:

SELECT 
    Table1.ID,

    -- CASE WHEN Table1.Code1 = t1.Code1 THEN NULL ELSE Table1.Code1 END AS Code1,
    CASE WHEN LAG(Table1.Code1) OVER (ORDER BY Table1.ID) = Table1.Code1 THEN NULL ELSE Table1.Code1 END AS Code1,

    Table1.Code2, Table1.Details, Table1.IDS, 

    -- CASE WHEN Table1.Name = t1.Name THEN NULL ELSE Table1.Name END AS Name,
    CASE WHEN LAG(Table2.Name) OVER (ORDER BY Table1.ID) = Table2.Name THEN NULL ELSE Table2.Name END AS Name

FROM 
    Table1 
INNER JOIN 
    Table2 ON Table1.Code1 = Table2.Code1 
-- LEFT JOIN Table1 t1 ON Table1.ID = t1.ID + 1 
WHERE 
    Table1.IDS = 1 
ORDER BY 
    Table1.Code1, Table1.Code2

Sample SQL Fiddle