Forums before death by AOL, social media and spammers... "We can't have nice things"
|    comp.databases.ms-sqlserver    |    Notorious Rube Goldberg contraption    |    19,505 messages    |
[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]
|    Message 18,668 of 19,505    |
|    Erland Sommarskog to bill    |
|    Re: Combine Multiple Rows into one (deno    |
|    14 Jun 12 21:49:44    |
   
   ed619d2f   
   From: esquel@sommarskog.se   
      
   bill (billmaclean1@gmail.com) writes:   
   > I have definitely not seen the part at the end where the sub-query is   
   > aliased as g, and then a parenthesized letter (y) is put at the end   
   >   
   > It appears that g.y becomes a column, but the syntax is confusing to   
   > me.   
      
   Rather than writing   
      
    (SELECT somecol, cnt = COUNT(*)   
    FROM tbl   
    GROUP BY somecol) AS T   
      
   You can write   
      
    (SELECT somecol, COUNT(*)   
    FROM tbl   
    GROUP BY somecol) AS T (somecol, cnt)   
      
   You don't see this very often, but sometimes there is no choice and   
   this is such a case:   
      
   > CROSS APPLY (   
   > SELECT DISTINCT ',' + CAST(SubID AS VARCHAR(11))   
   > FROM @Sample AS s   
   > WHERE s.ID = i.ID   
   > ORDER BY ',' + CAST(SubID AS VARCHAR(11))   
   > FOR XML PATH('')   
   > ) AS g (y)   
      
   FOR XML means that you want the result as an XML document. FOR XML   
   PATH is the best method to use when you want to customise the XML   
   document. The argument to PATH is the root tag, but as you see from   
   the example, you can leave it out. And if you have a single column   
   with no name you get a concatenated string. Add a column alias in   
   the subquery and see what happens!   
      
      
      
   --   
   Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se   
      
   Links for SQL Server Books Online:   
   SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx   
   SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   
|
[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]
(c) 1994, bbs@darkrealms.ca