How to Select All the Columns from a Table, Plus Additional Columns Like Rownum

How do I select all the columns from a table, plus additional columns like ROWNUM?

Qualify the * with the name of the table:

select rownum, table.* from table

How to select * plus another column

I think you need to put the table_name in front of the *.

i.e.

Select Poly.*, 

Mysql 5.7: select all the columns but see one in particular at the beginning of the selection

In MySQL, unqualified * is valid only if it is in first position in the SELECT clause. To do what you want, you need to prefix the * with the table name, or alias:

SELECT oneSpecificColumn, t.* FROM mytable t

Add fields to SELECT *

Use table alias:

SQL> select 1 as t from dual;

T
----------
1

SQL> select *, 1 as t from dual;
select *, 1 as t from dual
*
ERROR at line 1:
ORA-00923: FROM keyword not found where expected


SQL> select d.*, 1 as t from dual d; --> this! "d."

D T
- ----------
X 1

SQL>

How can I select multiple columns while setting one of them as DISTINCT

Using ROW_NUMBER and TOP:

SELECT TOP 1 WITH TIES Column1, Column2, Column3, Column4, Column5, Column6
FROM yourTable
ORDER BY ROW_NUMBER() OVER (PARTITION BY Column1, Column2, Column3 ORDER BY Column4);

But this assumes that the second and third column are part of what defines a "group" in your expected result set. If instead only Column1 decides that, then use this version:

SELECT TOP 1 WITH TIES Column1, Column2, Column3, Column4, Column5, Column6
FROM yourTable
ORDER BY ROW_NUMBER() OVER (PARTITION BY Column1 ORDER BY Column4);

How to change rownum only if the next row is distinct

Here is one way - using the match_recognize clause, available since Oracle 12.1. Note that rownum is a reserved keyword, so it can't be a column name; I changed it to rnum in the input and rn in the output, adapt as needed.

The with clause is not part of the query - it's there just to include your sample data. Remove it before using the query on your actual data.

with
inputs (rnum, value) as (
select 1, 'X' from dual union all
select 2, 'X' from dual union all
select 3, 'Y' from dual union all
select 4, 'Z' from dual union all
select 5, 'Z' from dual union all
select 6, 'Z' from dual union all
select 7, 'V' from dual
)
select rn, value
from inputs
match_recognize (
order by rnum
measures match_number() as rn
all rows per match
pattern ( a+ )
define a as value = first(value)
);

RN VALUE
-- -----
1 X
1 X
2 Y
3 Z
3 Z
3 Z
4 V

In Oracle 11.2 and earlier, you can use the start-of-group method (the flags created in the subquery and counted in the outer query):

select count(flag) over (order by rnum) as rn, value
from (
select rnum, value,
case lag(value) over (order by rnum)
when value then null else 1 end as flag
from inputs
)
;

MYSQL select * plus one column again

You can do like below

SELECT `mytable`.`class` as `myclass`, `mytable`.*  FROM `mytable`

or like this

SELECT t.class AS myclass, t.*  FROM mytable t;


Related Topics



Leave a reply



Submit