mysql rows to columns, text type - sql

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

Related

SQL select rows into new columns

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

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;

SQL - Impala - How to unfold one categorical column into many?

I have the following table :
user category number
1 A 8
1 B 6
2 A 1
2 C 9
3 B 5
I want to "unfold" or "dummify" the category column and fill them with the "number" column to obtain:
user cat_A cat_B cat_C
1 8 6 0
2 1 0 9
3 0 5 0
Is it possible to achieve this in SQL (Impala) ?
I found this question How to create dummy variable columns for thousands of categories in Google BigQuery?
However it seems a little bit complex and I'd rather do it in Pandas.
Is there a simpler solution, knowing that I have 10 categories (A, B, C, D etc)?
You can try to use condition aggregate function.
SELECT user,
SUM(CASE WHEN category = 'A' THEN number ELSE 0 END) cat_A,
SUM(CASE WHEN category = 'B' THEN number ELSE 0 END) cat_B,
SUM(CASE WHEN category = 'C' THEN number ELSE 0 END) cat_C
FROM T
GROUP BY user

Creating a select to 'de-normalise' the data within a table

I put the work de-normalise in quote marks, because it might not be the right way of putting it, but not too sure how else to describe it...
I have the following table
Source Priority Attribute
A 1 Name
B 2 Name
C 3 Name
A 1 Address
B 2 Address
C 3 Address
A 2 Email
B 3 Email
C 1 Email
I would like my select to return:
Source Name_Pri Addr_Pri Email_Pri
A 1 1 2
B 2 2 3
C 3 3 1
Thanks
You are looking for a pivot. I often do this using conditional aggregation:
select source,
max(case when attribute = 'Name' then priority end) as name_priority,
max(case when attribute = 'Address' then priority end) as address_priority,
max(case when attribute = 'Email' then priority end) as email_priority
from t
group by source;

SQL QUERY MERGE TWO ROW DATA

Suppose My Database is like this :
MemberName MemberID ResultsEligibilityID
Thuso 2 1
Thuso 2 1
Maubane 3 2
Maubane 3 1
CDeveloper 5 2
CDeveloper 5 2
Now is it possible to write a query to display (The Below output) based on this:
if both ResultsEligibilityID for a single Member is 1 then Eligibile,
Otherwise Non-Eligible.
OUTPUT
MemberName MemberID ResultsEligibilityID Results
Thuso 2 1 Eligible
Maubane 3 2 Non-Eligible
CDeveloper 5 2 Non-Eligible
Thanks in advance for the help.
Please try:
select
MemberName,
MemberID,
MAX(ResultsEligibilityID) ResultsEligibilityID ,
(case when sum(case when ResultsEligibilityID=1 then 1 else 0 end)= COUNT(*)
then 'Eligible' else 'Non-Eligible' end) Results
From
YourTable
group by MemberName,MemberID