[SQL Server] field merging

Source: Internet
Author: User

Actual Stickers:

Http://topic.csdn.net/u/20100720/10/c0b95705-308b-4ef4-b086-77b0c4c04522.html

 

 

Http://topic.csdn.net/u/20100802/14/8be14ec7-e560-4541-9d06-0a1a9bec17b7.html

 

-- Create a test environment
If object_id ('tb') is not null drop table TB
Go
Create Table TB
(
Id int identity,
Housename varchar (10 ),
Proid int,
ProCOUNT int
)
Go
Insert TB
Select 'a warehouse ', 1,100 union all
Select 'a warehouse ', 2, 50 Union all
Select 'B warehouse', 1, 60 Union all
Select 'B warehouse', 2, 70
-- Query
-- 1
If object_id ('f _ getproid ') is not null drop function f_getproid
Go
Create Function f_getproid (@ housename varchar (10 ))
Returns varchar (50)
As
Begin
Declare @ s varchar (50)
Select @ s = isnull (@ s + ',', '') + ltrim (proid) from TB where housename = @ housename
Return @ s
End
Go
If object_id ('f _ getprocount ') is not null drop function f_getprocount
Go
Create Function f_getprocount (@ housename varchar (10 ))
Returns varchar (50)
As
Begin
Declare @ s varchar (50)
Select @ s = isnull (@ s + ',', '') + ltrim (ProCOUNT) from TB where housename = @ housename
Return @ s
End
Go
Select housename, DBO. f_getproid (housename) proid, DBO. f_getprocount (housename) ProCOUNT from TB group by housename
Go
-- 2
Select housename,
Stuff (select ',' + ltrim (proid) from TB where housename = T. housename for XML Path (''), 1, 1,'') proid,
Stuff (select ',' + ltrim (ProCOUNT) from TB where housename = T. housename for XML Path (''), 1, 1,'') ProCOUNT
From TB t
Group by housename

-- Result
/*

(Four rows affected)
Housename proid ProCOUNT
-----------------------
Warehouse A, 50
Warehouse B, 70

(2 rows affected)

Housename proid ProCOUNT
-----------------------
Warehouse A, 50
Warehouse B, 70

(2 rows affected)

*/

 

 

 

 

/*
Title: Merge strings by a field (simple merge)
Author: AI xinjue Luo. Xin Hua)
Time:
Location: Shenzhen, Guangdong Province

Description: merges the following data into the value field by ID.
ID value
-----------
1 aa
1 bb
2 aaa
2 bbb
2 ccc
Expected results:
ID value
-----------------
1 aa, BB
2 AAA, BBB, CCC
That is: group by ID, sum of values (string addition)
*/
-- 1. SQL2000 can only be solved using custom functions
Create Table Tb (ID int, value varchar (10 ))
Insert into TB values (1, 'A ')
Insert into TB values (1, 'bb ')
Insert into TB values (2, 'aaa ')
Insert into TB values (2, 'bbb ')
Insert into TB values (2, 'ccc ')
Go

Create Function DBO. f_str (@ id int) returns varchar (100)
As
Begin
Declare @ STR varchar (1000)
Set @ STR =''
Select @ STR = @ STR + ',' + Cast (value as varchar) from TB where id = @ ID
Set @ STR = right (@ STR, Len (@ Str)-1)
Return @ Str
End
Go

-- Call a function
Select ID, value = DBO. f_str (ID) from TB group by ID

Drop function DBO. f_str
Drop table TB

-- 2. Methods in sql2005
Create Table Tb (ID int, value varchar (10 ))
Insert into TB values (1, 'A ')
Insert into TB values (1, 'bb ')
Insert into TB values (2, 'aaa ')
Insert into TB values (2, 'bbb ')
Insert into TB values (2, 'ccc ')
Go

Select ID, [value] = stuff (select ',' + [value] from TB t where id = Tb. ID for XML Path (''), 1, 1 ,'')
From TB
Group by ID

Drop table TB

-- 3. Use a cursor to merge data
Create Table Tb (ID int, value varchar (10 ))
Insert into TB values (1, 'A ')
Insert into TB values (1, 'bb ')
Insert into TB values (2, 'aaa ')
Insert into TB values (2, 'bbb ')
Insert into TB values (2, 'ccc ')
Go
Declare @ t table (ID int, value varchar (100) -- Define the result set table variable
-- Define and merge the cursor
Declare my_cursor cursor local
Select ID, value from TB
Declare @ id_old int, @ ID int, @ value varchar (10), @ s varchar (100)
Open my_cursor
Fetch my_cursor into @ ID, @ Value
Select @ id_old = @ ID, @ s =''
While @ fetch_status = 0
Begin
If @ ID = @ id_old
Select @ s = @ s + ',' + Cast (@ value as varchar)
Else
Begin
Insert @ T values (@ id_old, stuff (@ s, 1, 1 ,''))
Select @ s = ',' + Cast (@ value as varchar), @ id_old = @ ID
End
Fetch my_cursor into @ ID, @ Value
End
Insert @ T values (@ id_old, stuff (@ s, 1, 1 ,''))
Close my_cursor
Deallocate my_cursor

Select * From @ t
Drop table TB

Related Article

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.