SQL query : name columns by "columnname.field" - sql

Hello I've written the following query :
SELECT *
FROM [woJob]
LEFT JOIN [woJobTask]
ON [woJob].jobID=[woJobTask].jobID
The query it returns has duplication columns but they are named the same. Is it possible to name column by table.Field. For example, name woJob.jobID and woJobTask.jobID?
My work flow is to use SQL to get the data out of the database and then im using pandas (a python library) to explore the data. Having duplicate column names makes things a little more complicated analyzing the data in python. I want to get all the data out labeled up with column names so I know each column belongs to which table and then analyze the data in Pandas, I can drop any columns in pandas I don't want.

You need to enumerate the columns, and assign alias as needed.
You did not tell what the columns of the tables are, so here is a contrived example, assuming colums jobid, name and value in both tables:
SELECT j.jobid, j.name, j.value, jt.name as jt_name, jt.value as jt_value
FROM [woJob] j
LEFT JOIN [woJobTask] jt ON j.jobid = jt.jobid
Or more simply:
SELECT j.*, jt.name as jt_name, jt.value as jt_value
FROM [woJob] j
LEFT JOIN [woJobTask] jt ON j.jobid = jt.jobid

Related

Concatenate ALL values from 2 tables using SQL?

I am trying to use SQL to create a table that concatenates all dates from a specific range to all items in another table. See image for an example.
I have a solution where I can create a column of "null" values in both tables and join on that column but wondering if there is a more sophisticated approach to doing this.
Example image
I've tried the following:
Added a constant value to each table
Then I joined the 2 tables on that constant value so that each row matched each row of both tables.
This got the intended result but I'm wondering if there's a better way to do this where I don't have to add the constant values:
SELECT c.Date_,k.user_email
FROM `operations-div-qa.7_dbtCloud.calendar_table_hours_st` c
JOIN `operations-div-qa.7_dbtCloud.table_key` k
ON c.match = k.match
ORDER BY Date_,user_email asc
It's not a concatenation in the image given, Its a join
select t1.dates Date ,t2.name Person
from table t1,table t2;
Cross join should work for you:
It joins every row from both tables with each other. Use this when there is no relationship between the tables.
Did not test so syntax may be slightly off.
SELECT c.Date_,k.user_email
FROM `operations-div-qa.7_dbtCloud.calendar_table_hours_st` c
CROSS JOIN `operations-div-qa.7_dbtCloud.table_key` k
ORDER BY Date_,user_email asc

Oracle SQL String Contains a value from another Table

I am looking to find a way to add a where clause where a string in one table contains data from a column in another table.
Select
ID,
Name,
Group,
List
From EDG
Where
List Like '% (Select(Column X) FRom Diag)%'
I'm looking for something that would work like you see above. There are no columns to join on for the tables, it is just List in EDG can contain values from column X in Diag.
Any help would be appreciated.
Thanks,
Then you can join, because any statement that results in a truth can be used for join:
Select
E.ID,
E.Name,
E.LGroup,
E.List
From
EDG E
INNER JOIN Diag D
ON
E.List Like '%'||D.X||'%'
Inner join will result in only rows where List string contains X

Column in field list is ambiguous error

i've been recently working in mysql and in one of the requests i wrote :
SELECT SIGLE_EEP, ID_SOUS_MODULE, LIBELLE
FROM mef_edi.eep a, mef_edi.envoi e, mef_edi.sous_module s
WHERE a.ID_EEP = e.ID_EEP
AND a.ID_SOUS_MODULE = s.ID_SOUS_MODULE;
and they told me :
Column ID_SOUS_MODULE in field list is ambiguous
What should i do ?
More than one table has a column named ID_SOUS_MODULE.
So you need to name the table every time you mention the column to specify which table you mean.
Change
SELECT ID_SOUS_MODULE
for instance to
SELECT a.ID_SOUS_MODULE
I agree with the answer above, you may have duplicate column names across your 3 tables, assigning the table id (a, e, s) as noted above will avoid that issue in the select. In addition to what #juergen said you may want to get rid of that cartesian join by using an inner or left join (inner seems to be what your going for). The way you are joining your table you are joining every possible combination of rows together than filtering. using a proper join will get you better performance in the long run as your table line counts grow. Here is an example of a non cartesian join:
SELECT SIGLE_EEP, ID_SOUS_MODULE, LIBELLE
FROM mef_edi.eep a
INNER JOIN mef_edi.envoi e ON (a.ID_EEP = e.ID_EEP)
INNER JOIN mef_edi.sous_module s ON (a.ID_SOUS_MODULE = s.ID_SOUS_MODULE)

Sql querying a new column as array from a set of results from another table

i'm trying to make a join query of two tables and include a new column that is actually a result from a query from another table, but the new column is an array, can be a json perhaps. To demonstrate I have this table :
and each entry can have several plots :
I know I can select all plots using the entry id, but that's not what I want. I want to select all entries, and have all its plots in array form, so that I can have them in a nice single table :
I don't know really where to look. So how do I return an array as a column of a join query. Thanks! I'm using PostgreSQL if that makes any difference.
Use array_agg():
select e.id, e.entry_name, array_agg(plot_name)
from entry e join
plot p
on e.id = p.entry_id
group by e.id, e.entry_name;

SQL Inner Join to compare the result

I am new to SQL and want to compare two tables using inner join and then use the result to compare it with the third table and so on. Also the third table does not share the same column name as compared in the first two.
Below is the SQL statement to compare two tables.
SELECT WORKLOAD_ITEM_ID wid
FROM OUTPUT o
INNER JOIN workload_item wi ON O.WORKLOAD_ITEM_ID = wi.WORKLOAD_ITEM_ID
Now whatever is returned I want to use that and do an inner join with a third table which does not have a column WORKLOAD_ITEM_ID. So the question is how to save and compare the result?
Please Help!
You can follow this to hold records in temp table
INSERT INTO #WORKLOAD_ITEM_TEMP
SELECT WORKLOAD_ITEM_ID wid
FROM OUTPUT o
INNER JOIN workload_item wi ON O.WORKLOAD_ITEM_ID = wi.WORKLOAD_ITEM_ID
Then with your third table made a join with this #WORKLOAD_ITEM_TEMP table.
Now you have to decide on which column need to put inner join. Either add a common column between output result set and third table or use just cross join.
I think you need to reevaluate your Database structure and make efficient tables relationship.