{"id":1498,"date":"2015-07-14T20:35:39","date_gmt":"2015-07-14T18:35:39","guid":{"rendered":"http:\/\/www.capri-soft.de\/blog\/?p=1498"},"modified":"2016-03-16T15:07:57","modified_gmt":"2016-03-16T14:07:57","slug":"ms-sql-server-concat-strings-in-group-by-clausel-group-by-strings-gruppieren","status":"publish","type":"post","link":"https:\/\/www.capri-soft.de\/blog\/?p=1498","title":{"rendered":"MS SQL Server: Concat Strings in Group By Clausel \/ Group By, Strings gruppieren"},"content":{"rendered":"<h1>Problem<\/h1>\n<p>Strings sollen in einer Group By Klausel anhand eines Attributs gruppiert und Komma separiert dargestellt werden.<\/p>\n<p>Beispiel:<\/p>\n<table border=1>\n<thead>\n<tr>\n<td>ID<\/td>\n<td>Name<\/td>\n<td>Value<\/td>\n<\/tr>\n<\/thead>\n<tr>\n<td>1<\/td>\n<td>A<\/td>\n<td>4<\/td>\n<\/tr>\n<tr>\n<td>1<\/td>\n<td>B<\/td>\n<td>8<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>C<\/td>\n<td>9<\/td>\n<\/tr>\n<\/table>\n<h1>Approach &#8211; Ansatz<\/h1>\n<p>Durch Verwendung der XPath Funktionen l\u00e4sst sich etwas kreatives erreichen&#8230; <\/p>\n<h1>Solution &#8211; L\u00f6sung<\/h1>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nCREATE TABLE #YourTable (&#x5B;ID] INT, &#x5B;Name] CHAR(1), &#x5B;Value] INT)\r\n\r\nINSERT INTO #YourTable (&#x5B;ID],&#x5B;Name],&#x5B;Value]) VALUES (1,&#039;A&#039;,4)\r\nINSERT INTO #YourTable (&#x5B;ID],&#x5B;Name],&#x5B;Value]) VALUES (1,&#039;B&#039;,8)\r\nINSERT INTO #YourTable (&#x5B;ID],&#x5B;Name],&#x5B;Value]) VALUES (2,&#039;C&#039;,9)\r\n\r\nSELECT \r\n  &#x5B;ID],\r\n  STUFF((\r\n    SELECT &#039;, &#039; + &#x5B;Name] + &#039;:&#039; + CAST(&#x5B;Value] AS VARCHAR(MAX)) \r\n    FROM #YourTable \r\n    WHERE (ID = Results.ID) \r\n    FOR XML PATH(&#039;&#039;),TYPE).value(&#039;(.\/text())&#x5B;1]&#039;,&#039;VARCHAR(MAX)&#039;)\r\n  ,1,2,&#039;&#039;) AS NameValues\r\nFROM #YourTable Results\r\nGROUP BY ID\r\n\r\nDROP TABLE #YourTable\r\n<\/pre>\n<iframe src=\"http:\/\/www.facebook.com\/plugins\/like.php?href=https%3A%2F%2Fwww.capri-soft.de%2Fblog%2F%3Fp%3D1498&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light\" scrolling=\"no\" frameborder=\"0\" allowTransparency=\"true\" style=\"border:none; overflow:hidden; width:450px;margin-top:5px;\"><\/iframe>","protected":false},"excerpt":{"rendered":"<p>Problem Strings sollen in einer Group By Klausel anhand eines Attributs gruppiert und Komma separiert dargestellt werden. Beispiel: ID Name Value 1 A 4 1 B 8 2 C 9 Approach &#8211; Ansatz Durch Verwendung der XPath Funktionen l\u00e4sst sich etwas kreatives erreichen&#8230; Solution &#8211; L\u00f6sung CREATE TABLE #YourTable (&#x5B;ID] INT, &#x5B;Name] CHAR(1), &#x5B;Value] INT) &hellip; <a href=\"https:\/\/www.capri-soft.de\/blog\/?p=1498\" class=\"more-link\"><span class=\"screen-reader-text\">MS SQL Server: Concat Strings in Group By Clausel \/ Group By, Strings gruppieren<\/span> weiterlesen <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[10,11],"tags":[],"class_list":["post-1498","post","type-post","status-publish","format-standard","hentry","category-datenbanken","category-ms-sql-server"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p4yGeN-oa","jetpack_likes_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1498","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1498"}],"version-history":[{"count":2,"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1498\/revisions"}],"predecessor-version":[{"id":1501,"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1498\/revisions\/1501"}],"wp:attachment":[{"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1498"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1498"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1498"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}