0
Hello. I am building a query where I need to use pivot to analyze the results, but it does not return the necessary data.
Original table:
select * from tbCampoAdicionalTicket where TCK_SEQUENCIAL = 415945
I need the CAT_VALOR column to become a single row. However, I find some problems when creating the pivot:
1 - The column TCK_SEQUENCIAL exists through a foreign key, and I cannot call it in the pivot select.
2 - The CAT_VALOR column is of the VARCHAR type and I cannot convert it because there are cases with text.
Below, the closest I could create from the pivot, but the result returns a table with all NULL values:
SELECT TCK_SEQUENCIAL,
[1] AS CAT_NUMERO_1,
[2] AS CAT_NUMERO_2,
[3] AS CAT_NUMERO_3,
[4] AS CAT_NUMERO_4
FROM tbCampoAdicionalTicket
PIVOT(
MAX(CAT_VALOR)
FOR CAT_NUMERO IN (
[1],
[2],
[3],
[4])) AS pivot_table
WHERE TCK_SEQUENCIAL = 415948
I would like the result to be similar to the photo, but with the column TCK_SEQUENCIAL together, because this table will be used in a previous:
From now on I thank you all!
Hello alive puts a table with the expected result, to be clearer.
– Ernesto Casanova
You have here some examples of PIVOT, check if help.
– Ernesto Casanova
Ernesto, follow a better example image. The first image is my original table, the second is the pivot, but I need to group it in a single line
– Iago Gomes
Which SGDB this using?
– David