I have two tables A and B.
Table A Column contains values from 1 to 9 . and Table B Contains values 2,6,7.
Now my requirement is to get UNION of A and B and count of duplicate records. I'm using SqlServer.
My result should be like this
ResultColumn Count
1 1
2 2
3 1
4 1
5 1
6 2
7 2
8 1
9 1
EDIT: Use UNION ALL to account duplicates.
SELECT
[Column],
COUNT(*) AS [Count]
FROM(
SELECT [Column] FROM TableA
UNION ALL
SELECT [Column] FROM TableB
)t
GROUP BY [Column]
Related
If I have two columns:
col1 col2 amount
1 2 15
2 3 12
1 3 10
3 1 4
3 2 3
And I perform a group by col1,col2 then I get a row for each combination (present) in the data.
My problem though is, that I dont always have all combinations, but I would want to return a row of each combination still. So if there isn't a combination. for example 2 -> 1 then I would want its value to be 0.
Can I somehow specify the "levels" of the group by?
I'm using SQL Oracle.
and the outcome I would want is:
1 -> 2 15
1 -> 3 10
2 -> 1 0
2 -> 3 12
3 -> 1 4
3 -> 2 3
With their respective amount, and 0 if they dont exist, or null works. ( I have a filter to exclude where col1 and col2 are same)
Generate all the rows using cross join and then filter for the ones you want:
select c1.col1, c2.col2, coalesce(t.amount, 0)
from (select 1 as co1l from dual union all
select 2 as co1l from dual union all
select 3 as co1l from dual
) c1 cross join
(select 1 as co12 from dual union all
select 2 as co12 from dual union all
select 3 as co12 from dual
) c2 left join
t
on t.col1 = c1.col1 and t.col2 = c2.col2
where c1.col1 <> c2.col2;
I have 2 tables both structured as (id, views)
Table 1:
id views
A 1
B 2
B 3
C 3
C 4
D 4
Table 2:
id views
C 1
D 3
D 4
E 5
E 7
F 8
I'm looking to sum views of ids that are both in table 1 and 2 (id C and D) in this case so the output would be:
Table 3:
id views
C 8
D 11
You could use the following query in your case :
select a.id,sum(a.views) from ( select * from table1 union table2 ) as a group by id;
select id,sum(views) from (select * from table1 union all select * from table2)a where a.id="C" or a.id="D" group by id;
I have a data below
ID DATE COMPLIANCE ISBREAKREDUCED ISSECONDMEALBREAKREDUCED
1208240 4/12/2015 2 1 1
How do I use it in my base query to show different rows for column ISBREAKREDUCED & ISSECONDMEALBREAKREDUCED if there values are 1.
This table join condition is based on id and date fields which is why it selects single row. How can I split this row into a multiple rows in the output?
I think you are looking for a typical UNPIVOT query.
You could do it as:
SQL> WITH DATA AS(
2 SELECT 1208240 ID, 1 ISBREAKREDUCED, 1 ISSECONDMEALBREAKREDUCED FROM dual UNION ALL
3 SELECT 1208241 ID, 2 ISBREAKREDUCED, 3 ISSECONDMEALBREAKREDUCED FROM dual
4 )
5 SELECT *
6 FROM
7 (SELECT *
8 FROM DATA UNPIVOT (ISSECONDMEALBREAKREDUCED FOR ISBREAKREDUCED IN (ISBREAKREDUCED AS 1, ISSECONDMEALBREAKREDUCED AS 1))
9 )
10 WHERE ISBREAKREDUCED =1
11 AND ISSECONDMEALBREAKREDUCED =1
12 /
ID ISBREAKREDUCED ISSECONDMEALBREAKREDUCED
---------- -------------- ------------------------
1208240 1 1
1208240 1 1
SQL>
I want to fetch the records in order passed for IN condition.
select * from table where id in(6,3,7,1);
is returning the rows as
id name
1 abc
3 xy
6 ab
7 ac
but I want to display the records in same orders as ids passed in condition in Oracle
id name
6 ab
3 xy
7 ac
1 abc
Please help me in fetching the records in same order as in condition ids in oracle. The values in IN condition may change dynamically.
You can do this with a case statement in the order by clause or using a join.
select *
from table
where id in(6,3,7,1)
order by (case id when 6 then 1 when 3 then 2 when 7 then 3 when 1 then 4 end);
Or:
with ids as (
select 6 as id, 1 as ordering from dual union all
select 3 as id, 2 as ordering from dual union all
select 7 as id, 3 as ordering from dual union all
select 1 as id, 4 as ordering from dual
)
select *
from table t join
ids
on t.ids = ids.id
order by ids.ordering;
Note that you don't need the in in this case, because the join does the filtering.
you can use trick
select * from table where id in(6,3,7,1) order by case when id = 6 then 1
id = 3 then 2
id = 7 then 3
id = 1 then 4
end
i want get query from 2 table but show all column overhand
first all column from table 1 Second all column from table 2 then show overhand
table1
----------
a 1 2 3
table2
----------
b 4 5 6
query Result
----------
a 1 2 3
b 4 5 6
Grateful
do you mean:
Select * from table1
Union
Select * from table2
if you want all data no matter they are repetitive or not use UNION ALL otherwise if you want to have unique result use UNION
for example if you had 1 2 3 in table2 the result with UNION will be
1 2 3
and result with UNION ALL will be
1 2 3
1 2 3
Try using UNION in your query:
select * from table1
union all
select * from table2