2015-10-13 20 views
5

Ho una tabella in cui ogni colonna è una domanda e le righe sono le risposte che possono assumere il valore da 1 a 4TSQL occorrenze di conteggio in più colonne

Qual è il modo più efficiente per calcolare le occorrenze di ogni risposta per ogni domanda?

tabella di input

q1 q2 q3 
1 3 1 
2 1 4 
1 2 1 

tabella di output desiderato

answer q1 q2 q3 

    1 2 0 2 
    2 1 1 0 
    3 0 1 0 
    4 0 0 1 

Finora sono arrivato la seguente (per la domanda Q3), ma è solo per una questione

CREATE TABLE #t 
    (
    answer int 
) 
insert into #t (answer) values (1) 
insert into #t (answer) values (2) 
insert into #t (answer) values (3) 
insert into #t (answer) values (4) 

select * into #q3 from (select q3 as q3,count(*) as occurenceq3 
        from [table] 
        group by q3) as x 

select t.answer,tb.occurenceq3 as occurenceq3 
from #t t left join #q3 tb on t.answer=tb.Q3 

drop table #q3 
drop table #t 
+0

Probabilmente UNPIVOT poi perno. – shawnt00

risposta

4
select answer, q1, q2, q3 
from 
    q 
    unpivot (answer for q in (q1, q2, q3)) as upvt 
    pivot (count(q) for q in (q1, q2, q3)) as pvt 

Ho commesso l'errore di provare prima count(*) ma penso che abbia senso che l'aggregazione debba essere esplicitamente sulla colonna girevole, anche se penso che sarebbero logicamente equivalenti.

+0

hmm non compilato: sintassi errata vicino a '*'. – user2418209

+0

Forse non è possibile fare riferimento a una directory della tabella di base. Stavo cercando di eseguirlo su SQL Fiddle ma il sito si comporta in modo strano come al solito. – shawnt00

+0

Se si torna al post originale e si sostituisce il conteggio (*) con count (q), la query funzionerà. –

1

Questo dovrebbe funzionare: -

CREATE TABLE #question (q1 int, q2 int, q3 int) 
INSERT INTO #question 
VALUES 
(1,3,1), 
(2,1,4), 
(1,2,1); 

--unpivot to start with 
WITH 
UNPIVOTED AS 
(
SELECT * 
FROM 
    (SELECT q1,q2,q3 
    FROM #question) p 
UNPIVOT 
    (answer FOR question in (q1,q2,q3)) AS unpvt 
) 

--Then pivot 
SELECT * FROM 
(SELECT answer, question FROM unpivoted) p 
PIVOT 
(
COUNT(question) 
FOR question IN (q1,q2,q3) 
) as pvt 
0

Se si desidera che il modo più efficace, vorrei suggerire di mettere un indice su ciascuna delle colonne "Q" e l'esecuzione della query come:

select a.answer, 
     (select count(*) from #question q where q.q1 = a.answer) as q1, 
     (select count(*) from #question q where q.q2 = a.answer) as q2, 
     (select count(*) from #question q where q.q3 = a.answer) as q3 
from (select 1 as answer union all select 2 union all select 3 union all select 4 
    ) a; 

Questo utilizza fondamentalmente gli indici per il conteggio dei valori e dovrebbe essere abbastanza veloce, più veloce dell'aggregazione di tutti i risultati non quotati.

0

Ciò funzionerà

select t1.dis, 
q1=(select count(q1) from CountAnswers where q1=t1.dis), 
q2=(select count(q2) from countAnswers where q2=t1.dis), 
q3=(select count(q3) from countAnswers where q3=t1.dis) 
from (select dis from(select q1 as dis from CountAnswers union select q2 as dis from CountAnswers union select q3 as dis from CountAnswers)mytab)t1; 
Problemi correlati