I have an SQLite table called match that has two columns: column1 and column2 that contain integer values:
column1 column2
------------------
5 6
6 8
8 9
90 91
1 20
10 20
I want to match duplicate numbers found in either columns and join them, including the matches second value, so that my search result returns would be:
5, 6, 8, 9
1, 20, 10
(notice that 90 and 91 have no matches and therefore are not included).
My 'guess' at making this is:
SELECT column1, column2
FROM match
WHERE column2
IN (SELECT column1
FROM match
GROUP BY column1 HAVING (COUNT(column1) > 0))
UNION
SELECT column1, column2
FROM match
WHERE column1
IN (SELECT column2
FROM match
GROUP BY column1 HAVING (COUNT(column2) > 0))
UNION
SELECT column1, column2
FROM match
WHERE column1
IN (SELECT column1
FROM match
GROUP BY column1 HAVING (COUNT(column1) > 1))
UNION
SELECT column1, column2
FROM match
WHERE column2
IN (SELECT column2
FROM match
GROUP BY column2 HAVING (COUNT(column2) > 1))
and the result is almost what I need:
5 6
6 8
8 9
1 20
10 20
But what I really need is to have the result grouped somehow. For example:
(5, 6, 8, 9) (1, 10, 20)
Is this possible? And is my SQL attempt over-complicated?
I think this is what you want: http://sqlfiddle.com/#!7/05747/9
SELECT column1 as newColumn
FROM match WHERE column1 in (
SELECT myColumn
FROM(
SELECT count(*) as cnt, myColumn
FROM (
SELECT column1 as myColumn
FROM match
UNION ALL
SELECT column2 as myColumn
FROM match
) x
GROUP BY myColumn
HAVING cnt > 1
) y
) OR column2 in (
SELECT myColumn
FROM(
SELECT count(*) as cnt, myColumn
FROM (
SELECT column1 as myColumn
FROM match
UNION ALL
SELECT column2 as myColumn
FROM match
) x
GROUP BY myColumn
HAVING cnt > 1
) y
)
UNION
SELECT column2 as newColumn
FROM match WHERE column1 in (
SELECT myColumn
FROM(
SELECT count(*) as cnt, myColumn
FROM (
SELECT column1 as myColumn
FROM match
UNION ALL
SELECT column2 as myColumn
FROM match
) x
GROUP BY myColumn
HAVING cnt > 1
) y
) OR column2 in (
SELECT myColumn
FROM(
SELECT count(*) as cnt, myColumn
FROM (
SELECT column1 as myColumn
FROM match
UNION ALL
SELECT column2 as myColumn
FROM match
) x
GROUP BY myColumn
HAVING cnt > 1
) y
)
Related
using BigQuery, I would like to be able to divide one column, column1, into two separate columns, column2, and column3 with 50% of all records in column1 in column2 and 50% of all records in column1 in column 3. Ex column1 has 8 records of the number 2. I'd like to create a column2 with 4 records of the number 2 and column3 with 4 records of the number 2.
Is there a query to write this in BigQuery?
Column1
2
2
2
2
2
2
2
2
Column2
2
2
2
2
Column3
2
2
2
2
try:
SELECT
Column1 AS Column2
FROM `my-project.my-dataset.my-table`
WHERE 1=1
QUALIFY ROW_NUMBER() OVER (ORDER BY Column1) <= (
SELECT COUNT(*)/2
FROM `my-project.my-dataset.my-table`
);
SELECT
Column1 AS Column3
FROM `my-project.my-dataset.my-table`
WHERE 1=1
QUALIFY ROW_NUMBER() OVER (ORDER BY Column1) > (
SELECT COUNT(*)/2
FROM `my-project.my-dataset.my-table`
);
This will give you 2 results: One for each Column2 and Column3 with the first and second half of the data respectively order by Column1 (to use analytical functions you always have to specify an ORDER BY inside an OVER clause)
For random order try:
CREATE TEMP TABLE a AS (
SELECT Column1 as Column2
FROM `my-project.my-dataset.my-table`
WHERE 1=1
QUALIFY
ROW_NUMBER() OVER (ORDER BY RAND()) <= (SELECT COUNT(*)/2 FROM `my-project.my-dataset.my-table`)
);
SELECT Column1 as Column3
FROM `my-project.my-dataset.my-table`
WHERE Column1 NOT IN (SELECT * FROM a);
SELECT * FROM a
In this case you'll get 3 results: first one is the temporary table creation and the other 2 are the columns 2 and 3.
SELECT
COUNT (*)
FROM
table
WHERE
column2 > 10
UNION
SELECT
COUNT (*)
FROM
table
WHERE
column3 > 10
UNION
SELECT
COUNT (*)
FROM
table
WHERE
column3 > 10
UNION
Results:
(No column name)
155
433
931
How to put the results into a new column?
Column1 Column2 Column3
155 433 931
SELECT
(
SELECT
COUNT (*)
FROM table
WHERE column2 > 10
) AS [col1],
(
SELECT
COUNT (*)
FROM table
WHERE column3 > 10
) AS [col2],
(
SELECT
COUNT (*)
FROM table
WHERE column3 > 10
) AS [col3]
Should do the trick
I have 2 queries, both are returning sum, I want the difference between those sums in a single query.
Query 1:
select sum(Records) from my_table where column1 ='1' and column2 = 'abc'
Query 2:
select sum(Records) from my_table where column1 ='2' and column2 = 'adf'
note both queries having same table_name.
My table:
column1 column2 column3 column4 records
1 aa something abc 12
2 bb something acd 25
2 aa something adf 04
1 bb something abc 21
Any help will be appreciated.
Thanks In advance.
You can just use the your existing queries as the columns and take the difference.
with my_table (column1, column2, column3,column4, records) as
( select 1, 'aa', 'something', 'abc', 12 from dual union all
select 2, 'bb', 'something', 'acd', 25 from dual union all
select 2, 'aa', 'something', 'adf', 04 from dual union all
select 1, 'bb', 'something', 'abc', 21 from dual
)
select r1, r2, r1-r2 diff
from (select sum(Records) r1 from my_table where column1 ='1' and column4 = 'abc')
, (select sum(Records) r2 from my_table where column1 ='2' and column4 = 'adf');
Note: I changed the queries to reference column4 instead of column2 in order to match the sample date provided.
Use conditional aggregation:
select sum(case when column1 = '1' and column2 = 'abc' then Records
when column1 = '2' and column2 = 'adf' then - Records
else 0
end) as diff
from my_table ;
Or, if you want to keep queries you've already written, use them as CTEs:
with
a (suma) as
(select sum(Records) from my_table where column1 ='1' and column2 = 'abc'),
b (sumb) as
(select sum(Records) from my_table where column1 ='2' and column2 = 'adf')
select a.suma - b.sumb as difference
from a cross join b
I don't have Oracle handy today but this should get you close.
select ( select sum(Records) from my_table where column1 ='1' and column2 = 'abc') -
(select sum(Records) from my_table where column1 ='2' and column2 = 'adf')
as Total from dual;
Good day
I have Table1:
COLUMN1 COLUMN2 Column3
----------------------------
Eva Apple 1
Eva Apple 2
Eva Apple 3
Eva Apple 4
Eva Apple 5
Eva Apple 6
Bob Apple 1
Bob Samsung 1
Bob Samsung 2
... ... ...
I need
COLUMN1 COLUMN2 Column3
----------------------------
Eva Apple 6
Bob Samsung 2
Bob Apple 1
... ... ...
How i can setup string for select only rows with MAX values in Column3 ?
My version of string is :
SELECT MAX(Column3) , [column2], [Column2]
FROM Table1
WHERE Column3 = MAX ;
Thanks for Opinions
You can use row_number
Select top (1) with ties *
from table1
order by row_number() over (partition by Column1, Column2 order by Column3 desc)
Other way is to use outer query:
Select * from (
Select *, RowN = row_number() over (partition by Column1, Column2 order by Column3 desc) from table1 ) a
Where a.RowN = 1
You want to find the maximum Column3 for each combination of Column1 and Column2.
You can achieve this with a GROUP BY
SELECT Column1, Column2, MAX(Column3)
FROM Table1
GROUP BY Column1, Column2
See https://learn.microsoft.com/en-us/sql/t-sql/queries/select-group-by-transact-sql
select * from (
SELECT *, rn=ROW_NUMBER() over (partition by COLUMN1,COLUMN2,Column3 order by
Column3 desc)
FROM Table1
)
WHERE rn=1
Please try the following:
WITH B AS
(
SELECT
Column1, Column2, Column3,
ROW_NUMBER() OVER (PARTITION BY Column1, Column2 ORDER BY Column3 DESC) AS row_num
FROM
Table1
)
SELECT Column1, Column2, Column3
FROM B
WHERE row_num = 1
You need to add a group by. In queries of this type, you have a set of columns you want the values from (these are the columns you group by) and you have other columns most of whose values you'll throw away. You use a function like MAX, MIN, SUM, AVG to specify what to do with the data from rows that are "thrown away". The result is a unique set of values from the columns that were grouped, and a single value corresponding to the min/max/avg etc from the columns that were not grouped:
SELECT [column1], [Column2], MAX(Column3) as Column3
FROM Table1
GROUP BY Column3 ;
Add a Group by on column 3.
Select col1, col2, max(col3)
from test
Group By col3
You can use a function like DENSE_RANK().
In this example, if there are duplicate values you want to retrieve.
declare #t as table (COLUMN1 char(3), COLUMN2 varchar(10), COLUMN3 int)
INSERT #t (COLUMN1 , COLUMN2 , COLUMN3 ) select 'Eva', 'Apple' ,1
INSERT #t (COLUMN1 , COLUMN2 , COLUMN3 ) select 'Eva', 'Apple' ,2
INSERT #t (COLUMN1 , COLUMN2 , COLUMN3 ) select 'Eva', 'Apple' ,3
INSERT #t (COLUMN1 , COLUMN2 , COLUMN3 ) select 'Bob', 'Apple' ,1
INSERT #t (COLUMN1 , COLUMN2 , COLUMN3 ) select 'Bob', 'Samsung' ,1
INSERT #t (COLUMN1 , COLUMN2 , COLUMN3 ) select 'Bob', 'Samsung' ,2
SELECT * FROM (
SELECT DENSE_RANK() OVER (PARTITION BY COLUMN1, COLUMN2 ORDER BY COLUMN1, COLUMN3 desc) [Max] , * from #t ) as T
WHERE max = 1 -- Set here what position do you want
Order by COLUMN3
I am using oracle 10g EE database.I have one table mytable and has two columns and data is as follows:
Note: I want to find out data based on same value in 2nd column only, it does not matter whether there exists same or different value in first column.
10 is repeated 3 times for A, B and C and these 3 are required output
similarly 20 is repeated 2 times for C and D and these are also required output
column1 column2
-------------- ---------------
A 10 //required
A 10 //required
B 10 //required
C 20//required
D 20//required
E 30--------not required as 30 is only here and not duplicated
F 40--------not required as 40 is only here and not duplicated
following output is required i.e. same value in 2nd column having same or different values in 1st column
column1 column2
-------------- ---------------
A 10
A 10
B 10
C 20
D 20
SELECT column1,
column2
FROM <table> t1
WHERE column2 IN (SELECT column2
FROM <table> t2
GROUP BY column2
HAVING count(*) > 1);
It sounds like you want
SELECT *
FROM table_name t1
WHERE column2 IN( SELECT column2
FROM table_name t2
GROUP BY column2
HAVING COUNT(*) > 1 )
This appears to work with your sample data
SQL> with table_name as (
2 select 'A' column1, 10 column2 from dual union all
3 select 'A', 10 from dual union all
4 select 'B', 10 from dual union all
5 select 'C', 20 from dual union all
6 select 'D', 30 from dual)
7 SELECT *
8 FROM table_name t1
9 WHERE column2 IN( SELECT column2
10 FROM table_name t2
11 GROUP BY column2
12 HAVING COUNT(*) > 1 );
C COLUMN2
- ----------
B 10
A 10
A 10
select * from table where column2 in ( select column2 from table group by coulmn2 having count(*)>1);
should work for you.
Thanks
Abhi