如何折叠此结果集中的这些字段和行

时间:2014-01-27 01:25:47

标签: sql sql-server tsql

我已经编写了这个查询来聚合来自几个表的数据,我希望结果集按caseID分组,但是如果有多行(有可能,我想做一些字段组合。

例如,我希望将特定案例的所有牙齿数组合成一个逗号分隔的tring和所有具有该牙齿束的caseID记录。我还想要MaterialID的最小值,并让所有具有相同caseID的记录显示

我有一个更新似乎对牙齿的部分更新,但不处理所有记录。

有点像这个例子(下面的SQL中有比这更多的字段):

CaseID  Material    Tooth
12345   1   12
12345   2   13
12345   2   1
12345   4   5

CaseID  Material    Tooth
12345   1   1,5,12,13
12345   1   1,5,12,13
12345   1   1,5,12,13
12345   1   1,5,12,13

这是我的SQL:

    IF OBJECT_ID ('tempdb..#tScanRemake') IS NOT NULL DROP TABLE #tScanRemake

Create Table #tScanRemake
(
ScanOrderUID VarChar( 100)
,ModelElementUID VarChar (100)
,ToothElementUID VarChar (100)
,ScanOrderCaseID VarChar (50)
,CasesCaseID VarChar (50)
,RemakeID Varchar (20)
,ReasonID VarChar (10)
,Reason VarChar (50)
,IntOrderID VarChar (100)
,ExtOrderID VarChar (100)
,OperatorName VarChar (100)
,ScanSource VarChar (50)
,DatabaseID INT
,MachineGroupID INT
,ToothNumber Varchar (50)
,Shade Varchar (20)
,Anterior_Posterior Varchar (20)
,Items  VarChar (50)
,IsBridge Bit
,ScanMaterialID INT
,ProcessStatusID  VarChar (150)
,DateIn DateTime
,DateInvoiced DateTime
,DoctorID Varchar (20)
,DoctorFirstName Varchar (50)
,DoctorLastName Varchar (50)
,PatientFirstName Varchar (50)
,PatientLastName Varchar (50)
)

Insert Into #tScanRemake

SELECT distinct top 1000
 so.ScanOrderUID
,se. ModelElementUID
,st. ToothElementUID
,so. CaseID ScanOrderCaseID
,c. CaseID
,c. RemakeID
,c. ReasonID
,r. Reason
,so. IntOrderID
,so. ExtOrderID
,so. OperatorName
,so. ScanSource
,so. DatabaseID
,so. MachineGroupID
,st. ToothNumber
,c. Shade
,CASE WHEN GDLControl.dbo. fn_HasAnteriorTeeth(GDLControl .dbo. fn_ConvertCSVToBitMaskTally(GDLControl .dbo. fn_ParseTeethStrings(st .ToothNumber), ',')) = 1
     THEN 'Anterior'
     ELSE 'Posterior'
 END AS Anterior_Posterior
,se. Items
,CASE WHEN se. Items like '%Bridge%'
       THEN 1
       ELSE 0
 END as IsBridge  
,se. ScanMaterialID
,so. ProcessStatusID
,c. DateIn
,c. DateInvoiced
,c. DoctorID
,d. FirstName as DoctorFirstname
,d. LastName as DoctorLastname
,c. PatientFirst
,c. PatientLast

FROM 
[3ShapeScanOrders] AS so WITH (NOLOCK)
Left Outer Join
[LS-DLPlus].[DLPlus] .[dbo]. [Cases] AS c  WITH (NOLOCK )
       ON so. CaseID =  c .RemakeOf  AND c.remakeof >''
Left outer   JOIN
[LS-DLPlus].[DLPlus] .[dbo]. [Reasons] AS r  WITH (NOLOCK )
    ON c. ReasonID = R .ReasonID
Left Outer JOIN
[LS-DLPlus].[DLPlus] .[dbo]. [Doctors] AS d  WITH (NOLOCK )
    ON d. DoctorID = c .DoctorID
INNER JOIN
[3ShapeScanModelElements] AS se WITH (NOLOCK)
       ON so. ScanOrderUID = se .ScanOrderUID
INNER JOIN
[3ShapeScanToothElements] AS st WITH (NOLOCK)
       ON so. ScanOrderUID = st .ScanOrderUID      

Where
so.CaseID >''

--Insert Into [GDLDatamart].[dbo].[Shape3ScanRemake]

--Select
--ScanOrderUID
--,ModelElementUID
--,ToothElementUID
--,ScanOrderCaseID
--,Remakeof
--,ReasonID
--,Reason
--,IntOrderID
--,ExtOrderID
--,OperatorName
--,ScanSource
--,DatabaseID
--,MachineGroupID
--,ToothElementTypeID
--,ToothNumber
--,Items 
--,ModelHeight
--,ScanMaterialID
--,VirtualItem
--,ProcessStatusID 
--,OrderasofLocalDTS
--,OrdersasofUTC
----,OrdersModifiedLocalDTS
----,OrdersModifiedUTC
--,ElementasofLocalDTS
--,ElementasofUTC
--,ToothasofLocalDTS
--,ToothasofUTC

Select *
from #tScanRemake

--Where ScanordercaseID = 1014535612
--Group by ScanOrdercaseID

--Go
--Update ToothString    
UPDATE tsr
SET tsr. ToothNumber = tsr .ToothNumber + ',' + t2. ToothNumber
FROM #tScanRemake tsr
     CROSS JOIN #tScanRemake t2
WHERE Exists (SELECT Distinct ScanOrderUID , ToothNumber
               FROM #tScanRemake t
               WHERE
                 ToothNumber < tsr. ToothNumber
                 AND ScanOrderUID = tsr.ScanOrderUID ) 

--Go

--Select *
--from #tScanRemake
--Where ScanOrderUID = '97D7FB5F-3129-E211-931A-001D09F2CB34'

1 个答案:

答案 0 :(得分:0)

SELECT 
caseid, 
MIN(material) Min_Material,
(select  CAST(tooth AS VARCHAR(10)) + ',' from tt b WHERE a.caseid=b.caseid FOR XML PATH('') ) list
FROM tt a
GROUP BY caseid
  • tt是你的桌子