I have calendar table with columns(id,agent_id,date,location) and agent table with columns(id,name,phone_number,email).
I need to create a view of calendar table with additional column(agent_email),where agent_email is from agent table email column as same as agent_id in calendar table is id from agent table.
I have tried as in above screenshot,but instead of agent_email in
table column ,I have ?column?.
select calendar_events.*, agents.email
from calendar_events
left outer join agents on calendar_events.agent_id=agents.id;
This gives my expected answer ,
columns :
id | agent_id | date | location | agent_email
Related
My source table has query id and a sql query.
Using Talend I need to run this CUSTOM_SQL query against the database and load a target table with the counts.
source table:
QUERY_ID|CUSTOM_SQL |
--------+----------------------------------------------------------------+
1|select count(1) as ROW_COUNT from SYSTEM_PRIVILEGE_MAP |
2|select count(1) as ROW_COUNT from OGIS_SPATIAL_REFERENCE_SYSTEMS|
3|select count(1) as ROW_COUNT from SDO_COORD_SYS |
4|select count(1) as ROW_COUNT from SDO_COORD_REF_SYS |
5|select count(1) as ROW_COUNT from SDO_PREFERRED_OPS_SYSTEM |
6|select count(1) as ROW_COUNT from SDO_TIN_PC_SYSDATA_TABLE |
expected output in target table:
QUERY_ID|QUERY_RESULT |
--------+-------------+
1|290 |
2|322 |
3|784 |
4|8484 |
5|743 |
I created a job that looks as follows but it is not complete:
tdbInput -> tFlowIterate -> tDBInput -> tMap -> tDBOutput
With the above design I'm able to run the CUSTOM_SQL, capture the result from tDBInput, but unable capture and propagate the QUERY_ID.
How do I propagate both query_id and the query result in one row to the target table. What components should I use?
Please note that each CUSTOM_SQLs always return one row and one column. So this is a very specific usecase.
I simplified my scenario by using some dummy data.
I will appreciate any help on this.
Thank you!
With your first tDBInput component, make sure you extract both QUERY_ID and CUSTOM_SQL (select QUERY_iD,CUSTOM_SQL from source_table) : you should get in global variables of your tFlowToIterate two variables (something like ((String)globalMap.get("row1.QUERY_ID")) , with row1 being the name of the flow between tDBInput and tFlowToIterate). You can also check in "outline" view if those variables appear under tFlowToIterate_1 component.
Then in tMap, you can just access this query_id global variable (with the above syntax) and push it to your target table.
Essentially what i want to do is to get by id from "Tracks" but i also want to get the relations it has to other tracks (found in table "Remixes").
I can write a simple query that gets the track i want by id, ex.
SELECT * FROM "Tracks" WHERE id IN ('track-id1');
That gives me:
id | dateModified | channels | userId
-----------+---------------------+-----------------+--------
track-id1 | 2019-07-21 12:15:46 | {"some":"json"} | 1
But this is what i want to get:
id | dateModified | channels | userId | remixes
-----------+---------------------+-----------------+--------+---------
track-id1 | 2019-07-21 12:15:46 | {"some":"json"} | 1 | track-id2, track-id3
So i want to generate a column called "remixes" with ids in an array based on the data that is available in the "Remixes" table by a SELECT query.
Here is example data and database structure:
http://sqlfiddle.com/#!17/ec2e6/3
Don't hesitate to ask questions in case anything is unclear,
Thanks in advance
Left join the remixes and then GROUP BY the track ID and use array_agg() to get an array of the remix IDs.
SELECT t.*,
CASE
WHEN array_agg(r."remixTrackId") = '{NULL}'::varchar(255)[] THEN
'{}'::varchar(255)[]
ELSE
array_agg(r."remixTrackId")
END "remixes"
FROM "Tracks" t
LEFT JOIN "Remixes" r
ON r."originalTrackId" = t."id"
WHERE t."id" = 'track-id1'
GROUP BY t."id";
Note that, if there are no remixes array_agg() will return {NULL}. But I figured you rather want an empty array in such a case. That's what the CASE is for.
BTW, providing a fiddle is a nice move of yours! But please also include the code in the original question. The fiddle site might be down (even permanently) and that renders the question useless because of the missing information.
That's a simple outer join with a string aggregation to get the comma separated list:
SELECT t.*,
string_agg(r."remixTrackId", ', ') as remixes
FROM "Tracks" t
LEFT JOIN "Remixes" r ON r."originalTrackId" = t.id
WHERE t.id = 'track-id1'
GROUP BY t.id;
The above assumes that Tracks.id is the primary key of the Tracks table.
I have a table in a POstgreSQL database with multiple columns out of which only one will have a value entered.
SELECT "Garden_GUID", "Municipality_Amajuba", "Municipality_Ilembe", "Municipality_Sisonke" from forms_garden
WHERE "Garden_GUID" = 'testguid';
Garden_GUID | Municipality_Amajuba | Municipality_Ilembe | Municipality_Sisonke
-------------+----------------------+---------------------+----------------------
testguid | Dannhauser | |
(1 row)
I wish to create a view in which the entries from those columns are colated into a single column.
I have tried:
CREATE VIEW municipality (GUID,funder,municipality)
AS SELECT "Garden_GUID"GUID,"Funder"funder,"Municipality_Amajuba","Municipality_Ilembe","Municipality_Sisonke"municipality
FROM forms_garden;
but it returns an error:
ERROR: column "municipality" specified more than once
Is there any way to query the various municipality_* columns row by row and only return the first positive entry?
Many thanks in advance.
I think coalesce() is what you're looking for:
with forms_garden as (
select 'guid1' guid, 'Dannhauser' amajuba, null ilembe, null sisonke
union all select 'guid2', null, 'muni2', null
union all select 'guid3', null, null, 'muni3'
) select guid, coalesce(amajuba,ilembe,sisonke) municipality from forms_garden;
I want dynamically search condition.
This is my table(This is generated dynamically ) this is not a physical table.
id Tablename columnname Value |
1 Company Company_name Microsoft |
2 Address Pcity CA |
3 Phone Pnumber 100-4582 |
I want search the Value in the particular table , In this tables are already in the database(Company,Address,Phone). dynamically pass the tablename and columnname and search the Value.
Ex
Select c.Company_name from Company c join Address a on
a.companyid=c.companyid join phone p on p.companyid=c.companyid
where 1=1 and c.company_name like '%Microsoft%' and a.Pcity Like
'%CA%' and p.Pnumber like '%100-4582%'
I want dynamically buld the query and search the condition in the Value column.
How can I do this ..
Thanks.
what i understood is basically you want to make dynamic query, so i am providing a sample for you
create proc dummy_proc
#arg1 varchar(50),
#arg2 int
as
Declare #S varchar(max)
Set #S='select * from tablename where 1=1'
if #arg1<>'' then
set #s=#s+ 'and column1 like''%'+#arg1+%'''
Execute(#S)
hope it helps
Say I have a table like so
id | device | cmd | value |
------+----------------+-------+---------
id = unique row ID
device = device identifier (mac address)
cmd = some arbitrary command
value = value of corresponding command
I would like to somehow self join on this table to grab specific cmds and their corresponding values for a particular device.
I do not want just SELECT cmd,value FROM table WHERE device='00:11:22:33:44:55';
Say the values I want correspond to the getname and getlocation commands. I would like to have output something like
mac | name | location
--------------------+-----------+------------
00:11:22:33:44:55 | some name | somewhere
My sql fu is pretty pants. I've been trying different combinations like SELECT a.value,b.value FROM table AS a INNER JOIN table AS b ON a.device=b.device but I am getting nowhere.
Thanks for any help.
SELECT a.value AS thisval ,b.value AS thatval
FROM table AS a JOIN table AS b USING (device)
WHERE a.command='this' AND b.command='that';