Hstore column to get a single value but getting duplicate values - postgresql
I have a large SQL query where I'm trying to track changes for updates, deletes, and inserts. If there was an update I should get a single record that show's all the changes. for example if there was a unique update the return result set would be as follow:
+=====+==========+=========+========+===============+============+
| id | study_id | user_id | Action | updated_value | date |
+=====+==========+=========+========+===============+============+
| 123 | 456 | 966 | Update | opt_out: t | 07/21/2022 |
+=====+==========+=========+========+===============+============+
However, I am getting multiple duplicates for the same value for updated value.
+=====+==========+========+==================================+============+
| id | study_id | Action | updated_value | date |
+=====+==========+========+==================================+============+
| 123 | 456 | Update | opt_out: t opt_out: t opt_out: t | 07/21/2022 |
+=====+==========+========+==================================+============+
with CTE as (
----******UPDATES************
select a.row_file ->'file_id' as id,
ta.transaction_id,
concat('participant_id',':',' ',a.file_changes -> 'participant_id') as updated_value,
to_char(ta."timestamp", 'mm/dd/yyyy') as date
from logs.log_files a
join files.transactions ta on a.transaction_id = ta.transaction_id
where a.file_changes -> 'participant_id'is not null
and a."type" = 'U'
union
select a.row_file ->'file_id' as id,
ta.transaction_id,
concat('email',':',' ',a.file_changes -> 'email') as updated_value,
to_char(ta."timestamp", 'mm/dd/yyyy') as date
from logs.log_files a
join files.transactions ta on a.transaction_id = ta.transaction_id
where a.file_changes -> 'email' is not null
and a."type" = 'U'
union
select a.row_file ->'file_id' as id,
ta.transaction_id,
concat('name',':',' ',a.file_changes -> 'name') as updated_value,
to_char(ta."timestamp", 'mm/dd/yyyy') as date
from logs.log_files a
join files.transactions ta on a.transaction_id = ta.transaction_id
where a.file_changes -> 'name' is not null
and a."type" = 'U'
union
select a.row_file ->'file_id' as id,
ta.transaction_id,
concat('last_name',':',' ',a.file_changes -> 'last_name') as updated_value,
to_char(ta."timestamp", 'mm/dd/yyyy') as date
from logs.log_files a
join files.transactions ta on a.transaction_id = ta.transaction_id
where a.file_changes -> 'last_name' is not null
and a."type" = 'U'
union
select a.row_file ->'file_id' as id,
ta.transaction_id,
concat('phone',':',' ',a.file_changes -> 'phone') as updated_value,
to_char(ta."timestamp", 'mm/dd/yyyy') as date
from logs.log_files a
join files.transactions ta on a.transaction_id = ta.transaction_id
where a.file_changes -> 'phone' is not null
and a."type" = 'U'
union
select a.row_file ->'file_id' as id,
ta.transaction_id,
concat('gender',':',' ',a.file_changes, 'gender') as updated_value,
to_char(ta."timestamp", 'mm/dd/yyyy') as date
from logs.log_files a
join files.transactions ta on a.transaction_id = ta.transaction_id
where a.file_changes -> 'gender' is not null
and a."type" = 'U'
union
select a.row_file ->'file_id' as id,
ta.transaction_id,
concat('opt_out',':',' ',a.file_changes -> 'opt_out') as updated_value,
to_char(ta."timestamp", 'mm/dd/yyyy') as date
from logs.log_files a
join files.transactions ta on a.transaction_id = ta.transaction_id
where a.file_changes -> 'opt_out' is not null
and a."type" = 'U'
union
select a.row_file ->'file_id' as id,
ta.transaction_id,
concat('study_id',':',' ',a.file_changes -> 'study_id') as updated_value,
to_char(ta."timestamp", 'mm/dd/yyyy') as date
from logs.log_files a
join files.transactions ta on a.transaction_id = ta.transaction_id
where a.file_changes -> 'study_id' is not null
and a."type" = 'U'
union
select a.row_file ->'file_id' as id,
ta.transaction_id,
concat('createdAt',':',' ',a.file_changes -> 'createdAt') as Updated_Value,
to_char(ta."timestamp", 'mm/dd/yyyy') as date
from logs.log_files a
join files.transactions ta on a.transaction_id = ta.transaction_id
where a.file_changes -> 'createdAt' is not null
and "type" = 'U'
union
select a.row_file ->'file_id' as id,
ta.transaction_id,
concat('family_id',':',' ',a.file_changes -> 'family_id') as Updated_Value,
to_char(ta."timestamp", 'mm/dd/yyyy') as date
from logs.log_files a
join files.transactions ta on a.transaction_id = ta.transaction_id
where a.file_changes -> 'family_id' is not null
and "type" = 'U'
union
select a.row_file ->'file_id' as id,
ta.transaction_id,
concat('is_tester',':',' ',a.file_changes -> 'is_tester') as Updated_Value,
to_char(ta."timestamp", 'mm/dd/yyyy') as date
from logs.log_files a
join files.transactions ta on a.transaction_id = ta.transaction_id
where a.file_changes -> 'is_tester' is not null
and "type" = 'U'
union
select a.row_file ->'file_id' as id,
ta.transaction_id,
concat('updatedAt',':',' ',a.file_changes -> 'updatedAt') as Updated_Value,
to_char(ta."timestamp", 'mm/dd/yyyy') as date
from logs.log_files a
join files.transactions ta on a.transaction_id = ta.transaction_id
where a.file_changes -> 'updatedAt' is not null
and "type" = 'U'
union
select a.row_file ->'file_id' as id,
ta.transaction_id,
concat('utc_offset',':',' ',a.file_changes -> 'utc_offset') as Updated_Value,
to_char(ta."timestamp", 'mm/dd/yyyy') as date
from logs.log_files a
join files.transactions ta on a.transaction_id = ta.transaction_id
where a.file_changes -> 'utc_offset' is not null
and "type" = 'U'
union
select a.row_file ->'file_id' as id,
ta.transaction_id,
concat('private_id',':',' ', a.file_changes -> 'private_id') as Updated_Value,
to_char(ta."timestamp", 'mm/dd/yyyy') as date
from logs.log_files a
join files.transactions ta on a.transaction_id = ta.transaction_id
where a.file_changes -> 'private_id' is not null
and "type" = 'U'
union
select a.row_file ->'file_id' as id,
ta.transaction_id,
concat('device_type',':',' ',a.file_changes ->'device_type') as Updated_Value,
to_char(ta."timestamp", 'mm/dd/yyyy') as date
from logs.log_files a
join files.transactions ta on a.transaction_id = ta.transaction_id
where a.file_changes -> 'device_type' is not null
and "type" = 'U'
union
select a.row_file ->'file_id' as id,
ta.transaction_id,
concat('first_login',':',' ', changed_fields -> 'first_login') as Updated_Value,
to_char(ta."timestamp", 'mm/dd/yyyy') as date
from logs.log_files a
join files.transactions ta on a.transaction_id = ta.transaction_id
where a.file_changes -> 'first_login' is not null
and a."type" = 'U'
union
select a.row_file ->'file_id' as id,
ta.transaction_id,
concat('unread_items',':',' ',a.file_changes -> 'unread_items') as Updated_Value,
to_char(ta."timestamp", 'mm/dd/yyyy') as date
from logs.log_files a
join files.transactions ta on a.transaction_id = ta.transaction_id
where a.file_changes -> 'unread_items' is not null
and a."type" = 'U'
union
select a.row_file ->'file_id' as id,
ta.transaction_id,
concat('date_of_birth',':',' ',a.file_changes -> 'date_of_birth') as Updated_Value,
to_char(ta."timestamp", 'mm/dd/yyyy') as date
from logs.log_files a
join files.transactions ta on a.transaction_id = ta.transaction_id
where a.file_changes -> 'date_of_birth' is not null
and a."type" = 'U'
union
select a.row_file ->'file_id' as id,
ta.transaction_id,
concat('instrument_history',':',' ',a.file_changes -> 'instrument_history') as Updated_Value,
to_char(ta."timestamp", 'mm/dd/yyyy') as date
from logs.log_files a
join files.transactions ta on a.transaction_id = ta.transaction_id
where a.file_changes -> 'instrument_history' is not null
and "type" = 'U'
), cte2 as (
---******INSERTS*******
---participant_id
select a.row_file ->'file_id' as id,
ta.transaction_id,
concat('participant_id',':',a.row_data -> 'participant_id') as original_value,
to_char(ta."timestamp", 'mm/dd/yyyy') as date
from logs.log_files a
join files.transactions ta on a.transaction_id = ta.transaction_id
where a.row_data -> 'participant_id' is not null
and a."type" = 'I'
Union
--email
select a.row_file ->'file_id' as id,
ta.transaction_id,
concat('email',':',a.row_data -> 'email') as original_value,
to_char(ta."timestamp", 'mm/dd/yyyy') as date
from logs.log_files a
join files.transactions ta on a.transaction_id = ta.transaction_id
where a.row_data -> 'email' is not null
and a."type" = 'I'
union
----fname
select a.row_file ->'file_id' as id,
ta.transaction_id,
concat('name',':',a.row_data -> 'name') as original_value,
to_char(ta."timestamp", 'mm/dd/yyyy') as date
from logs.log_files a
join files.transactions ta on a.transaction_id = ta.transaction_id
where a.row_data -> 'name' is not null
and a."type" = 'I'
union
-----fname
select a.row_file ->'file_id' as id,
ta.transaction_id,
concat('last_name',':',a.row_data -> 'last_name') as original_value,
to_char(ta."timestamp", 'mm/dd/yyyy') as date
from logs.log_files a
join files.transactions ta on a.transaction_id = ta.transaction_id
where a.row_data -> 'last_name' is not null
and a."type" = 'I'
union
--phone
select a.row_file ->'file_id' as id,
ta.transaction_id,
concat('phone',':',a.row_data -> 'phone') as original_value,
to_char(ta."timestamp", 'mm/dd/yyyy') as date
from logs.log_files a
join files.transactions ta on a.transaction_id = ta.transaction_id
where a.row_data -> 'phone' is not null
and a."type" = 'I'
union
--gender
select a.row_file ->'file_id' as id,
ta.transaction_id,
concat('gender',':',a.row_data -> 'gender') as original_value,
to_char(ta."timestamp", 'mm/dd/yyyy') as date
from logs.log_files a
join files.transactions ta on a.transaction_id = ta.transaction_id
where a.row_data -> 'gender' is not null
and a."type" = 'I'
union
--opt_out
select a.row_file ->'file_id' as id,
ta.transaction_id,
concat('opt_out',':',a.row_data -> 'opt_out') as original_value,
to_char(ta."timestamp", 'mm/dd/yyyy') as date
from logs.log_files a
join files.transactions ta on a.transaction_id = ta.transaction_id
where a.row_data -> 'opt_out' is not null
and a."type" = 'I'
union
--study_id
select a.row_file ->'file_id' as id,
ta.transaction_id,
concat('study_id',':',a.row_data -> 'study_id') as original_value,
to_char(ta."timestamp", 'mm/dd/yyyy') as date
from logs.log_files a
join files.transactions ta on a.transaction_id = ta.transaction_id
where a.row_data -> 'study_id' is not null
and a."type" = 'I'
union
--'createdAt'
select a.row_file ->'file_id' as id,
ta.transaction_id,
concat('createdAt',':',a.row_data -> 'createdAt') as original_value,
to_char(ta."timestamp", 'mm/dd/yyyy') as date
from logs.log_files a
join files.transactions ta on a.transaction_id = ta.transaction_id
where a.row_data -> 'createdAt' is not null
and a."type" = 'I'
union
--'family_id'
select a.row_file ->'file_id' as id,
ta.transaction_id,
concat('family_id',':',a.row_data -> 'family_id') as original_value,
to_char(ta."timestamp", 'mm/dd/yyyy') as date
from logs.log_files a
join files.transactions ta on a.transaction_id = ta.transaction_id
where a.row_data -> 'family_id' is not null
and a."type" = 'I'
union
--'is_tester'
select a.row_file ->'file_id' as id,
ta.transaction_id,
concat('is_tester',':',a.row_data -> 'is_tester') as original_value,
to_char(ta."timestamp", 'mm/dd/yyyy') as date
from logs.log_files a
join files.transactions ta on a.transaction_id = ta.transaction_id
where a.row_data -> 'is_tester' is not null
and a."type" = 'I'
union
--'updatedAt'
select a.row_file ->'file_id' as id,
ta.transaction_id,
concat('updatedAt',':',a.row_data -> 'updatedAt') as original_value,
to_char(ta."timestamp", 'mm/dd/yyyy') as date
from logs.log_files a
join files.transactions ta on a.transaction_id = ta.transaction_id
where a.row_data -> 'updatedAt' is not null
and a."type" = 'I'
union
--'utc_offset'
select a.row_file ->'file_id' as id,
ta.transaction_id,
concat('utc_offset',':',a.row_data -> 'utc_offset') as original_value,
to_char(ta."timestamp", 'mm/dd/yyyy') as date
from logs.log_files a
join files.transactions ta on a.transaction_id = ta.transaction_id
where a.row_data -> 'utc_offset' is not null
and a."type" = 'I'
union
--'private_id'
select a.row_file ->'file_id' as id,
ta.transaction_id,
concat('private_id',':',a.row_data -> 'private_id') as original_value,
to_char(ta."timestamp", 'mm/dd/yyyy') as date
from logs.log_files a
join files.transactions ta on a.transaction_id = ta.transaction_id
where a.row_data -> 'private_id' is not null
and a."type" = 'I'
union
--'device_type'
select a.row_file ->'file_id' as id,
ta.transaction_id,
concat('device_type',':',a.row_data -> 'device_type') as original_value,
to_char(ta."timestamp", 'mm/dd/yyyy') as date
from logs.log_files a
join files.transactions ta on a.transaction_id = ta.transaction_id
where a.row_data -> 'device_type' is not null
and a."type" = 'I'
union
--'first_login'
select a.row_file ->'file_id' as id,
ta.transaction_id,
concat('first_login',':',a.row_data -> 'first_login') as original_value,
to_char(ta."timestamp", 'mm/dd/yyyy') as date
from logs.log_files a
join files.transactions ta on a.transaction_id = ta.transaction_id
where a.row_data -> 'first_login' is not null
and a."type" = 'I'
union
--'unread_items'
select a.row_file ->'file_id' as id,
ta.transaction_id,
concat('unread_items',':',a.row_data -> 'unread_items') as original_value,
to_char(ta."timestamp", 'mm/dd/yyyy') as date
from logs.log_files a
join files.transactions ta on a.transaction_id = ta.transaction_id
where a.row_data -> 'unread_items' is not null
and a."type" = 'I'
union
--'date_of_birth'
select a.row_file ->'file_id' as id,
ta.transaction_id,
concat('date_of_birth',':',a.row_data -> 'date_of_birth') as original_value,
to_char(ta."timestamp", 'mm/dd/yyyy') as date
from logs.log_files a
join files.transactions ta on a.transaction_id = ta.transaction_id
where a.row_data -> 'date_of_birth' is not null
and a."type" = 'I'
union
--'instrument_history'
select a.row_file ->'file_id' as id,
ta.transaction_id,
concat('instrument_history',':',a.row_data -> 'instrument_history') as original_value,
to_char(ta."timestamp", 'mm/dd/yyyy') as date
from logs.log_files a
join files.transactions ta on a.transaction_id = ta.transaction_id
where a.row_data -> 'instrument_history' is not null
and a."type" = 'I'
), cte3 as (
--*******DELETES******---
select a.row_file ->'file_id' as id,
ta.transaction_id,
concat('participant_id',':',a.row_data -> 'participant_id') as deleted_value,
to_char(ta."timestamp", 'mm/dd/yyyy') as date
from logs.log_files a
join files.transactions ta on a.transaction_id = ta.transaction_id
where a.row_data -> 'participant_id' is not null
and a."type" = 'D'
Union
--email
select a.row_file ->'file_id' as id,
ta.transaction_id,
concat('email',':',a.row_data -> 'email') as deleted_value,
to_char(ta."timestamp", 'mm/dd/yyyy') as date
from logs.log_files a
join files.transactions ta on a.transaction_id = ta.transaction_id
where a.row_data -> 'email' is not null
and a."type" = 'D'
union
----fname
select a.row_file ->'file_id' as id,
ta.transaction_id,
concat('name',':',a.row_data -> 'name') as deleted_value,
to_char(ta."timestamp", 'mm/dd/yyyy') as date
from logs.log_files a
join files.transactions ta on a.transaction_id = ta.transaction_id
where a.row_data -> 'name' is not null
and a."type" = 'D'
union
select a.row_file ->'file_id' as id,
ta.transaction_id,
concat('last_name',':',a.row_data -> 'last_name') as deleted_value,
to_char(ta."timestamp", 'mm/dd/yyyy') as date
from logs.log_files a
join files.transactions ta on a.transaction_id = ta.transaction_id
where a.row_data -> 'last_name' is not null
and a."type" = 'D'
union
select a.row_file ->'file_id' as id,
ta.transaction_id,
concat('phone',':',a.row_data -> 'phone') as deleted_value,
to_char(ta."timestamp", 'mm/dd/yyyy') as date
from logs.log_files a
join files.transactions ta on a.transaction_id = ta.transaction_id
where a.row_data -> 'phone' is not null
and a."type" = 'D'
union
select a.row_file ->'file_id' as id,
ta.transaction_id,
concat('gender',':',a.row_data -> 'gender') as deleted_value,
to_char(ta."timestamp", 'mm/dd/yyyy') as date
from logs.log_files a
join files.transactions ta on a.transaction_id = ta.transaction_id
where a.row_data -> 'gender' is not null
and a."type" = 'D'
union
select a.row_file ->'file_id' as id,
ta.transaction_id,
concat('opt_out',':',a.row_data -> 'opt_out') as deleted_value,
to_char(ta."timestamp", 'mm/dd/yyyy') as date
from logs.log_files a
join files.transactions ta on a.transaction_id = ta.transaction_id
where a.row_data -> 'opt_out' is not null
and a."type" = 'D'
union
select a.row_file ->'file_id' as id,
ta.transaction_id,
concat('study_id',':',a.row_data -> 'study_id') as deleted_value,
to_char(ta."timestamp", 'mm/dd/yyyy') as date
from logs.log_files a
join files.transactions ta on a.transaction_id = ta.transaction_id
where a.row_data -> 'study_id' is not null
and a."type" = 'D'
union
select a.row_file ->'file_id' as id,
ta.transaction_id,
concat('createdAt',':',a.row_data -> 'createdAt') as deleted_value,
to_char(ta."timestamp", 'mm/dd/yyyy') as date
from logs.log_files a
join files.transactions ta on a.transaction_id = ta.transaction_id
where a.row_data -> 'createdAt' is not null
and a."type" = 'D'
union
select a.row_file ->'file_id' as id,
ta.transaction_id,
concat('family_id',':',a.row_data -> 'family_id') as deleted_value,
to_char(ta."timestamp", 'mm/dd/yyyy') as date
from logs.log_files a
join files.transactions ta on a.transaction_id = ta.transaction_id
where a.row_data -> 'family_id' is not null
and a."type" = 'D'
union
select a.row_file ->'file_id' as id,
ta.transaction_id,
concat('is_tester',':',a.row_data -> 'is_tester') as deleted_value,
to_char(ta."timestamp", 'mm/dd/yyyy') as date
from logs.log_files a
join files.transactions ta on a.transaction_id = ta.transaction_id
where a.row_data -> 'is_tester' is not null
and a."type" = 'D'
union
select a.row_file ->'file_id' as id,
ta.transaction_id,
concat('updatedAt',':',a.row_data -> 'updatedAt') as deleted_value,
to_char(ta."timestamp", 'mm/dd/yyyy') as date
from logs.log_files a
join files.transactions ta on a.transaction_id = ta.transaction_id
where a.row_data -> 'updatedAt' is not null
and a."type" = 'D'
union
select a.row_file ->'file_id' as id,
ta.transaction_id,
concat('utc_offset',':',a.row_data -> 'utc_offset') as deleted_value,
to_char(ta."timestamp", 'mm/dd/yyyy') as date
from logs.log_files a
join files.transactions ta on a.transaction_id = ta.transaction_id
where a.row_data -> 'utc_offset' is not null
and a."type" = 'D'
union
select a.row_file ->'file_id' as id,
ta.transaction_id,
concat('private_id',':',a.row_data -> 'private_id') as deleted_value,
to_char(ta."timestamp", 'mm/dd/yyyy') as date
from logs.log_files a
join files.transactions ta on a.transaction_id = ta.transaction_id
where a.row_data -> 'private_id' is not null
and a."type" = 'D'
union
select a.row_file ->'file_id' as id,
ta.transaction_id,
concat('device_type',':',a.row_data -> 'device_type') as deleted_value,
to_char(ta."timestamp", 'mm/dd/yyyy') as date
from logs.log_files a
join files.transactions ta on a.transaction_id = ta.transaction_id
where a.row_data -> 'device_type' is not null
and a."type" = 'D'
union
select a.row_file ->'file_id' as id,
ta.transaction_id,
concat('first_login',':',a.row_data -> 'first_login') as deleted_value,
to_char(ta."timestamp", 'mm/dd/yyyy') as date
from logs.log_files a
join files.transactions ta on a.transaction_id = ta.transaction_id
where a.row_data -> 'first_login' is not null
and a."type" = 'D'
union
select a.row_file ->'file_id' as id,
ta.transaction_id,
concat('unread_items',':',a.row_data -> 'unread_items') as deleted_value,
to_char(ta."timestamp", 'mm/dd/yyyy') as date
from logs.log_files a
join files.transactions ta on a.transaction_id = ta.transaction_id
where a.row_data -> 'unread_items' is not null
and a."type" = 'D'
union
select a.row_file ->'file_id' as id,
ta.transaction_id,
concat('date_of_birth',':',a.row_data -> 'date_of_birth') as deleted_value,
to_char(ta."timestamp", 'mm/dd/yyyy') as date
from logs.log_files a
join files.transactions ta on a.transaction_id = ta.transaction_id
where a.row_data -> 'date_of_birth' is not null
and a."type" = 'D'
union
select a.row_file ->'file_id' as id,
ta.transaction_id,
concat('instrument_history',':',a.row_data -> 'instrument_history') as deleted_value,
to_char(ta."timestamp", 'mm/dd/yyyy') as date
from logs.log_files a
join files.transactions ta on a.transaction_id = ta.transaction_id
where a.row_data -> 'instrument_history' is not null
and a."type" = 'D'
), cte4 as (
select distinct a.row_data -> 'study_id' as study_id,
a.row_file ->'file_id' as id,
t.system_id as "User ID",
a.transaction_id,
case
when "User Type" = '1' then 'User1'
when "User Type" = '2' then 'User2'
when "User Type" = '3' then 'All Users'
end as "User Type",
case
when "type" = 'I' then 'Create'
when "type" = 'U' then 'Update'
when "type" = 'D' then 'Deleted'end as "type"
from logs.log_files a
join files.transactions ta on a.transaction_id = ta.transaction_id
where a."type" in ('U','D','I')
)
select c.id,
c4.study_id,
c4."User ID",
c4."Author Type",
c4."type",
string_agg(c.updated_value, ' ') as updated_value,
string_agg(c2.original_value, ' ') as original_value,
string_agg(c3.deleted_value, ' ') as deleted_value,
c.date
from cte c
join cte2 c2 on c.id = c2.id
join cte3 c3 on c.id = c3.id
join cte4 c4 on c.id = c4.id and c.transaction_id = c4.transaction_id
group by c.id, c4.study_id, c4."User ID", c4."Author Type", c4."type", c.date
Related
[PostgreSQL]: Subquery has too many columns
I am trying to run a query, but the result is showing the error message "Subquery has too many columns". I have to change where the clause with array to string but the error is the same. could you help me, if you have any suggestions or different queries as modify to solve this? the query is: create table my_schema.table_1 as select a.* , O.ID1 , J.ID2 , K.ID3 , L.ID4 , M.ID5 , S.ID6 , O.plan_name plan_name1 , J.plan_name plan_name2 , K.plan_name plan_name3 , L.plan_name plan_name4 , M.plan_name plan_name5 , S.plan_name plan_name6 from ( select PRD_ID , NBR , SI , START_TIME , ATTR4 , G_ID , BYTE_UP , BYTE_DOWN , LIST , TYPE_ID1 , TYPE_ID2 , TYPE_ID3 , TYPE_ID4 , TYPE_ID5 , TYPE_ID6 , CHARGE1, CHARGE2, CHARGE3, CHARGE4, CHARGE5, CHARGE6 from my_schema.source where prd_id = '20200101' and TYPE IN (3) ) a left outer join (select distinct id, price_plan_name from my_schema.id_ref_v9) O on split_part(split_part(list,';',1),',',1) = O.id left outer join (select distinct id, price_plan_name from my_schema.id_ref_v9) J on split_part(split_part(list,';',2),',',1) = J.id left outer join (select distinct id, price_plan_name from my_schema.id_ref_v9) K on split_part(split_part(list,';',3),',',1) = K.id left outer join (select distinct id, price_plan_name from my_schema.id_ref_v9) L on split_part(split_part(list,';',4),',',1) = L.id left outer join (select distinct id, price_plan_name from my_schema.id_ref_v9) M on split_part(split_part(list,';',5),',',1) = M.id left outer join (select distinct id, price_plan_name from my_schema.id_ref_v9) S on split_part(split_part(list,';',6),',',1) = S.id WHERE ( O.id in (select * from my_schema.LIST_PRICEID4)or J.id in (select * from my_schema.LIST_PRICEID4)or K.id in (select * from my_schema.LIST_PRICEID4)or L.id in (select * from my_schema.LIST_PRICEID4)or M.id in (select * from my_schema.LIST_PRICEID4)or S.id in (select * my_schema.LIST_PRICEID4))
SQLWorkbenchJ and Redshift execute this query forever
I am trying to create a temporary table (using a CTE) to contain a list of all possible locales. I am executing this query in SQLWorkbenchJ build 125 on MacOS JVM 1.8 This query doesn't query any table in the database. If you copy paste this query it will execute forever. it will never print any results. It has something to do with the size of the query. If I remove locales and only have upto the line 'en-GB' it works fine. but as you add more locales to the query, suddenly SQLWorkbenchJ will go blank and never return any results. with locales(locale) as ( select 'af-ZA' union select 'am-ET' union select 'ar-AE' union select 'ar-BH' union select 'ar-DZ' union select 'ar-EG' union select 'ar-IQ' union select 'ar-JO' union select 'ar-KW' union select 'ar-LB' union select 'ar-LY' union select 'ar-MA' union select 'arn-CL' union select 'ar-OM' union select 'ar-QA' union select 'ar-SA' union select 'ar-SY' union select 'ar-TN' union select 'ar-YE' union select 'as-IN' union select 'az-Cyrl-AZ' union select 'az-Latn-AZ' union select 'ba-RU' union select 'be-BY' union select 'bg-BG' union select 'bn-BD' union select 'bn-IN' union select 'bo-CN' union select 'br-FR' union select 'bs-Cyrl-BA' union select 'bs-Latn-BA' union select 'ca-ES' union select 'co-FR' union select 'cs-CZ' union select 'cy-GB' union select 'da-DK' union select 'de-AT' union select 'de-CH' union select 'de-DE' union select 'de-LI' union select 'de-LU' union select 'dsb-DE' union select 'dv-MV' union select 'el-GR' union select 'en-029' union select 'en-AU' union select 'en-BZ' union select 'en-CA' union select 'en-GB' union select 'en-IE' union select 'en-IN' union select 'en-JM' union select 'en-MY' union select 'en-NZ' union select 'en-PH' union select 'en-SG' union select 'en-TT' union select 'en-US' union select 'en-ZA' union select 'en-ZW' union select 'es-AR' union select 'es-BO' union select 'es-CL' union select 'es-CO' union select 'es-CR' union select 'es-DO' union select 'es-EC' union select 'es-ES' union select 'es-GT' union select 'es-HN' union select 'es-MX' union select 'es-NI' union select 'es-PA' union select 'es-PE' union select 'es-PR' union select 'es-PY' union select 'es-SV' union select 'es-US' union select 'es-UY' union select 'es-VE' union select 'et-EE' union select 'eu-ES' union select 'fa-IR' union select 'fi-FI' union select 'fil-PH' union select 'fo-FO' union select 'fr-BE' union select 'fr-CA' union select 'fr-CH' union select 'fr-FR' union select 'fr-LU' union select 'fr-MC' union select 'fy-NL' union select 'ga-IE' union select 'gd-GB' union select 'gl-ES' union select 'gsw-FR' union select 'gu-IN' union select 'ha-Latn-NG' union select 'he-IL' union select 'hi-IN' union select 'hr-BA' union select 'hr-HR' union select 'hsb-DE' union select 'hu-HU' union select 'hy-AM' union select 'id-ID' union select 'ig-NG' union select 'ii-CN' union select 'is-IS' union select 'it-CH' union select 'it-IT' union select 'iu-Cans-CA' union select 'iu-Latn-CA' union select 'ja-JP' union select 'ka-GE' union select 'kk-KZ' union select 'kl-GL' union select 'km-KH' union select 'kn-IN' ) select locale from locales limit 100;
Weird behaviors of T-SQL in SQL Server
For below data set I am trying to get the results where total of Emp_AVG <> 100. Some how I am getting weird results in SQL Server based on the order of the Data in Emp_AVG column. See below example: drop table #temp1 select 'F_TEST1' as First_Name, 'L_TEST1' as Last_Name, 'P' as Emp_Catagory, '99.99' as Emp_AVG, 'JAN' as Emp_Month into #temp1 union all select 'F_TEST1' as First_Name, 'L_TEST1' as Last_Name, 'C' as Emp_Catagory, '33.3' as Emp_AVG, 'FEB' as Emp_Month union all select 'F_TEST1' as First_Name, 'L_TEST1' as Last_Name, 'C' as Emp_Catagory, '33.3' as Emp_AVG, 'MAR' as Emp_Month union all select 'F_TEST1' as First_Name, 'L_TEST1' as Last_Name, 'C' as Emp_Catagory, '33.4' as Emp_AVG, 'APR' as Emp_Month union all select 'F_TEST2' as First_Name, 'L_TEST2' as Last_Name, 'P' as Emp_Catagory, '99.98' as Emp_AVG, 'JAN' as Emp_Month union all select 'F_TEST2' as First_Name, 'L_TEST2' as Last_Name, 'C' as Emp_Catagory, '33.3' as Emp_AVG, 'FEB' as Emp_Month union all select 'F_TEST2' as First_Name, 'L_TEST2' as Last_Name, 'C' as Emp_Catagory, '33.4' as Emp_AVG, 'MAR' as Emp_Month union all select 'F_TEST2' as First_Name, 'L_TEST2' as Last_Name, 'C' as Emp_Catagory, '33.3' as Emp_AVG, 'APR' as Emp_Month union all select 'F_TEST3' as First_Name, 'L_TEST3' as Last_Name, 'P' as Emp_Catagory, '99.97' as Emp_AVG, 'JAN' as Emp_Month union all select 'F_TEST3' as First_Name, 'L_TEST3' as Last_Name, 'C' as Emp_Catagory, '33.4' as Emp_AVG, 'FEB' as Emp_Month union all select 'F_TEST3' as First_Name, 'L_TEST3' as Last_Name, 'C' as Emp_Catagory, '33.3' as Emp_AVG, 'MAR' as Emp_Month union all select 'F_TEST3' as First_Name, 'L_TEST3' as Last_Name, 'C' as Emp_Catagory, '33.3' as Emp_AVG, 'APR' as Emp_Month --select * from #temp1 select First_Name,Last_Name, Emp_Catagory, sum(cast(Emp_AVG as float)) as Total_AVG from #temp1 Group by First_Name,Last_Name, Emp_Catagory having Sum(cast(Emp_AVG as float)) <> 100 order by sum(cast(Emp_AVG as float)) desc /***************************************************************/ I will really appreciate if anyone can provide me the solution to this. Regards, Jigar B.
My guess is the "strange" result you're seeing is that there are results where Total_AVG seems to be equal to 100? Like this: /---------------------------------------------------\ | First_Name | Last_Name | Emp_Catagory | Total_AVG | |------------+-----------+--------------+-----------| | F_TEST2 | L_TEST2 | C | 100 | | F_TEST3 | L_TEST3 | C | 100 | | F_TEST1 | L_TEST1 | P | 99.99 | | F_TEST2 | L_TEST2 | P | 99.98 | | F_TEST3 | L_TEST3 | P | 99.97 | \---------------------------------------------------/ Try casting your values as decimal instead: select First_Name, Last_Name, Emp_Catagory, sum(cast(Emp_AVG as decimal(4,2))) as Total_AVG from #temp1 group by First_Name, Last_Name, Emp_Catagory having sum(cast(Emp_AVG as decimal(4,2))) <> 100 order by sum(cast(Emp_AVG as decimal(4,2))) desc You then only see the results you are expecting: /---------------------------------------------------\ | First_Name | Last_Name | Emp_Catagory | Total_AVG | |------------+-----------+--------------+-----------| | F_TEST1 | L_TEST1 | P | 99.99 | | F_TEST2 | L_TEST2 | P | 99.98 | | F_TEST3 | L_TEST3 | P | 99.97 | \---------------------------------------------------/
SQL exclude a where clause if the variable is null or empty
I have SQL (postgresql) expression like of these: select * from table1 t1 left join table2 t2 on t1.id = t2.id where t1.name = nameVariable and t2.field = fieldVariable; Would it be possible in case nameVariable is null or empty to not execute the "where t1.name = nameVariable " at all? EDIT Also I need to precise that I am using JPA and hibernate so the query looks like this: #Query(value = " select * from table1 t1 left join table2 t2 on t1.id = t2.id where t1.name = :name and t2.field = :fieldName;", nativeQuery=true) public List<Test> getTest(#Param("name") String name, #Param("fieldName") String fieldName);
SELECT * FROM table1 t1 LEFT JOIN table2 t2 ON t1.id = t2.id WHERE (t1.name = nameVariable OR nameVariable IS NULL) AND (t2.field = fieldVariable OR t2.field IS NULL OR fieldVariable IS NULL) ;
How to sort a Union All
I can't get this to sort. I want the entire result set ordered by DateId then productName and can't get it to work. SELECT d.DateId, s.product_name1 as productName FROM dbo.SaleDates d INNER JOIN dbo.Sale s ON s.saleId = d.saleId UNION ALL SELECT d.DateId, s.product_name2 as productName FROM dbo.SaleDates d INNER JOIN dbo.Sale s ON s.saleId = d.saleId UNION ALL SELECT d.DateId, s.product_name3 as productName FROM dbo.SaleDates d INNER JOIN dbo.Sale s ON s.saleId = d.saleId order by d.DateId, productName Not sure where and how to add this order by basically. I don't want to add an order by to each select because then I'd have subsets of orderings. I want to order the -entire- ending result set...
You could try this: SELECT * FROM ( SELECT d.DateId as DateId, s.product_name1 as productName FROM dbo.SaleDates d INNER JOIN dbo.Sale s ON s.saleId = d.saleId UNION ALL SELECT d.DateId as DateId, s.product_name2 as productName FROM dbo.SaleDates d INNER JOIN dbo.Sale s ON s.saleId = d.saleId UNION ALL SELECT d.DateId as DateId, s.product_name3 as productName FROM dbo.SaleDates d INNER JOIN dbo.Sale s ON s.saleId = d.saleId ) AS A order by DateId, ProductName
refer to the column alias (in your case DateId, Productname) select 1 a union all select 2 a order by a desc or refer to the column number select 1 a union all select 2 a order by 1
Select Col1, Col2 from tblA where <some condition> Union All Select Col1,Col2 from tblB where <some condition> Union All Select Col1,Col2 from tblC where <some condition> Order By 1,2 -- means by first column,second column -- or Order By Col1, Col2 But yours should work fine Order by DateId,productName Also instead of hitting the tables Sales and SalesDate 3 times, you can improve the performance of your query by catching the common result set in some temporary table and then performing the union on the desired columns e.g. SELECT d.DateId,s.product_name1,s.product_name2,s.product_name3 INTO #temp FROM dbo.SaleDates d INNER JOIN dbo.Sale s ON s.saleId = d.saleId Select t.DateId,t.product_name1 As productName From #temp t Union All Select t.DateId,t.product_name2 From #temp t Union All Select t.DateId,t.product_name3 From #temp t Order By DateId,productName Drop Table #temp Hope this helps
You could use a CTE for this. Or just do: Select res.DateID, res.productName from ( SELECT d.DateId, s.product_name1 as productName FROM dbo.SaleDates d INNER JOIN dbo.Sale s ON s.saleId = d.saleId UNION ALL SELECT d.DateId, s.product_name2 as productName FROM dbo.SaleDates d INNER JOIN dbo.Sale s ON s.saleId = d.saleId UNION ALL SELECT d.DateId, s.product_name3 as productName FROM dbo.SaleDates d INNER JOIN dbo.Sale s ON s.saleId = d.saleId ) order by res.DateId, res.productName