SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Help me SQL Select


Help me SQL Select

Author
Message
vantuan02t1
vantuan02t1
SSC Veteran
SSC Veteran (210 reputation)SSC Veteran (210 reputation)SSC Veteran (210 reputation)SSC Veteran (210 reputation)SSC Veteran (210 reputation)SSC Veteran (210 reputation)SSC Veteran (210 reputation)SSC Veteran (210 reputation)

Group: General Forum Members
Points: 210 Visits: 47
I have sample data as below:
IDActivity Subject ProjectName Fullname CusName
2 Tuan NULL NULL Aquafina
2 Tuan NULL T1 NULL
2 Tuan Test NULL NULL
2 Tuan NULL NULL NULL
3 ABC NULL NULL NULL
3 ABC Test NULL NULL

And I want Select Result as below:
IDActivity Subject ProjectName Fullname CusName
2 Tuan Test Test T1 Aquafina
3 ABC Test NULL NULL

Every one, please.
Thank you very much.
Neeraj Prasad Sharma
Neeraj Prasad Sharma
Say Hey Kid
Say Hey Kid (685 reputation)Say Hey Kid (685 reputation)Say Hey Kid (685 reputation)Say Hey Kid (685 reputation)Say Hey Kid (685 reputation)Say Hey Kid (685 reputation)Say Hey Kid (685 reputation)Say Hey Kid (685 reputation)

Group: General Forum Members
Points: 685 Visits: 675
vantuan02t1 (7/17/2013)
I have sample data as below:
IDActivity Subject ProjectName Fullname CusName
2 Tuan NULL NULL Aquafina
2 Tuan NULL T1 NULL
2 Tuan Test NULL NULL
2 Tuan NULL NULL NULL
3 ABC NULL NULL NULL
3 ABC Test NULL NULL

And I want Select Result as below:
IDActivity Subject ProjectName Fullname CusName
2 Tuan Test Test T1 Aquafina
3 ABC Test NULL NULL

Every one, please.
Thank you very much.








check this solution :



Create table #t1 ( IDActivity int, Subject varchar(10) , ProjectName varchar(10), Fullname varchar(10), CusName varchar(10))


insert into #t1
Select 2, 'Tuan', NULL, NULL , 'Aquafina' union all
Select 2 , 'Tuan' ,NULL, 'T1' ,NULL union all
Select 2 , 'Tuan' ,'Test', NULL, NULL union all
Select 2, 'Tuan', NULL, NULL ,NULL union all
Select 3 , 'ABC', NULL, NULL , NULL union all
Select 3 , 'ABC', 'Test', NULL , NULL



select distinct IDActivity , (
Select distinct Subject From #t1 t2 where T2.Subject is not null And T2.IDActivity = T1.IDActivity
) Subject , (
Select distinct ProjectName From #t1 t3 where T3.ProjectName is not null And T3.IDActivity = T1.IDActivity
) ProjectName , (
Select distinct Fullname From #t1 t4 where T4.Fullname is not null And T4.IDActivity = T1.IDActivity
) Fullname , (
Select distinct CusName From #t1 t5 where T5.CusName is not null And T5.IDActivity = T1.IDActivity
) CusName

From #t1 t1

Neeraj Prasad Sharma
Sql Server Tutorials
HanShi
HanShi
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14546 Visits: 3765
If you want the highest value of multiple rows, this is also a working solution:
select
IDActivity
, MAX(Subject) as 'Subject'
, MAX(ProjectName) as 'ProjectName'
, MAX(Fullname) as 'Fullname'
, MAX(CusName) as 'CusName'
from table_name
group by IDActivity



** Don't mistake the ‘stupidity of the crowd’ for the ‘wisdom of the group’! **
vantuan02t1
vantuan02t1
SSC Veteran
SSC Veteran (210 reputation)SSC Veteran (210 reputation)SSC Veteran (210 reputation)SSC Veteran (210 reputation)SSC Veteran (210 reputation)SSC Veteran (210 reputation)SSC Veteran (210 reputation)SSC Veteran (210 reputation)

Group: General Forum Members
Points: 210 Visits: 47
Thank you very much, I done finished
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum







































































































































































SQLServerCentral


Search