I have a table:
table1
col1 col2
1 a
1 b
1 c
I want to add rownum but from a specific number, for ex. starting from 100, so it would look like:
col1 col2 rn
1 a 100
1 b 101
1 c 102
I know how to add rownum like below:
select a.*, rownum as rn from table1 a;
But I don't know how to add from a specific number. How to do it in Oracle SQL?
The ANSI SQL way of doing this would be to use ROW_NUMBER:
SELECT col1, col2, 99 + ROW_NUMBER() OVER (ORDER BY col2) rn
FROM table1;
You might be able to use Oracle's ROWNUM function here, but in that case you would also need to provide an ORDER BY clause to your query:
SELECT col1, col2, 99 + ROWNUM AS rn
FROM table1
ORDER BY col2;
I think it's not necessary to get this kind of rownum from systematic source, you can use below query for example
select a.*, 99+rownum as rn from table1 a;
Related
Here is a mock table
MYTABLE ROWS
PKEY 1,2,3,4,5,6
COL1 a,b,b,c,d,d
COL2 55,44,33,88,22,33
I want to know which rows have duplicated COL1 values:
select col1, count(*)
from MYTABLE
group by col1
having count(*) > 1
This returns :
b,2
d,2
I now want all the rows that contain b and d. Normally, I would use where in stmt, but with the count column, not certain what type of statement I should use?
maybe you need
select * from MYTABLE
where col1 in
(
select col1
from MYTABLE
group by col1
having count(*) > 1
)
Use a CTE and a windowed aggregate:
WITH CTE AS(
SELECT Pkey,
Col1,
Col2,
COUNT(1) OVER (PARTITION BY Col1) AS C
FROM dbo.YourTable)
SELECT PKey,
Col1,
Col2
FROM CTE
WHERE C > 1;
Lots of ways to solve this here's another
select * from MYTABLE
join
(
select col1 ,count(*)
from MYTABLE
group by col1
having count(*) > 1
) s on s.col1 = mytable.col1;
--mytable
col1 col2 col3
1 A red
2 A green
3 B purple
4 C blue
Let's call the table above mytable. I want to select only distinct values from col2:
SELECT DISTINCT
col2
FROM
mytable
When I do this the output looks like this, which is expected:
col2
A
B
C
but how do I perform the same type of query, yet keep all columns? The output would look like below. In essence I'm going through mytable looking at col2, and when there's multiple occurrences of col2 I'm only keeping the first row.
col1 col2 col3
1 A red
3 B purple
4 C blue
Do SQL functions (eg DISTINCT) have arguments I could set? I could imagine it to be something like KeepAllColumns = TRUE for this DISTINCT function? Or do I need to perform JOINs to get what I want?
You can use window functions, particularly row_number():
select t.*
from (select t.*, row_number() over (partition by col2 order by col2) as seqnum
from mytable t
) t
where seqnum = 1;
row_number() enumerates the rows, starting with "1". You can control whether you get the oldest, earliest, biggest, smallest . . .
You can use the QUALIFY clause in Teradata:
SELECT col1, col2, col3
FROM mytable
QUALIFY ROW_NUMBER() OVER(PARTITION BY col2 ORDER BY col2) = 1 -- Get 1st row per group
If you want to change the ordering for how to determine which col2 row to get, just change the expression in the ORDER BY.
With NOT EXISTS:
select m.* from mytable m
where not exists (
select 1 from mytable
where col2 = m.col2 and col1 < m.col1
)
This code will return the rows for which there is not another row with the same col2 and a smaller value in col1.
I want top 2 valus per key. The result would look like:
What should be the hive query.
You can use a window function with OVER() close:
select col1,col2 from (SELECT col1,
col2,
ROW_NUMBER() OVER (PARTITION BY col1 ORDER BY col2 DESC) AS row_num
FROM data)f
WHERE f.row_num < 3
order by col1,col2
I had an issue with writing a query that would gather groups in a column, and then select one of them by a number.
A good person (#sstan) gave me this:
select your_col
from (select your_col,
row_number() over (order by your_col) as rn
from your_table
group by your_col)
where rn = 2
And it works. However, it appears that my query needs to consider other columns. For now, it looks like this:
select MAINCOL, sum(some_col+other_col) as together_col, count(another_col)
from my_table
where date_col >= next_day(trunc(sysdate), 'MONDAY') - 14
and date_col < next_day(trunc(sysdate), 'MONDAY') - 7
group by MAINCOL, other_col, together_col
order by MAINCOL
So the challenge is to extend the upper query with what is below. Although I couldn't make it work, it seems simple..
You may try with Inner table alias
SELECT your_col,rn.your_col,rn.your_col2,rn.your_col3
FROM(select your_col,your_col2,your_col3,row_number() over (order by your_col)
from your_table group by your_col)as rn where rn = 2
Got it!
With help of Stack, of course.
select t.*
from (select MAINCOL, col1, col2, col3, col4, DENSE_RANK()OVER(ORDER BY MAINCOL) GROUPID
from tab_1
group by MAINCOL, col1, col2
) t
where GROUPID = 1;
I am using Oracle Database 10g Enterprise Edition 10.2.0.4.0 64bit
and I would like to know, what is the best way to write the following query?
1. With rownum
SELECT * FROM
(
SELECT ID_DONNEE_H, DATE_DONNEE
FROM DONNEE_H d
WHERE d.DATE_DONNEE > sysdate -50000
AND d.ID_SC = 38648
ORDER BY DATE_DONNEE DESC
)
WHERE rownum=1;
2. With a WITH clause
with req as (
select d.ID_DONNEE_H, row_number() over (order by DATE_DONNEE desc) as seqnum
from DONNEE_H d
where d.DATE_DONNEE > sysdate -50000
AND d.ID_SC = 38648 )
select * from req where seqnum = 1;
3. With rank clause
select * from (select d.ID_DONNEE_H, row_number() over (order by DATE_DONNEE desc) as seqnum
from DONNEE_H d
where d.DATE_DONNEE > sysdate -50000
AND d.ID_SC = 38648) test
where seqnum = 1;
I think 2 and 3 are similar, but which is the fastest, 1, 2 or 3?
I don't think you can particularly generalise as to which query is the "best" in all situations. As with most IT questions, the answer is: "it depends"! You would need to investigate each query on its own merits.
As an aside, you've missed off another alternative - assuming that you're only after a single column, rather than the whole row for the highest column you're interested in:
with sample_data as (select 10 col1, 3 col2 from dual union all
select 20 col1, 3 col2 from dual union all
select 30 col1, 1 col2 from dual union all
select 40 col1, 2 col2 from dual)
select max(col1) keep (dense_rank first order by col2 desc) col1_val_of_max_col2
from sample_data;
COL1_VAL_OF_MAX_COL2
--------------------
20