我的表格如下:
ID Name 1 Amit 2 Mohit 2 Nahar
我的乐观态度是提出一个问题,把这一表格更新到以下格式中。
ID Name 1 Amit 2 Mohit OR NAHAR
是否有能够解决这一问题的疑问?
我的表格如下:
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
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 ...
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 ...
I have a stored procedure which takes an XML parameter and inserts the data into multiple tables. If I run the stored procedure into a database using a SSMS query window, everything works fine. ...
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, ...
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 = ...
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)"? ...
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 (...
I m trying to transform the SQL Query below into Linq to SQL select Categorias.IdCategoria, Categorias.Nome, SUM(lancamentos.valor) from lancamentos left outer join Categorias on Lancamentos....