I am using java/oracle. i have below issue.
SELECT
DISTINCT id_one,
status
FROM
sometable
WHERE
id_two IN (
SELECT
DISTINCT id_two
FROM
othertable
)
AND id_one IN (1946,1948,1949)
i have above query to fetch data from one table. inner query's data from other table.
now i need add one where condition to inner query.
othertable has one more field called name.
these are the ids that i mentioned: 1946,1948,1949. here each id will have names as below: name1, name2, name3
name1, name2, name3 are passed from java application.
finally the query that i need as follows.
SELECT
DISTINCT id_one,
status
FROM
sometable
WHERE
id_two IN (
SELECT
DISTINCT id_two
FROM
othertable other where other.name in('name1','name2','name3')
)
AND id_one IN (1946,1948,1949)
my questions is: for 1946 id name1 should be considered and for 1948 id name2 should be considered
*and for 1949 id name3 should be considered as criteria.* i mean query should not consider all 3 names for every id.
how can i achieve it?
This can also be done like in the following way:
select distinct s.id_one, s.status
from sometable s join othertable o on (s.id_two = o.id_two)
where (s.id_one = 1946 and o.name = 'name1')
OR (s.id_one = 1948 and o.name = 'name2')
OR (s.id_one = 1949 and o.name = 'name3');
From doc: http://docs.oracle.com/cd/B19306_01/server.102/b14200/conditions013.htm
SELECT DISTINCT id_one, status
FROM sometable
WHERE
id_two IN (
SELECT DISTINCT id_two
FROM othertable other
where ( other.name, id_one ) in (
select 'name1',1946 from dual union all
select 'name2',1948 from dual union all
select 'name3',1949 from dual )
)
Related
In the dataset shown, I am looking to isolate or show the ID that is only in workbaskets a,b or c but not in d,e or f. I would expect the ID of 111222 to fit that criteria. I have tried this SQL with no luck. Any suggestions?
select id
from table
where workbasket in ('a','b','c') and workbasket not in ('d','e','f')
Workbasket ID
a 111222
a 123456
b 987654
c 112333
d 123456
e 987654
f 112333
select id
from table
where workbasket in ('a','b','c')
EXCEPT
select id
from table
where workbasket in ('d','e','f')
You can also use NOT EXISTS subquery
select id
from table t1
where workbasket in ('a','b','c')
and NOT EXISTS
(
select *
from table t2
where t1.id = t2.id
AND t2.workbasket in ('d','e','f')
)
SQL is by default case-insensitive, but it's possible your database has that flag changed; you also don't need to specify a NOT IN clause here because anything outside of ('a', 'b', 'c') is already ignored by your first clause. TABLE is a SQL term, and may also be causing some kind of problem with your query; alias it if you can - if you can't, wrap it in backticks.
Try this:
SELECT ID
FROM `table`
WHERE Workbasket IN ('a', 'b', 'c');
WITH CTE(Workbasket, ID) AS
(
SELECT 'A',123456 UNION ALL
SELECT 'A',111222 UNION ALL
SELECT 'B',987654 UNION ALL
SELECT 'C',112333 UNION ALL
SELECT 'D',123456 UNION ALL
SELECT 'E',987654 UNION ALL
SELECT 'F',112333
)
SELECT C.ID
FROM CTE AS C
GROUP BY C.ID
HAVING MAX(C.Workbasket) IN('A','B','C')
I have two tables having same columns.I want to get those columns whose values are distinct in both tables.How Can I achieve this?please help.Iam stuck.
imageid is primary key in both tables.Its not necessary that imageids present in first table should be present
on second table.
First table:
imageid name id
1 priya 001
2 neha 002
3 divya 003
4 santo 004
Second table:
imageid name id
1 priy 001
2 neha 003
4 santo 004
Result
imageid firstdata seconddata columnname
1 priy priya name
2 002 003 id
Assuming you have no duplicates, then you can do this with a basic inner join with union all:
select t1.imageid, t1.name as firstdata, t2.name as seconddata, 'name' as colname
from t1 join
t2
on t1.imageid = t2.imageid
where t1.name <> t2.name
union all
select t1.imageid, t1.id as firstdata, t2.id as seconddata, 'id'
from t1 join
t2
on t1.imageid = t2.imageid
where t1.id <> t2.id;
Depending on types and on the database, you might need to cast the ids to a string.
I found the following method on AskTom.com years ago and have kept it as a code template ever since. I find it's reasonably fast and I use it frequently.
SELECT COUNT(src1) AS in_first_table, COUNT(src2) AS in_second_table, imageid, name, id
FROM (SELECT imageid, name, id,
1 AS src1,
to_number(NULL) AS src2
FROM first_table
UNION ALL
SELECT imageid, name, id,
to_number(NULL) AS src1,
2 AS src2
FROM second_table
)
GROUP BY imageid, name, id
HAVING COUNT(src1) <> COUNT(src2)
ORDER BY 3, 1 DESC;
The first two columns (counts) indicate how many records found in that table, which can identify rows where the tables have multiple matching records.
I've also used this to compare very complex queries by putting them in a WITH clause, so the main query is easier to read.
For example:
WITH first_t AS
(SELECT imageid, NAME, id
FROM first_table),
second_t AS
(SELECT imageid, NAME, id
FROM second_table)
SELECT COUNT(src1) AS in_first_table,
COUNT(src2) AS in_second_table,
imageid, NAME, id
FROM (SELECT first_t.*,
1 AS src1,
to_number(NULL) AS src2
FROM first_t
UNION ALL
SELECT second_t.*,
to_number(NULL) AS src1,
2 AS src2
FROM second_t)
GROUP BY imageid,
NAME,
id
HAVING COUNT (src1) <> COUNT (src2)
UNION will return unique records on the result. If you specify ALL (UNION ALL) will keep duplicates on the result set.
SELECT column1 column2 FROM first_table
UNION
SELECT column1 column2 FROM second_table
Is there a way to union two tables, but keep the rows from the first table appearing first in the result set? However orderby column is not in select query
For example:
Table 1
name surname
-------------------
John Doe
Bob Marley
Ras Tafari
Table 2
name surname
------------------
Lucky Dube
Abby Arnold
Result
Expected Result:
name surname
-------------------
John Doe
Bob Marley
Ras Tafari
Lucky Dube
Abby Arnold
I am bringing Data by following query
SELECT name,surname FROM TABLE 1 ORDER BY ID
UNION
SELECT name,surname FROM TABLE 2
The above query is not keeping track of order by after union.
P.S - I dont want to show ID in my select query
I am getting ORDER BY Column by joining tables. Following is my real query
SELECT tbl_Event_Type_Sort_Orders.Appraisal_Event_Type_ID AS Appraisal_Event_Type_ID , ISNULL(tbl_Appraisal_Event_Types.Appraisal_Event_Type_Display_Name, 'UnCategorized') AS Appraisal_Event_Type_Display_Name
INTO #temptbl
FROM tbl_Event_Type_Sort_Orders
INNER JOIN tbl_Appraisal_Event_Types
ON tbl_Event_Type_Sort_Orders.Appraisal_Event_Type_ID = tbl_Appraisal_Event_Types.Appraisal_Event_Type_ID
WHERE 1=1
AND User_Name='abc'
ORDER BY tbl_Event_Type_Sort_Orders.Sort_Order
SELECT * FROM #temptbl
UNION
SELECT DISTINCT (tbl_Appraisal_Event_Types.Appraisal_Event_Type_ID) AS Appraisal_Event_Type_ID , ISNULL(tbl_Appraisal_Event_Types.Appraisal_Event_Type_Display_Name, 'UnCategorized') AS Appraisal_Event_Type_Display_Name
FROM tbl_Appraisal_Event_Types
INNER JOIN tbl_Appraisal_Events
ON tbl_Appraisal_Event_Types.Appraisal_Event_Type_ID = tbl_Appraisal_Events.Event_Type_ID
INNER JOIN tbl_Appraisals
ON tbl_Appraisal_Events.Appraisal_ID = tbl_Appraisal_Events.Appraisal_ID
WHERE 1=1
AND ((tbl_Appraisals.Assigned_To_Staff_User) = 'abc' OR (tbl_Appraisals.Assigned_To_Staff_User2) = 'abc' OR (tbl_Appraisals.Assigned_To_Staff_User3) = 'abc')
Put a UNION ALL in a derived table. To keep duplicate elimination, do select distinct and also add a NOT EXISTS to second select to avoid returning same person twice if found in both tables:
select name, surname
from
(
select distinct name, surname, 1 as tno
from table1
union all
select distinct name, surname, 2 as tno
from table2 t2
where not exists (select * from table1 t1
where t2.name = t1.name
and t2.surname = t1.surname)
) dt
order by tno, surname, name
You can use a column for the table and one for the ID to order by:
SELECT x.name, x.surname FROM (
SELECT ID, TableID = 1, name, surname
FROM table1
UNION ALL
SELECT ID = -1, TableID = 2, name, surname
FROM table2
) x
ORDER BY x.TableID, x.ID
You can write as below, if you are ok with duplicate data then please use UNION ALL it will be faster:
SELECT NAME, surname FROM (
SELECT ID,name,surname FROM TABLE 1
UNION
SELECT ID,name,surname FROM TABLE 2 ) t ORDER BY ID
this will order the first row sets first then by anything you need
(haven't tested the code)
;with cte_1
as
(SELECT ID,name,surname,1 as table_id FROM TABLE 1
UNION
SELECT ID,name,surname,2 as table_id FROM TABLE 2 )
SELECT name, surname
FROM cte_1
ORDER BY table_id,ID
simply use a UNION clause with out order by.
SELECT name,surname FROM TABLE 1
UNION
SELECT name,surname FROM TABLE 2
if you wanted to order first table use the below query.
;WITH cte_1
AS
(SELECT name,surname,ROW_NUMBER()OVER(ORDER BY Id)b FROM TABLE 1 )
SELECT name,surname
FROM cte_1
UNION
SELECT name,surname
FROM TABLE 2
It is often convenient in PosgreSQL to create "tables" on the fly so to refer to them, e.g.
with
selected_ids as (
select 1 as id
)
select *
from someTable
where id = (select id from selected_ids)
Is it impossible to provide multiple values as id this way? I found this answer that suggests using values for similar problem, but I have problem with translating it to the example below.
I would like to write subqueries such as
select 1 as id
union
select 2 as id
union
select 7 as id
or
select 1 as id, 'dog' as animal
union
select 7 as id, 'cat' as animal
in more condensed way, without repeating myself.
You can use arguments in the query alias:
with selected_ids(id) as (
values (1), (3), (5)
)
select *
from someTable
where id = any (select id from selected_ids)
You can also use join instead of a subquery, example:
create table some_table (id int, str text);
insert into some_table values
(1, 'alfa'),
(2, 'beta'),
(3, 'gamma');
with selected_ids(id) as (
values (1), (2)
)
select *
from some_table
join selected_ids
using(id);
id | str
----+------
1 | alfa
2 | beta
(2 rows)
You can pass id and animal field in WITH like this
with selected_ids(id,animal) as (
values (1,'dog'), (2,'cat'), (3,'elephant'),(4,'rat')--,..,.. etc
)
select *
from someTable
where id = any (select id from selected_ids)
You should use union and IN statement like this:
with
selected_ids as (
select 1 as id
union
select 2 as id
union
select 3 as id
....
)
select *
from someTable
where id in (select id from selected_ids)
after reviewing wingedpanther's idea and looking for it, you can use his idea IF those id's are continuously like this:
with
selected_ids as (
SELECT * FROM generate_series(Start,End) --(1,10) for example
)
select *
from someTable
where id in (select id from selected_ids)
If they are not continuously , the only way you can do that is by storing those ID's in a different table(maybe you have it already and if not insert it)
And then:
select *
from someTable
where id in (select id from OtherTable)
I can perform the following SQL Server selection of distinct (or non-repeating names) from a column in one table like so:
SELECT COUNT(DISTINCT [Name]) FROM [MyTable]
But what if I have more than one table (all these tables contain the name field called [Name]) and I need to know the count of non-repeating names in two or more tables.
If I run something like this:
SELECT COUNT(DISTINCT [Name]) FROM [MyTable1], [MyTable2], [MyTable3]
I get an error, "Ambiguous column name 'Name'".
PS. All three tables [MyTable1], [MyTable2], [MyTable3] are a product of a previous selection.
After the clarification, use:
SELECT x.name, COUNT(x.[name])
FROM (SELECT [name]
FROM [MyTable]
UNION ALL
SELECT [name]
FROM [MyTable2]
UNION ALL
SELECT [name]
FROM [MyTable3]) x
GROUP BY x.name
If I understand correctly, use:
SELECT x.name, COUNT(DISTINCT x.[name])
FROM (SELECT [name]
FROM [MyTable]
UNION ALL
SELECT [name]
FROM [MyTable2]
UNION ALL
SELECT [name]
FROM [MyTable3]) x
GROUP BY x.name
UNION will remove duplicates; UNION ALL will not, and is faster for it.
EDIT: Had to change after seeing recent comment.
Does this give you what you want? This gives a count for each person after combining the rows from all tables.
SELECT [NAME], COUNT(*) as TheCount
FROM
(
SELECT [Name] FROM [MyTable1]
UNION ALL
SELECT [Name] FROM [MyTable2]
UNION ALL
SELECT [Name] FROM [MyTable3]
) AS [TheNames]
GROUP BY [NAME]
Here's another way:
SELECT x.name, SUM(x.cnt)
FROM ( SELECT [name], COUNT(*) AS cnt
FROM [MyTable]
GROUP BY [name]
UNION ALL
SELECT [name], COUNT(*) AS cnt
FROM [MyTable2]
GROUP BY [name]
UNION ALL
SELECT [name], COUNT(*) AS cnt
FROM [MyTable3]
GROUP BY [name]
) AS x
GROUP BY x.name
In case you have different amounts of columns per table, like:
table1 has 3 columns,
table2 has 2 columns,
table3 has 1 column
And you want to count the amount of distinct values of different column names, what it was useful to me in AthenaSQL was to use CROSS JOIN since your output would be only one row, it would be just 1 combination:
SELECT * FROM (
SELECT COUNT(DISTINCT name1) as amt_name1,
COUNT(DISTINCT name2) as amt_name2,
COUNT(DISTINCT name3) as amt_name3,
FROM table1 ) t1
CROSS JOIN
(SELECT COUNT(DISTINCT name4) as amt_name4,
COUNT(DISTINCT name5) as amt_name5,
MAX(t3.amt_name6) as amt_name6
FROM table2
CROSS JOIN
(SELECT COUNT(DISTINCT name6) as amt_name6
FROM table3) t3) t2
Would return a table with one row and their counts:
amt_name1 | amt_name2 | amt_name3 | amt_name4 | amt_name5 | amt_name6
4123 | 675 | 564 | 2346 | 18667 | 74567