This is the query which I am using to get the distinct values:
SELECT DISTINCT
LOGIN_BY,
RECEIVED_BY
FROM SAMPLE
My requirement is I need the distinct values of both the columns as a single output. Is there any way to do that?
try this
(SELECT DISTINCT LOGIN_BY as ID from sample )
union all
(Select Distinct RECEIVED_BY as ID FROM SAMPLE)
Try it like this:
select distinct a from (
select
distinct LOGIN_BY as a
from
SAMPLE
union
select
distinct RECEIVED_BY as a
from
SAMPLE
);
This works for me on an Oracle database.
Related
Hello I want merge two tables in oracle like below -
I tried below answer from stack overflow but not getting answer like Output table
Combining Two Tables With Oracle SQL
Please try with below query:
select * from (
select id, name, rate,total from table1
union all
select id, name, rate,total from table2
) as t
order by t.id asc
use union all
select id, name, rate,total from table1
union all
select id, name, rate,total from table2
Please find attached image for table structure
You could try using UNION ALL for build a unique column result and select the max from this
select max(col)
from (
select col1 col
from trans_punch
union all
select col2
from trans_punch) t
You can use a common table expression to union the columns, then select the max.
;with cteUnionPunch(Emp_id, both_punch) AS
(
SELECT Emp_id, In_Punch FROM trans_punch
UNION ALL
SELECT Emp_id, Out_Punch FROM trans_punch
)
SELECT Emp_id, max(both_punch) FROM cteUnionPunch GROUP BY Emp_id
You can use apply :
select tp.Emp_id, max(tpp.Punchs)
from trans_punch as tp cross apply
( values (In_Punch), (Out_Punch) ) tpp(Punchs)
group by tp.Emp_id;
I have two table containing some information as below
ENRNO, PROGRAM, NAME, ADDRESS, AGE
I want to find data referencing ENRNO which is containing from one of the given table but I don't know which table have the information.
Please suggest.
Malay Barik
If ENRNO is unique in tables u may try use UNION
select * from t1
where ENRNO ='ENRNO1'
UNION select * from t2
where ENRNO ='ENRNO1'
else use DISTINCT and subquery
select DISTINCT * from (
select * from t1
where ENRNO ='ENRNO1'
UNION select * from t2
where ENRNO ='ENRNO1')
But goodest way for solving this is redesign(normalize) you DB.
I need to return the unique records between two tables. Ideally, an UNION would solve my problem but both tables contain an object field which gives me an error(cannot ORDER objects without MAP or ORDER method) when I do UNION/distinct.
So, I was wondering if I can do a UNION ALL(to avoid the error) to get all the records first then do something to return only the unique records from there. I tried analytic function combined with the UNION ALL query but no luck so far.
Select * from Table1
union all
Select * from table2
Any help? Note:I need to return all fields.
I actually solved the problem using analytic function+row_num. The query will choose the first record for each set of duplicates hence returning only the unique records.
select * from
(
select ua.*,row_number() over (partition by p_id order by p_id ) row_num from
(
select * from table1
union all
select * from table2
)ua
) inner
where inner.row_num=1
How about this :
SELECT DISTINCT A.* FROM
(
Select * from Table1
union all
Select * from table2
) A;
(or)
SELECT col1,col2,col3...coln FROM
(
Select col1,col2,col3...coln from Table1
union all
Select col1,col2,col3...coln from table2
) A
GROUP BY A.col1,col2,col3...coln;
UNION ALL will give duplicate values as well .. instead use UNION and see if you are facing the error
I need to count rows in more than one table in SQL Server 2008. I do this:
select count(*) from (select * from tbl1 union all select * from tbl2)
But it gives me an error of incorrect syntax near ). Why?
PS. The actual number of tables can be more than 2.
In case you have different number of columns in your tables try this way
SELECT count(*)
FROM (
SELECT NULL as columnName
FROM tbl1
UNION ALL
SELECT NULL
FROM tbl2
) T
try this:
You have to give a name to your derived table
select count(*) from
(select * from tbl1 union all select * from tbl2)a
I think you have to alias the SELECT in the FROM clause:
select count(*)
from
(
select * from tbl1
union all
select * from tbl2
) AS SUB
You also need to ensure that the * in both tables tbl1 and tbl2 return exactly the same number of columns and they have to be matched in their type.
I don't like doing the union before doing the count. It gives the SQL optimizer an opportunithy to choose to do more work.
AlexK's (deleted) solution is fine. You could also do:
select (select count(*) from tbl1) + (select count(*) from tbl2) as cnt