Server SQL:Esempi di pivoting di dati stringa
-
09-06-2019 - |
Domanda
Cercando di trovare alcuni semplici esempi PIVOT di SQL Server.La maggior parte degli esempi che ho trovato riguardano il conteggio o la somma di numeri.Voglio solo ruotare alcuni dati di stringa.Ad esempio, ho una query che restituisce quanto segue.
Action1 VIEW
Action1 EDIT
Action2 VIEW
Action3 VIEW
Action3 EDIT
Vorrei utilizzare PIVOT (se possibile) per ottenere risultati in questo modo:
Action1 VIEW EDIT
Action2 VIEW NULL
Action3 VIEW EDIT
Questo è possibile anche con la funzionalità PIVOT?
Soluzione
Ricorda che la funzione di aggregazione MAX funzionerà sia sul testo che sui numeri.Questa query richiederà la scansione della tabella solo una volta.
SELECT Action,
MAX( CASE data WHEN 'View' THEN data ELSE '' END ) ViewCol,
MAX( CASE data WHEN 'Edit' THEN data ELSE '' END ) EditCol
FROM t
GROUP BY Action
Altri suggerimenti
Se desideri utilizzare specificamente la funzione PIVOT di SQL Server, dovrebbe funzionare, presupponendo che le due colonne originali siano chiamate act e cmd.(Non è poi così bello da vedere però.)
SELECT act AS 'Action', [View] as 'View', [Edit] as 'Edit'
FROM (
SELECT act, cmd FROM data
) AS src
PIVOT (
MAX(cmd) FOR cmd IN ([View], [Edit])
) AS pvt
Configurazione della tabella:
CREATE TABLE dbo.tbl (
action VARCHAR(20) NOT NULL,
view_edit VARCHAR(20) NOT NULL
);
INSERT INTO dbo.tbl (action, view_edit)
VALUES ('Action1', 'VIEW'),
('Action1', 'EDIT'),
('Action2', 'VIEW'),
('Action3', 'VIEW'),
('Action3', 'EDIT');
Il tuo tavolo:SELECT action, view_edit FROM dbo.tbl
Interrogazione senza utilizzare PIVOT:
SELECT Action,
[View] = (Select view_edit FROM tbl WHERE t.action = action and view_edit = 'VIEW'),
[Edit] = (Select view_edit FROM tbl WHERE t.action = action and view_edit = 'EDIT')
FROM tbl t
GROUP BY Action
Interrogazione utilizzando PIVOT:
SELECT [Action], [View], [Edit] FROM
(SELECT [Action], view_edit FROM tbl) AS t1
PIVOT (MAX(view_edit) FOR view_edit IN ([View], [Edit]) ) AS t2
Entrambe le query risultano:
Da http://blog.sqlauthority.com/2008/06/07/sql-server-pivot-and-unpivot-table-examples/:
SELECT CUST, PRODUCT, QTY
FROM Product) up
PIVOT
( SUM(QTY) FOR PRODUCT IN (VEG, SODA, MILK, BEER, CHIPS)) AS pvt) p
UNPIVOT
(QTY FOR PRODUCT IN (VEG, SODA, MILK, BEER, CHIPS)
) AS Unpvt
GO
Bene, per il tuo campione e per quelli con un numero limitato di colonne univoche, questo dovrebbe bastare.
select
distinct a,
(select distinct t2.b from t t2 where t1.a=t2.a and t2.b='VIEW'),
(select distinct t2.b from t t2 where t1.a=t2.a and t2.b='EDIT')
from t t1
With pivot_data as
(
select
action, -- grouping column
view_edit -- spreading column
from tbl
)
select action, [view], [edit]
from pivot_data
pivot ( max(view_edit) for view_edit in ([view], [edit]) ) as p;
Ho avuto una situazione in cui stavo analizzando le stringhe e le prime due posizioni della stringa in questione sarebbero i nomi dei campi di uno standard di codifica delle indicazioni sanitarie.Quindi eliminerei le stringhe e otterrei valori per F4, UR e UQ o quant'altro.Questo è stato ottimo per uno o più record per un utente.Ma quando volevo vedere centinaia di record e i valori per tutti gli utenti, dovevo essere un PIVOT.Questo è stato meraviglioso soprattutto per esportare molti record per eccellere.La richiesta di segnalazione specifica che avevo ricevuto era "ogni volta che qualcuno presentava una richiesta per Benadryl, quale valore presentava nei campi F4, UR e UQ.Avevo un OUTER APPLY che creava ColTitle e i campi valore sottostanti
PIVOT(
min(value)
FOR ColTitle in([F4], [UR], [UQ])
)
Puoi usarlo con il pivoting:
With pivot_data as
(
select
action, -- grouping column
view_edit -- spreading column
from tbl
)
select action, [view], [edit]
from pivot_data
pivot ( max(view_edit) for view_edit in ([view], [edit]) ) as p;