SQL select rows into new columns - sql

I have a database table which looks like this:
ID parameter value
1 A 1
1 B 1002
2 A 5
2 B 1055
I would like to create a SQL query to receive such a table:
ID value of parameter A value of parameter B
1 1 1002
2 5 1055
How can I transform the table to create a new columns for each parameter with it corresponding value?

You can combine CASE with any aggregation function (like SUM(), MAX(), etc.) to pivot the data manually.
For example:
select
id,
sum(case when parameter = 'A' then value end) as a,
sum(case when parameter = 'B' then value end) as b,
...
sum(case when parameter = 'Z' then value end) as z
from t
group by id

Related

Group Matching Values and Finding Which Ones Are Missing an Associated Value?

I need help with writing a query to generate a result that will provide me with all record numbers NOT assigned to Group D from a table structured like the below example. From the below table my desired result would be record number "3" .
Record_Number Assigned_To_Group
1 A
1 B
1 C
1 D
2 A
2 E
2 D
3 A
3 B
3 E
One method uses aggregation:
select Record_Number
from t
group by Record_Number
having sum(case when Assigned_To_Group = 'D' then 1 else 0 end) = 0;

Create a new column based on existing columns inside a case statement

Say I have this table t:
id value
1 1 10
2 2 3
3 1 55
4 1 20
5 2 98
When drawing from t I want to add a column value2 that equals value when id == 2, otherwise 0
I tried
select id, value, max(case when id = 2 then value else 0) from t
but it did not work
Not sure why you included a max in your attempt but based on your description, this is all you should need.
select id, value, case when id = 2 then value else 0 end as value2
from t;
It sounds like you want a conditional window aggregate:
select
id,
value,
max(case when id = 2 then value end) over ()
from t;

convert cell value to respective column in PostgreSQL

Here's the sample:
select * from tmp
--output
A B Value
---------------------
a x 1
b x 2
a y 3
b y 4
c y 5
After a SQL command grouping on B column, I'd like to make each value of column A to be a separate column as illustrated below:
B a b c
----------------------------
x 1 2 null
y 3 4 5
If there any specific terminology for this transformation? Thanks!
You need to find max of other value and group it by with anchor column(b in your case). Please note that your column count should be similar to number of values expected in field A.
select b,
max(case when A='a' then Value else null end)a,
max(case when A='b' then Value else null end)b,
max(case when A='c' then Value else null end)c
from tmp
group by 1

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

mysql rows to columns, text type

If i have a table of data like
Option_id|Component_id|Option_parent|Option_name|Option_value
1 1 0 id
2 1 1 option1 Some value
3 1 1 option2 Other
4 1 0 id Value
5 1 4 option1 More
6 1 4 option2 More&More
Is it possible to return rows with the option_name as columns when providing the "option_name" to select and the component_id. The option_name with "id" will be the parent using it's "option_id".
So Select option1, option2 where Component_id = 1 returns
Option1 |Option2
Some Value Other
More More&More
I'm basically trying to see if i can have a generic table that can be used by components to store varying amounts of data. I know i can use joins but wondered if there might be a better way as one component could have 10 options.
Use:
SELECT MAX(CASE WHEN t.option_name = 'option1' THEN t.option_value END) AS option1,
MAX(CASE WHEN t.option_name = 'option2' THEN t.option_value END) AS option2
FROM TABLE t
WHERE t.option_name IN ('option1', 'option2')
GROUP BY t.component_id, t.option_parent