Merge two rows in SQL based on a key - sql

I have the table as below:
FK | Field1 | Field2 | Feild3
============================
3 | Y | N |N
3 | N | Y |N
I want the result to be like this. please help:
FK | Field1 | Field2 | Feild3
================================
3 | Y | Y | N

select fk, max(field1), max(field2)
from your_table
group by fk

Select fk, Max(field1), max(field2) from table group by fk
In this case you can just use max to return the highest value for each column.

Related

Insert value into a table when strings match conditions from another table

I have two tables in a PostgreSQL database. Table2 has an FK from table1's PK. I want to search table1 for specific strings, and if I find matches I want to update a column in table2 with a string.
Table1
+----+------+------+------+
| PK | Col1 | Col2 | Col3 |
+----+------+------+------+
| 1 | A | x | x |
| 2 | x | x | x |
| 3 | x | A | x |
| 4 | x | x | x |
| 5 | x | x | A |
+----+------+------+------+
Table2
+----+-----------------+
| FK | matching_column |
+----+-----------------+
| 1 | string |
| 2 | |
| 3 | string |
| 4 | |
| 5 | string |
+----+-----------------+
So where table1 contains '%A%'
update table2 with 'string'
I'm not sure where to start on this one. Does anyone have a solution?
Use a subquery:
UPDATE table2
SET matching_column = 'string'
WHERE fk = (SELECT pk
FROM table1
WHERE "Col1" LIKE '%A%'
OR "Col2" LIKE '%A%'
OR "Col3" LIKE '%A%');
You could use the update ... set ... from syntax. If I followed you correctly, you want:
update table2 t2
set t2.matching_column = 'string'
from table 1 t1
where
t1.pk = t2.fk
and 'A' in (t1.col1, t1.col2, t1.col3)
This phrases as: if the fk of table2 exists in table1 and one of the 3 columns contains (col1, col2, col3) contains 'A', then set column matching_column in the corresponding record in table2 to 'string'.

find records having more than one distinct colum value

Given table t1 with columns Id ( text, primary key ) and place (text) like below.
+-------+-----------+
| Id | place |
+-------+-----------+
| abcde | Santori |
| bcdef | Krypt |
| cdefg | Bali |
| defgh | Bangkok |
| abcde | Colombo |
+-------+-----------+
I need to find out the records for Ids having more than one distinct place. In the above example the output shall be
+-------+-----------+
| Id | place |
+-------+-----------+
| abcde | Santori |
| abcde | Colombo |
+-------+-----------+
I would use exists :
select t.*
from table t
where exists (select 1 from table t1 where t1.id = t.id and t1.place <> t.place);
I think it is OK for you:
SELECT ID, PLACE FROM T1 as A
WHERE A.ID IN
(SELECT ID FROM T1 AS B
GROUP BY ID
HAVING count(*) > 1
)
In a subquery, you need to get count of distinct place and get the ID. And then use an outer query to fetch all records.
Fiddle Example
select * From T1
where T1.ID in
(select ID from T1
group by ID
having count(distinct PLACE) > 1
)

Multirow Pivot Oracle 11g

I need to bring back the values in a long table as wide while preserving duplicate rows:
IE: My table:
Colname | ColValue | PK
Field1 | 12 | 1
Field2 | apple | 1
Field3 | blue | 1
Field3 | Red | 1
What I want:
PK | Field1| Field2 | Field3
1 |12 | apple |blue
1 |12 | apple |red
I can't seem to figure out how to make a pivot work in this situation as to do any max(colvalue) then removes the second Field 3 value. A dynamic column solution would be wonderful as I do not necessarily know the names of the fields/columns.
What I have:
select PK as CompPK,
ColName,
Colvalue
from test1)
Pivot(
max(colvalue) for ColNamein ('Field1' as Field1,'Field2' as Field2,'Field3' as Field3)
This returns of course just:
PK | Field1| Field2 | Field3
1 |12 | apple |blue
I do have a time stamp column and other 'random' columns on this table.
Edit:
Yes. Theoretically I want all possible iterations.
Try
select * from(
Select pk, colvalue as field1
From table1
Where colname ='Field1'
) T1
Full Join (
Select pk, colvalue as field2
From table1
Where colname ='Field2'
) T2
Using (pk)
Full join (
Select pk, colvalue as field3
From table1
Where colname ='Field3'
) T3
Using (pk)
Demo http://sqlfiddle.com/#!4/ec147/3
| PK | FIELD1 | FIELD2 | FIELD3 |
|----|--------|--------|--------|
| 1 | 12 | apple | blue |
| 1 | 12 | apple | Red |

Getting values from all JSON fields in postgres

I need a SQL query where I can select all json keys. The following query let's me get all the keys to the JSON field. But I'm a bit at loss how I would go about making a query to also get all the values out too.
SELECT DISTINCT ON (key.*) key.*
FROM my_table,
jsonb_object_keys(my_table.json_field) as key
So the result of the above query would simply just be
key1
key2
With the following query you would get a result similar to this
SELECT * FROM my_table
| id | json_field |
| -- | ---------- |
| 1 | '{"key1": "value1"}' |
| 2 | '{"key2": "value2"}' |
The result I'm looking for would be the following
| id | key1 | key2 |
| -- | -------| ------ |
| 1 | value1 | null |
| 2 | null | value2 |
What makes it difficult is that I don't know the names of all keys which also may be a lot of keys for a single row.
select distinct on (field_1, field_2) id, job_id, field_1, field_2
from
my_table,
jsonb_populate_recordset(json_field) jprs (field_1 int, field2 text)
https://www.postgresql.org/docs/current/static/functions-json.html#FUNCTIONS-JSON-PROCESSING-TABLE

... where count(col) > 1

I have a table like this:
+-----+-----+-------+
| id | fk | value |
+-----+-----+-------+
| 0 | 1 | peter |
| 1 | 1 | josh |
| 3 | 2 | marc |
| ... | ... | ... |
I'd like now to get all entries which have more than one value.
The expected result would be:
+-----+-------+
| fk | count |
+-----+-------+
| 1 | 2 |
| ... | ... |
I tried to achieve that like this:
select fk, count(value) from table where count(value) > 1;
But Oracle didn't like it.
So I tried this...
select * from (
select fk, count(value) as cnt from table
) where cnt > 1;
...with no success.
Any ideas?
Use the having clause for comparing aggregates.
Also, you need to group by what you're aggregating against for the query to work correctly. The following is a start, but since you're missing a group by clause still it won't quite work. What exactly are you trying to count?
select fk, count(value)
from table
group by fk
having count(value) > 1;