English 中文(简体)
如何计算 在表格一栏中,根据计算结果分类
原标题:How to count same Id in a column in a table and based on count perform concatenation
  • 时间:2009-12-02 08:01:10
  •  标签:
  • sql
  • tsql

我的表格如下:

ID Name 1 Amit 2 Mohit 2 Nahar

我的乐观态度是提出一个问题,把这一表格更新到以下格式中。

ID Name 1 Amit 2 Mohit OR NAHAR

是否有能够解决这一问题的疑问?

最佳回答

引言

DECLARE @Table TABLE(       
        ID INT,
        NAME VARCHAR(MAX)
)
    INSERT INTO @Table (ID,[NAME]) SELECT 1,  Amit 
INSERT INTO @Table (ID,[NAME]) SELECT 2,  Mohit 
INSERT INTO @Table (ID,[NAME]) SELECT 2,  Nahar 
INSERT INTO @Table (ID,[NAME]) SELECT 3,  C 
INSERT INTO @Table (ID,[NAME]) SELECT 3,  D 
INSERT INTO @Table (ID,[NAME]) SELECT 3,  E 

www.un.org/Depts/DGACM/index_spanish.htm 学历:

select id,
        REPLACE(stuff((select  ,  +      + name  +      
            from @Table b
            where b.id = a.id
            FOR xml path(  )),1,1,   ), , , OR ) MergedData
from @Table a
group by a.id 

<<>Output>:

id  MergedData
1      Amit  
2      Mohit  OR  Nahar

www.un.org/Depts/DGACM/index_spanish.htm 学历(关于更改要求)

select distinct a.id,

    case when coalesce(x.cnt,0) <= 2 then

        REPLACE(stuff((select  ,  +      + name  +      
            from @Table b
            where b.id = a.id
            FOR xml path(  )),1,1,   ), , , OR ) 


      when x.cnt > 2 then 

      REPLACE(stuff((select  ,  +      + name  +      
            from @Table b
            where b.id = a.id
            FOR xml path(  )),1,1,   ), , , AND ) 

            end

            MergedData
from @Table a

left join 

(select id,COUNT(id) cnt
from @Table 
group by ID
having (COUNT(id)>1))x
on a.ID = x.ID

<<>Output>:

id  MergedData
1      Amit  
2      Mohit  OR  Nahar  
3      C  AND  D  AND  E  
问题回答

你们可以尝试这样一些事情,以获得名字。

DECLARE @Table TABLE(
        ID INT,
        NAME VARCHAR(MAX)
)

DECLARE @TempTable TABLE(       
        ID INT,
        NAME VARCHAR(MAX)
)

INSERT INTO @Table (ID,[NAME]) SELECT 1,  A 
INSERT INTO @Table (ID,[NAME]) SELECT 2,  B 
INSERT INTO @Table (ID,[NAME]) SELECT 2,  C 

DECLARE @ID INT
DECLARE Cur CURSOR FOR 
SELECT  DISTINCT 
        ID
FROM    @Table

OPEN Cur
FETCH NEXT FROM Cur INTO @ID

WHILE @@FETCH_STATUS = 0
BEGIN
    DECLARE @Name VARCHAR(MAX)
    SET @Name = NULL
    SELECT  @Name = COALESCE(@Name +   OR   + NAME, Name)
    FROM    @Table
    WHERE   ID = @ID
    INSERT INTO @TempTable (ID,[NAME]) SELECT @ID, @Name
    FETCH NEXT FROM Cur INTO @ID
END

CLOSE Cur
DEALLOCATE Cur

SELECT * FROM @TempTable




相关问题
How to write this T-SQL WHERE condition?

I ve got two tables: TableA Col1 Col2 TableB Col3 Col4 I want to join them together: SELECT * from TableA join TableB ON (...) Now, in place of ... I need to write an expression ...

Customer and Order Sql Statement

TSQL query to select all records from Customer that has an Order and also select all records from customer that does not have an Order. The table Customer contains a primary key of CustomerID. The ...

Recommended way of querying multiple Versioned tables

Have a win 2003 box with MSSQL 2005 running on it. There is a database which is populated every morning with new/modified SalesOrder made the previous day. The database has several tables: SalesOrder, ...

update duplicate record

I have a table with the following fields Id Name IsPublic i need to write a sql query that updates IsPublic to false where name has a duplicate. Only one of the duplicates should have IsPublic = ...

Define variable to use with IN operator (T-SQL)

I have a Transact-SQL query that uses the IN operator. Something like this: select * from myTable where myColumn in (1,2,3,4) Is there a way to define a variable to hold the entire list "(1,2,3,4)"? ...

Selecting records during recursive stored procedure

I ve got a content management system that contains a hierarchical structure of categories, with sub-categories subject to different ordering options at each level. Currently, that s retrieved by a (...

热门标签