SQL table data horizontally using PIVOT - sql

My SQL table is
GUID Step_ID Value
----------------------------------------------
ADFE12-ASDER-... 1 10
ADFE12-ASDER-... 2 20
ADFE12-ASDER-... 3 30
ADFE12-ASDER-... 4 160
CD4563-FG567-... 1 20
CD4563-FG567-... 2 80
Q23RT5-GH678... 1 30
Q23RT5-GH678-... 2 80
Q23RT5-GH678-... 3 20
And Expected result should be
GUID 1 2 3 4
---------------------------------------------------
ADFE12-ASDER-... 10 20 30 160
CD4563-FG567-... 20 80 NULL NULL
Q23RT5-GH678-... 30 80 20 NULL
Here I need to get the details on the basis of column whose data type is GUID. I tried using PIVOT table but getting an exception because I cannot use an aggregate function on GUID column. Is there any other alternative or approach I can use to get the above desired result.

Try this:
select [GUID],[1],[2],[3],[4]
from
(
select [GUID], Step_ID, Value
from test
) d
pivot
(
max(Value)
for Step_ID in ([1],[2],[3],[4])
) piv;
SQL FIDDLE DEMO

You could try manually pivotting, I don't have a SQL Server handy to check the behavior on a GUID but this has worked well for me in the past.
select guid,
max(case when step_ID = 1 then value else null end) step_1,
max(case when step_ID = 2 then value else null end) step_2,
max(case when step_ID = 3 then value else null end) step_3,
max(case when step_ID = 4 then value else null end) step_4
from your_table
group by guid;
HTH

Related

query a table with multiple rows for same id, into single data row in results

I have a few tables like this where a person has multiple data rows. The IDs are sequential but do not always start at 1. Is there a way to have the results come out in a single data row for each person. I have a few tables like this and I ultimately would like to join them via CLIENT_ID, but I'm a bit stumped. Is this possible?
Using oracle sql.
CLIENT_ID
NAME
ID
ID_DESCRIPTION
5
joe
1
apple
5
joe
5
orange
68
brian
2
orange
68
brian
6
mango
68
brian
10
lemon
12
katie
3
watermelon
where the results look like this
CLIENT_ID
NAME
ID1
ID1_DESCRIPTION
ID2
ID2_DESCRIPTION
ID3
ID3_DESCRIPTION
5
joe
1
apple
5
orange
68
brian
2
orange
6
mango
10
lemon
12
katie
3
watermelon
If Pivot ist not available, this should do it:
Select
Client_id,
sum(case when id_description='apple' then 1 else 0 end) as Apples,
sum(case when id_description='orange' then 1 else 0 end) as Oranges...
[]etc.
from
t
group by Client_ID
Might need some minor tweaking as I wrote this just off the top of my head, but something like this should work. Will say this doesn't account for more than 3 rows per CLIENT_ID. For that, would need to do a dynamic pivot (plenty of online articles on this topic).
Pivoting Based on Order of Items
WITH cte_RowNum AS (
SELECT ROW_NUMBER() OVER (PARTITION BY CLIENT_ID ORDER BY ID) AS RowNum
,*
FROM YourTable
)
SELECT CLIENT_ID
,MAX(CASE WHEN RowNum = 1 THEN ID END) AS ID1
,MAX(CASE WHEN RowNum = 1 THEN [Description] END) AS ID1_DESCRIPTION
,MAX(CASE WHEN RowNum = 2 THEN ID END) AS ID2
,MAX(CASE WHEN RowNum = 2 THEN [Description] END) AS ID2_DESCRIPTION
,MAX(CASE WHEN RowNum = 3 THEN ID END) AS ID3
,MAX(CASE WHEN RowNum = 3 THEN [Description] END) AS ID3_DESCRIPTION
FROM cte_RowNum
GROUP BY CLIENT_ID;

TSQL Sum with type [duplicate]

This question already has answers here:
SQL Server dynamic PIVOT query?
(9 answers)
Closed 5 years ago.
I'm writing using translate
Table1
id number type
1 6 111
1 5 111
1 6 113
2 3 112
2 6 111
i need to sum group by value of "type" and "id"
the result I want to see
RESULT Table
id type111 type112 typ113
1 11 0 6
2 6 3 0
Another way to use pivoting:
SELECT id,
ISNULL([111],0) as Type111,
ISNULL([112],0) as Type112,
ISNULL([113],0) as Type113
FROM (
SELECT id,
number,
[type]
FROM Table1
) AS SourceTable
PIVOT (SUM(number) FOR [type] IN ([111], [112], [113])
) AS PivotTable;
Output:
id Type111 Type112 Type113
----------- ----------- ----------- -----------
1 11 0 6
2 6 3 0
(2 rows affected)
In case when the variety of types is high - use dynamic SQL to build and execute query.
Try using SUM with CASE:
SELECT ID,
SUM(CASE WHEN type = 111 THEN number END) AS type111,
SUM(CASE WHEN type = 112 THEN number END) AS type112,
SUM(CASE WHEN type = 113 THEN number END) AS typE113,
FROM TABLE1
GROUP BY ID

SQL Server Rowcount of column value

I am trying to write a simple SQL code that can get the desired output, from this table -
Original Table :
id | type
123 0
123 1
123 1
345 0
345 0
What I'm trying to get is:
id | zeroCount | oneCount
123 1 2
345 2 0
I tried to group by the id and also type but, both of them gives the same thing!
How to get desired output?
Here is a simple way, assuming the values are only 0 and 1:
select id, sum(1 - type) as zerocount, sum(type) as onecount
from t
group by id;
A more typical approach would use case (or even pivot):
select id, sum(case when type = 0 then 1 else 0 end) as zerocount,
sum(case when type = 1 then 1 else 0 end) as onecount
from t
group by id;
You could get fancy and use a pivot function. But this will work:
Select id,
sum(case when type = 0 then 1 else 0 end) as ZeroCount,
Sum(case when type = 1 then 1 else 0 end) as OneCount
from table
group by id
order by id

Sql Result two columns

I have the following table:
Full name status
ricardo 1 2
ricardo 2 4
How do I make a select to return like this:
name totalstatus1 totalstatus2 total
ricardo 2 4 6
You did not include the name of the column with the 2 and 4 but you could use something similar to this:
select name,
sum(case when status = 1 then value end) totalStatus1,
sum(case when status = 2 then value end) totalStatus2,
sum(value) Total
from yourtable
group by name;
See SQL Fiddle with Demo

Group By Questions

Here is a table of profile answers:
profile_id | answer_id
----------------------
1 1
1 4
1 10
Here is a table which contains a list of responses by poll respondents:
user_id | answer_id
-------------------
1 1
1 9
2 1
2 4
2 10
3 14
3 29
I want to return a list of users whose answer was in (6,9) but also in(1,10), basically all of the answers that match profile 1.
How can I write this select query?
I tried the following, but apparently I don't quite understand how group by works:
SELECT DISTINCT [user_id]
FROM [user_question_answers] a
GROUP BY a.[user_id]
HAVING a.[answer_id] IN (6,9)
AND a.[answer_id] IN (1,10)
EDIT: Return user_id 1 only
Your query is close . . .
SELECT [user_id]
FROM [user_question_answers] a
GROUP BY a.[user_id]
HAVING max(case when a.[answer_id] IN (6,9) then 1 else 0 end) = 1
AND max(case when a.[answer_id] IN (1,10) then 1 else 0 end) = 1