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
Related
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
How can I merge 2 tables with the same column names into 1 table? Something like this:
The 2nd table should fill in the 1st table.
This is as close as I got
SELECT * FROM
Animals
LEFT JOIN Best
ON Animals.species=Best.species;
http://sqlfiddle.com/#!5/d0a98/3
But it seems to concatenate the 2nd table on there.
Is LEFT JOIN really the correct way to do this?
You should list the columns in the SELECT. Then you would readily see that all you need is COALESCE():
SELECT a.price, a.species, COALESCE(b.name, a.name) as name
FROM Animals a LEFT JOIN
Best b
ON a.species = b.species;
I have that sql:
SELECT DISTINCT
count(KTT)
FROM
TRA.EVENT;
it returns me a number of 1901335.
Now I want to expand the sql with a join like this:
SELECT DISTINCT
count(E.KTT)
FROM
TRA.EVENT E
LEFT JOIN TRA.TMP_BNAME TBN ON E.KTT = TBN.KTT_DEF;
But here I have a result of 1942376.
I dont understand why? I expect also a result of 1901335. I thought I easily join the values from TBN based on the entries of EVENT?
EDIT
SELECT DISTINCT
E.KTT,
TB.B_BEZEICHNER
FROM
TRA.EVENT E
LEFT JOIN TRA.TMP_BNAME TBN ON E.KTT = TBN.KTT_DEF
LEFT JOIN TRA.TMP_B TB ON TBN.B_ID = TB.B_ID;
What I am doing wrong?
Thx for your help.
Stefan
You have not provided full details so treat those comments as general ones.
When you join 2 tables, it may happen that it can create "duplicate" rows from one table. In your instance, there may be more than 1 record with the same KTT_DEF in TRA.TMP_BNAME table. When you join that to TRA.EVENT table, it create more than one record for each original record in TRA.EVENT table.
You may choose to count the distinct values of KTT from TRA.EVENT and use DISTINCT keyword but you need to put it into the COUNT: SELECT COUNT(DISTINCT E.KTT). This will work provided that your values are actually unique. If they are not, the count will be different from the first query.
You want to count the distinct KTT?
Then your code is wrong. You have to use:
SELECT count(DISTINCT KTT)
FROM TRA.EVENT;
You get different count because you count every row. Not the distinct ones. And because the join add more rows to the query thats why you get a bigger number.
Try this:
SELECT COUNT(DISTINCT E.KTT)
FROM TRA.EVENT E
LEFT JOIN TRA.TMP_BNAME TBN ON E.KTT = TBN.KTT_DEF;
I am trying to join two oracle database tables where the columns to join on contain slightly different data.
For example Table A has a column 'ref' and Table B has a column 'id'.
A.ref contains data like A1234567890B and B.id contains data of the form 1234567890
I have tried joining the two based on the following query;
SELECT * FROM A INNER JOIN B
ON SUBSTR(A.ref, 2,10) = B.id;
But this has returned no results when I know that there is matching data from this substring.
Any ideas?
you could try something like this:
SELECT * FROM A INNER JOIN B
ON regexp_substr(A.ref, '^[[:alpha:]]+([[:digit:]]+)[[:alpha:]]+$',1,1,'c',1) = B.id
I was able to solve this in the end by padding out SUBSTR(A.ref, 2,10) to 12 characters.
Let's say I have the following SQL query:
SELECT *
FROM employee
INNER JOIN department ON employee.EmpID = department.EmpID
I wanted to ask, why I am getting two EmpID columns, and how can I get only one of those, preferably the first.
I'm using SQL server
SELECT employee.EmpID, employee.name, ...
FROM employee
INNER JOIN department ON employee.EmpID=department.EmpID
Be precise and specify which columns you need instead of using the astrisk to select all columns.
You get all columns from these two tables, that's why you have two EmpID columns. The only JOIN type that removes common column is NATURAL JOIN, which is not implemented by SQL Server. Your query would look then like this:
SELECT *
FROM employee
NATURAL JOIN department
This generates join predicates by comparing all columns with the same name in both tables. The resulting table contains only one column for each pair of equally named columns.
You're getting all columns from all tables involved in your query since you're asking for it: SELECT *
If you want only specific column - you need to specify which ones you want:
SELECT e.EmpID, e.Name as 'Employee Name', d.Name AS 'Department Name'
FROM employee e
INNER JOIN department d ON e.EmpID = d.EmpID
Don't use *. Specify the columns you want in the field list.
SELECT E.EmpID, E.EmpName -- etc
FROM employee as E
INNER JOIN department as D
ON E.EmpID=D.EmpID
As stated by others, don't use *
See this SO question for reasons why:
Which is faster/best? SELECT * or SELECT column1, colum2, column3, etc
Essentially, the answer to your question is that the output from a SQL SELECT query is not a relation, and therefore if you do not take care you may end up with duplicate attribute names (columns) and rows.
Standard SQL has some constructs to mitigate SQL's non-relational problems e.g. NATURAL JOIN would ensure the result has only one EmpID attribute. Sadly, SQL Server does not support this syntax but you can vote for it here.
Therefore, you are forced to write out in long-hand the columns you want, using the table name to qualify which attribute you prefer e.g. employee.EmpID.