group multiple rows into single row multiple columns - sql

My data tables look like this:
User table:
UserID Parameter Value
1 1 1
2 1 1
1 2 2
Parameter Table:
ID Name
1 "Age"
2 "Gender"
Value Table:
ParameterID ValueID Name
1 1 "17"
2 1 "Male"
Is it possible to group the user table into single rows with multiple columns to look something like this?
UserID Age Gender
1 1 1

Related

Oracle sql view rows that id exist and doesn't exist

I have two tables: "Orders", "Names"
Some orders doesn't exist in "Names", but I need to get them too.
Example:
Orders:
ID
1
2
3
Names
ID
Name
1
Name1
2
Name2
If ID from Orders doesn't exist in Names, column Name will be null
Result:
ID
Name
1
Name1
2
Name2
3
If I try "WHERE ORDERS.ID = NAMES.ID", ID number 3 doesn't match, so it will not show up.

DB2-Find the count of number of occurrences of different values for each primary key in a table

I have a table like
ID
value
1
ABC
1
ABC
1
ABCD
2
ABC
Column 2 can either have ABC or ABCD
Now I want to find the count for values in column 2 for each ID
ID
count (ABC)
count (ABCD)
1
2
1
2
1
0
How to write DB2 query to get the output in the above format for count?
Found a solution
Select ID, sum(decode(value, 'ABC',1,0)) as count_abc
, sum(decode(value, 'ABCD',1,0)) as count_abcd
from table
group by ID

How can I select a table skipping duplicated value postgreSQL

I have a table like this.
id
grade_1
grade_2
createdAt
1
1
1
20220304
2
1
1
20220301
3
4
2
20220228
I want to select the current row(in here, id=1) and a row where the grade's value is different with the row I selected.(in here, id=3)
Like This
id
grade_1
grade_2
createdAt
1
1
1
20220304
3
4
2
20220228
I tried to use subquery but it doesn't really worked for me. Is there any way to skip the duplicated value when selecting table?
You can just do it with group by and a max value to retieve the one you want
SELECT
grade_1,
grade_2,
Max(createdAt)
from
yourTable
Group by
grade_1,
grade_2

distinct value row from the table in SQL

There is a table with values as below,
Id Value
1 1
2 1
3 2
4 2
5 3
6 4
7 4
now need to write a query to retrieve value from the table and output should look as
ID Value
1 1
3 2
5 3
6 4
any suggestion ?
The query you want is nothing to do with being distinct, it's a simple aggregation of value with the minimum ID for each:
select Min(id) Id, value
from table
group by value

Matching two variables to create a new ID

I'm trying to create an SQL statement to match either an id number or a postcode and then assign a new id number
What I want to end up with is ‘newid’ that correctly recognizes that the first four records are the same person (even though the postcode for record 2 is different).
record id postcode newid
--------------------------
1 1 1 1
2 1 2 1
3 1 1 1
4 2 1 1
5 3 3 2
Any suggestions would be appreciated greatly.
Going based on your example:
SELECT RECORD,
(SELECT MIN (ID)
FROM users u2
WHERE users.id IN (u2.id, u2.postcode)
OR users.postcode in (u2.id, u2.postcode)
) AS newid
FROM users
This results with the following data:
RECORD NEWID
------------------
1 1
2 1
3 1
4 1
5 3
Here is the SQLFiddle