sql >> Database teknologi >  >> RDS >> Sqlserver

hvordan man går gennem en tabel for at finde datasæt?

Du kan nemt gøre dette ved at pivoting dataene. Det kan gøres på to måder.

1. Brug Conditional Aggregate at pivotere dataene. Efter pivoting du kan finde datediff mellem forskellige stadier. Prøv dette.

SELECT orderid,Received,Keyed,Printed,Delivered,
       Datediff(minute, Received, Keyed)    TurnTime1,
       Datediff(minute, Keyed, Printed)     TurnTime2,
       Datediff(minute, Printed, Delivered) TurnTime3
FROM  (SELECT OrderID,
              Max(CASE WHEN ActivityID = 1 THEN ActivityDate END) Received,
              Max(CASE WHEN ActivityID = 2 THEN ActivityDate END) Keyed,
              Max(CASE WHEN ActivityID = 3 THEN ActivityDate END) Printed,
              Max(CASE WHEN ActivityID = 4 THEN ActivityDate END) Delivered
       FROM   Yourtable
       GROUP  BY OrderID)A 
 

2.brug Pivot at transponere dataene

SELECT orderid, [1] AS Received, [2] AS Keyed, [3] AS Printed, [4] AS Delivered, Datediff(minute, [1], [2]) TurnTime1, Datediff(minute, [2], [3]) TurnTime2, Datediff(minute, [3], [4]) TurnTime3 FROM Yourtable PIVOT (Max(ActivityDate) FOR ActivityID IN([1],[2],[3],[4]))piv

  1. Trin-for-trin-procedure til at migrere data fra Excel til MySql-databasetabel

  2. SQLite til Oracle

  3. Duplikationer af en relationstabel

  4. Hvordan tester man MySQL-transaktioner?