I have the following two tables with data like so:
Table Values
var_label
1
2
2
1
3
Table Codes
var_code
1
2
4
2
I need to join these tables and get the distinct result. The var_label and var_code are equal pieces of data. I want to have the joined output like so:
MyColumn
1
2
3
4
Wht's the best way to do this?
Use UNION without ALL(implicit distinct) like so:
SELECT var_label AS MyColumn
FROM Values
UNION
SELECT var_code
FROM Codes
Live Demo
SELECT var_label
FROM Table1 as MyColumn
UNION
SELECT var_data as MyColumn
FROM Table2
you can give aliases for getting only one colum name.
SQLFiddle DEMO
SELECT distinct(var_label)
FROM Table1
UNION
SELECT distinct(var_data)
FROM Table2
Related
I am new to sql and are trying to combine a column value from three different tables and combine to one row in DB2 Warehouse on Cloud. Each table consists of only one row and unique column name. So what I want to is just join these three to one row their original column names.
Each table is built from a statement that looks like this:
SELECT SUM(FUEL_TEMP.FUEL_MLAD_VALUE) AS FUEL
FROM
(SELECT ML_ANOMALY_DETECTION.MLAD_METRIC AS MLAD_METRIC, ML_ANOMALY_DETECTION.MLAD_VALUE AS FUEL_MLAD_VALUE, ML_ANOMALY_DETECTION.TAG_NAME AS TAG_NAME, ML_ANOMALY_DETECTION.DATETIME AS DATETIME, DATA_CONFIG.SYSTEM_NAME AS SYSTEM_NAME
FROM ML_ANOMALY_DETECTION
INNER JOIN DATA_CONFIG ON
(ML_ANOMALY_DETECTION.TAG_NAME =DATA_CONFIG.TAG_NAME AND
DATA_CONFIG.SYSTEM_NAME = 'FUEL')
WHERE ML_ANOMALY_DETECTION.MLAD_METRIC = 'IFOREST_SCORE'
AND ML_ANOMALY_DETECTION.DATETIME >= (CURRENT DATE - 9 DAYS)
ORDER BY DATETIME DESC)
AS FUEL_TEMP
I have tried JOIN, INNER JOIN, UNION/UNION ALL, but can't get it to work as it should. How can I do this?
Use a cross-join like this:
create table table1 (field1 char(10));
create table table2 (field2 char(10));
create table table3 (field3 char(10));
insert into table1 values('value1');
insert into table2 values('value2');
insert into table3 values('value3');
select *
from table1
cross join table2
cross join table3;
Result:
field1 field2 field3
---------- ---------- ----------
value1 value2 value3
A cross join joins all the rows on the left with all the rows on the right. You will end up with a product of rows (table1 rows x table2 rows x table3 rows). Since each table only has one row, you will get (1 x 1 x 1) = 1 row.
Using UNION should solve your problem. Something like this:
SELECT
WarehouseDB1.WarehouseID AS TheID,
'A' AS TheSystem,
WarehouseDB1.TheValue AS TheValue
FROM WarehouseDB1
UNION
SELECT
WarehouseDB2.WarehouseID AS TheID,
'B' AS TheSystem,
WarehouseDB2.TheValue AS TheValue
FROM WarehouseDB2
UNION
WarehouseDB3.WarehouseID AS TheID,
'C' AS TheSystem,
WarehouseDB3.TheValue AS TheValue
FROM WarehouseDB3
Ill adapt the code with your table names and rows if you tell me what they are. This kind of query would return something like the following:
TheID TheSystem TheValue
1 A 10
2 A 20
3 B 30
4 C 40
5 C 50
As long as your column names match in each query, you should get the desired results.
How can I get records from my table where any of a list of integers is in the range defined by columnA and columnB integer values?
I know about the IN operator when comparing against a column value instead of a range defined by a pair of columns.
For example: select * from mytable where mytable.colA in (1,3,5,6); would get all records where colA is either 1,3,5 or 6.
Is there anything like that for ranges? Or should I do like:
select * from mytable where 1 between mytable.colA and mytable.colb
OR
3 between mytable.colA and mytable.colb
OR
5 between mytable.colA and mytable.colb
OR
6 between mytable.colA and mytable.colb;
Maybe this way:
select distinct mytable.*
from mytable
join (select 1 nr union all select 3 union all select 5 union all select 6) n
on n.nr between mytable.colA and mytable.colb
Update:
Just tested on MariaDB (10.0.19) and a 1M-row indexed table.. Your original query is ways faster.
A common tactic is to set up a temporary table, and use that to join on your main table.
A simple way to set one up is like so:
DECLARE #TempList table (LookFor int not null)
INSERT #TempList (LookFor) values
(1)
,(3)
,(5)
,(6)
As this is a table, you can use querying logic to populate it.
Next up, join this into your target table. For your example above:
SELECT mt.*
from myTable mt
inner join #TempList tl
on tl.LookFor = mt.ColA
And, if I'm interpreting correctly, this might be what you're really looking for:
SELECT mt.*
from myTable mt
inner join #TempList tl
on tl.LookFor between mt.ColA and mt.ColB
Please advise me on the following question:
I have two tables in an Oracle db, one that contains full numbers and the other that contains parts of them.
Table 1:
12323543451123
66542123345345
16654232423423
12534456353451
64565463345231
34534512312312
43534534534533
Table 2:
1232
6654212
166
1253445635
6456546
34534
435345
Could you please suggest a query that joins these two tables and shows the relation between 6456546 and 64565463345231, for example. The main thing is that Table 2 contains a lot more data than Table 1, and i need to find all the substrings from Table 2 that are not present in Table 1.
Thanks in advance!
Try this:
with t as (
select 123 id from dual union all
select 567 id from dual union all
select 891 id from dual
), t2 as (
select 1112323 id from dual union all
select 32567321 id from dual union all
select 44891555 id from dual
)
select t.id, t2.id
from t, t2
where t2.id||'' like '%'||t.id||'%'
You could try using the CONTAINS operator like this :
SELECT * FROM Table2 JOIN Table1 ON Table1.id=Table2.id
WHERE NOT CONTAINS (Table2.data, Table1.data)
Are numbers from table two in a set place in table 1? For example is the 1232 in the same place each time or do you have to search a sting for the numbers. If it's set you could use an inline select or a temp table and create a substring of the string your searching and then join the table or temp table on that field.
you first need to say if the number in Table 1 and 2 are repeated, if is not then I think this query would help you:
SELECT *
FROM Table_1
JOIN Table_2 ON Table_1.ID = Table_2.ID
WHERE Table_2.DATA LIKE Table_1.DATA
I have two tables:
FirstField | SecondField | ThirdField
FirstValue SecondValue ThirdValues
----------------------------------------------
FirstField | SecondField | ThirdField
OtherValue1 OtherValue2 OtherValue3
What I need it to add those two tables together into one SQL query. They can not be joined as I don't have anything to join them on and that's not what I want. I want my new table to look like:
FirstField | SecondField | ThirdField
FirstValue SecondValue ThirdValues
OtherValue1 OtherValue2 OtherValue3
This may be very simple but I am new to SQL and have been unable to find any help elsewhere.
Try UNION ALL:
SELECT FirstField ,SecondField ,ThirdField
FROM Table1
UNION ALL
SELECT FirstField ,SecondField ,ThirdField
FROM Table2
If you want to remove duplicate rows use UNION instead.
SELECT FirstField ,SecondField ,ThirdField
FROM Table1
UNION
SELECT FirstField ,SecondField ,ThirdField
FROM Table2
Have a lok at using a UNION/UNION ALL
Combines the results of two or more queries into a single result set
that includes all the rows that belong to all queries in the union.
The UNION operation is different from using joins that combine columns
from two tables.
So something like
SELECT Field1,
Field2,
...
Fieldn
FROM Table1
UNION ALL
SELECT Field1,
Field2,
...
Fieldn
FROM Table2
Provided that the column types and count match, use UNION ALL:
SELECT * FROM T1
UNION ALL
SELECT * FROM T2
I´d like to add several rows stemming from another SQL query (different table) to a query result. e.g.:
SELECT mycol from mytable
# returns
mycol
1
4
6
SELECT anothercol from anothertable
#returns
anothercol
3
8
9
What I would like to obtain is:
myresult
1
4
6
3
8
9
Currently I do this kind of operation with statistical software packages, but I wonder if that is possible in MySQL somehow. It's often needed when merging time series from different sources. Is there a SQL way to do it?
Use the UNION statement.
It's something like:
(SELECT * FROM table WHERE column = 34)
UNION
(SELECT * FROM table WHERE column2 = 45);
Then you can add an ORDER BY at the end like:
(SELECT * FROM table WHERE column = 34)
UNION
(SELECT * FROM table WHERE column2 = 45)
ORDER BY column;
SELECT mycol
FROM mytable
UNION ALL
SELECT othercol
FROM othertable
This question might be helpful: Merge 2 tables for a SELECT query?