从SQL Server

时间:2015-07-16 12:20:51

标签: sql sql-server

帮我查询SQL查询。我会从第一个查询获得所有值,如'01,02,03,20,92,93,94',但在两个查询中不是函数MAX。结果:在b_BookNum的对面,我会看到一个字符串:'01 ,02,03,20,92,93,94'

enter image description here

1 个答案:

答案 0 :(得分:0)

只需取出MAX()功能,然后在您的小组中添加此列,快乐的日子......

SELECT  [b_BookNum]
       ,[b_CHMAT]
       ,COUNT(*) iCount
FROM MPA.dbo.SCHM_Books
GROUP BY [b_BookNum] ,[b_CHMAT]
ORDER BY [b_BookNum]

修改

我很惊讶您拥有解决问题所需的所有信息,但您无法解决问题。无论如何,这是完整的解决方案:

;WITH CTE AS (
SELECT  [b_BookNum]
       ,COUNT(*) iCount
FROM MPA.dbo.SCHM_Books
GROUP BY [b_BookNum] 
)
SELECT [b_BookNum]
      ,STUFF((SELECT ',' + CAST([b_CHMAT] AS VARCHAR(10))
      FROM dbo.SCHM_Books 
      WHERE [b_BookNum] = C.[b_BookNum]
      FOR XML PATH(''),TYPE)
      .value('.','NVARCHAR(MAX)'),1,1,'') AS [b_CHMAT]
      ,iCount
FROM CTE C