Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy is this query returning unwanted results?
    primarykey
    data
    text
    <p>Good morning,</p> <p>I have a problem with this query:</p> <pre><code>SELECT P.txt_nome AS Pergunta, IP.nome AS Resposta, COUNT(*) AS Qtd FROM tb_resposta_formulario RF INNER JOIN formularios F ON F.id_formulario = RF.id_formulario INNER JOIN tb_pergunta P ON P.id_pergunta = RF.id_pergunta INNER JOIN tb_resposta_formulario_combo RFC ON RFC.id_resposta_formulario = RF.id_resposta_formulario INNER JOIN itens_perguntas IP ON IP.id_item_pergunta = RFC.id_item_pergunta WHERE RF.id_formulario = 2 GROUP BY P.txt_nome, IP.nome </code></pre> <p>This is the actual result of this query:</p> <p>|Pergunta| Resposta |Qtd|<br> |Produto |Combo 1MB | 3 |<br> |Produto |Combo 2MB | 5 |<br> |Produto |Combo 4MB | 1 |<br> |Produto |Combo 6MB | 1 |<br> |Produto |Combo 8MB | 4 |<br> |Região |MG | 3 |<br> |Região |PR | 2 |<br> |Região |RJ | 3 |<br> |Região |SC | 1 |<br> |Região |SP | 5 | </p> <p>These are the results I was expecting:</p> <p>|Produto | Região |Qtd|<br> |Combo 1MB | MG | 3 |<br> |Combo 2MB | SP | 5 |<br> |Combo 4MB | SC | 1 |<br> |Combo 6MB | RJ | 1 |<br> |Combo 8MB | PR | 2 | </p> <p>I am using the PIVOT and UNPIVOT operators but the result is not satisfactory.</p> <p>Has anyone already faced this situation before? Do you have any insight you can offer?</p> <p>I already analyzed these links:</p> <ul> <li><a href="https://stackoverflow.com/questions/213702/sql-server-2005-pivot-on-unknown-number-of-columns">SQL Server 2005 Pivot on Unknown Number of Columns</a></li> <li><a href="https://stackoverflow.com/questions/152770/transpose-a-set-of-rows-as-columns-in-sql-server-2000">Transpose a set of rows as columns in SQL Server 2000</a></li> <li><a href="https://stackoverflow.com/questions/428041/sql-server-2005-turn-columns-into-rows">SQL Server 2005, turn columns into rows</a></li> <li><a href="https://stackoverflow.com/questions/159456/pivot-table-and-concatenate-columns-sql-problem#159803">Pivot Table and Concatenate Columns</a> </li> <li><a href="https://stackoverflow.com/questions/198716/pivot-in-sql-2005#199763">PIVOT in sql 2005</a></li> </ul> <p>Att, Pelegrini</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload