Author |
Topic |
raysefo
Constraint Violating Yak Guru
260 Posts |
Posted - 2013-05-13 : 09:26:31
|
Hi,I have 2 tables as follows:CREATE TABLE [dbo].[A]( [MatchID] [nvarchar](100) NULL, [DrawName] [nvarchar](100) NULL, [MatchCode] [nvarchar](100) NULL, [SpecialEventCode] [nvarchar](100) NULL, [TeamNames] [nvarchar](100) NULL, [StartDate] [datetime] NULL, [Round] [nvarchar](100) NULL, [Status] [nvarchar](50) NULL, [StatusID] [nvarchar](50) NULL) ON [PRIMARY]CREATE TABLE [dbo].[B]( [MatchID] [nvarchar](100) NULL, [MatchCode] [nvarchar](100) NULL, [SpecialEventCode] [nvarchar](100) NULL, [IncidentID] [nvarchar](100) NULL, [Team] [nvarchar](10) NULL, [Minute] [nvarchar](10) NULL, [Score] [nvarchar](50) NULL, [deleteInfo] [nvarchar](10) NULL, [Player] [nvarchar](100) NULL) ON [PRIMARY] When I use a query as follows:SELECT Team, Minute, Player FROM [dbo].[B] bWHERE MatchID =(SELECT MatchID FROM [dbo].[LS_A] where SpecialEventCode =1 and StartDate <= GETDATE() and StatusID in (2,3,4,8,9))order by Minute asc Here is a sample output:Team Minute Player1 44 Cenk1 55 Cenk2 58 Harun Is there a way to group by the players with same name according to teams? I mean I would like to get the output as follows:Team Minute Player1 44,58 Cenk2 58 Harun |
|
stepson
Aged Yak Warrior
545 Posts |
Posted - 2013-05-13 : 09:59:36
|
[code];with T AS( select 1 as Team, '44' as Minute, 'Cenk' as Player union all select 1 as Team, '55' as Minute, 'Cenk' as Player union all select 2 as Team, '58' as Minute, 'Harun' as Player)select T.Team , MM.[Minute] as Minutes , T.Player from (select T.Team,T.Player from T group by T.Team,T.Player) as T CROSS APPLY (select T2.Minute + ' , ' from T as T2 where T2.Player=T.Player and T2.Team=T.Team for xml path('') ) as MM(Minute)[/code]Ce-am pe mine am si-n dulap, cand ma-mbrac zici ca ma mutsabinWeb |
|
|
raysefo
Constraint Violating Yak Guru
260 Posts |
Posted - 2013-05-14 : 02:16:40
|
Hi,I need to use the where clause of this query also;SELECT MatchID FROM [dbo].[LS_A] where SpecialEventCode =1 and StartDate <= GETDATE() and StatusID in (2,3,4,8,9) |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-05-14 : 02:44:13
|
[code];With CTEAS(SELECT Team, Minute, Player FROM [dbo].[B] bWHERE MatchID =(SELECT MatchID FROM [dbo].[LS_A] where SpecialEventCode =1 and StartDate <= GETDATE() and StatusID in (2,3,4,8,9)))SELECT c.Team,STUFF((SELECT ',' + CAST([Minute] AS varchar(5)) FROM CTE WHERE Team = c.Team AND Player = c.Player ORDER BY [Minute] ascFOR XML PATH('')),1,1,'') AS [Minute],c.PlayerFROM (SELECT DISTINCT Team,Player FROM CTE) c[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
raysefo
Constraint Violating Yak Guru
260 Posts |
Posted - 2013-05-14 : 02:49:28
|
visakh16 saved the day again. Thank you. By the way do you have linkedin account? I would like to add you. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-05-14 : 02:53:45
|
quote: Originally posted by raysefo visakh16 saved the day again. Thank you. By the way do you have linkedin account? I would like to add you.
yep I'vehttp://in.linkedin.com/in/visakhsqlmvp------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
raysefo
Constraint Violating Yak Guru
260 Posts |
Posted - 2013-05-14 : 02:59:45
|
I sent a friend request. May the query above cause any performance problems? |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-05-14 : 03:04:26
|
quote: Originally posted by raysefo I sent a friend request. May the query above cause any performance problems?
Ok...i'll accept when i check it later in the dayIts one of the optimized methods for concatenating column values compared to traditional UDF etc approaches------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
raysefo
Constraint Violating Yak Guru
260 Posts |
Posted - 2013-05-14 : 03:07:27
|
thank you for your help. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-05-14 : 03:40:26
|
you're welcome------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
raysefo
Constraint Violating Yak Guru
260 Posts |
Posted - 2013-05-14 : 04:28:44
|
Hi visakh16,Can I have single quote after minutes as follows??Team Minute Player1 44',55' Cenk2 58' Harun |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-05-14 : 05:09:14
|
quote: Originally posted by raysefo Hi visakh16,Can I have single quote after minutes as follows??Team Minute Player1 44',55' Cenk2 58' Harun
yep you can modify like below;With CTEAS(SELECT Team, Minute, Player FROM [dbo].[B] bWHERE MatchID =(SELECT MatchID FROM [dbo].[LS_A] where SpecialEventCode =1 and StartDate <= GETDATE() and StatusID in (2,3,4,8,9)))SELECT c.Team,STUFF((SELECT ',' + CAST([Minute] AS varchar(5)) + ''''FROM CTE WHERE Team = c.Team AND Player = c.Player ORDER BY [Minute] ascFOR XML PATH('')),1,1,'') AS [Minute],c.PlayerFROM (SELECT DISTINCT Team,Player FROM CTE) c ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
raysefo
Constraint Violating Yak Guru
260 Posts |
Posted - 2013-05-14 : 05:23:06
|
thank you again |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-05-14 : 05:26:33
|
welcome as always ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
raysefo
Constraint Violating Yak Guru
260 Posts |
Posted - 2013-05-15 : 04:08:53
|
Hi visakh16,Now the query does NOT order by Minute asc. Should I add ORDER BY [Minute] asc at the end of the query again? |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-05-15 : 04:11:20
|
you mean ordering inside comma separated list or ordering in final result?if former, then earlier suggestion is enough.if latter make query like;With CTEAS(SELECT Team, Minute, Player FROM [dbo].[B] bWHERE MatchID =(SELECT MatchID FROM [dbo].[LS_A] where SpecialEventCode =1 and StartDate <= GETDATE() and StatusID in (2,3,4,8,9)))SELECT c.Team,STUFF((SELECT ',' + CAST([Minute] AS varchar(5)) + ''''FROM CTE WHERE Team = c.Team AND Player = c.Player ORDER BY [Minute] ascFOR XML PATH('')),1,1,'') AS [Minute],c.PlayerFROM (SELECT DISTINCT Team,Player FROM CTE) cORDER BY [Minute] ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
raysefo
Constraint Violating Yak Guru
260 Posts |
Posted - 2013-05-15 : 04:13:09
|
thank you |
|
|
raysefo
Constraint Violating Yak Guru
260 Posts |
Posted - 2013-05-29 : 14:37:21
|
Hi visakh16,How can I join another table to the query above? This table is same as table B. Is there a way to join this table also?CREATE TABLE [dbo].[C]( [MatchID] [nvarchar](100) NULL, [MatchCode] [nvarchar](100) NULL, [SpecialEventCode] [nvarchar](100) NULL, [IncidentID] [nvarchar](100) NULL, [Team] [nvarchar](10) NULL, [Minute] [nvarchar](10) NULL, [Score] [nvarchar](50) NULL, [deleteInfo] [nvarchar](10) NULL, [Player] [nvarchar](100) NULL) ON [PRIMARY]Best Regards. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-05-30 : 01:20:23
|
do you mean this?;With CTEAS(SELECT Team, Minute, Player FROM [dbo].[B] bWHERE MatchID =(SELECT MatchID FROM [dbo].[LS_A] where SpecialEventCode =1 and StartDate <= GETDATE() and StatusID in (2,3,4,8,9))UNION ALLSELECT Team, Minute, Player FROM [dbo].[C] cWHERE MatchID =(SELECT MatchID FROM [dbo].[LS_A] where SpecialEventCode =1 and StartDate <= GETDATE() and StatusID in (2,3,4,8,9)))SELECT c.Team,STUFF((SELECT ',' + CAST([Minute] AS varchar(5)) + ''''FROM CTE WHERE Team = c.Team AND Player = c.Player ORDER BY [Minute] ascFOR XML PATH('')),1,1,'') AS [Minute],c.PlayerFROM (SELECT DISTINCT Team,Player FROM CTE) cORDER BY [Minute] if C contains the same records as B and if you want unique ones only then use UNION instead of UNION ALL------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
raysefo
Constraint Violating Yak Guru
260 Posts |
Posted - 2013-05-30 : 01:53:20
|
thank you again. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-05-30 : 01:56:41
|
welcome------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
Next Page
|