I have a problem, where first field in unique/distinct and the second field is like concatenated string. i am looking for the distinct rows to be displayed in the second concatenated column not on the first column and i need in the same seq of columns, for ex:
1stcolumn 2ndcolumn(concatenated)
100 ABC-123-PQR
101 ABC-123-PQR
102 ABC-123-PQR
104 ABC-123-STU
in the above example i need to select only ABC-123-PQR AND ABC-123-STU, i don't care on the first column values.
Why not just use the DISTINCT keyword?
SELECT DISTINCT col2 FROM mytable
SELECT MAX(col1), col2 FROM mytable GROUP BY col2
Related
I am trying to group column 1 by their values, and remove duplicate values of column 2 within the group.
For example,
Input
Output
I assume I need to use the function group by column 1 and use distinct to column 2, but I am not sure how to implement it.
SELECT DISTINCT Column_1,Column_2
from your_table
or
SELECT Column_1,Column_2
FROM YOUR_TABLE
GROUP BY Column_1,Column_2
You can do
SELECT distinct col2,
col1
from (Table)
GROUP BY 2
Question: if this is my data:
col1,col2,col3,col4
===================
www.com,0,dangerous,reason A
www.com,1,dangerous 2,reason B
I want the a single result where column 2 value is max, so I will use in my select the Max(col2) function - but how can I get those corresponding col3 and col4 row ?
select
col1, max(col2), col3, col4
group by
col1
and ???
Thanks
Idan
You can use order by and limit to one row. The ANSI-standard syntax is:
select t.*
from t
order by t.col2 desc
fetch first 1 row only;
Not all databases support the fetch first clause, so you might have to use select top 1, limit, or some other construct.
You can use where in select statement
Like
Select * from table name where col2=max(col2)
You can get max column entire row with single value
If the column col2 which contain same value like 1,1,2,2 at this time above query return the 2 rows. At that time if you want single row you want to use this
Select * from table name where col2=max(col2) fetch first 1 row only
Might be this helpful
I am performing a union between two tables. In order to make the two tables consistent for a UNION, I need to add a dummy column.
One table has DATE field whereas the other table does not have that field. How can I create the dummy DATE field which can either be '' (blank) or NULL?
I am trying something like this in DB2
TO_DATE('','MM/DD/YYYY') AS DUMMY_DATE
Just select null.
select col1, datecol from table1
union all
select col1, null from table2
Just use NULL as column:
NULL as Dummy
My select-statement looks like:
Select column1, column2
From Table1
Group By column2
column1 is a CLOB and I want to receive one of the values that is part of one group. I know they are all the same so it doesn't matter which one I get. I've tried functions like MIN and MAX but they don't accept CLOB as a type.
To be clear I don't want to aggregate the CLOBs just pick one of them.
This is a simplification of the actual SELECT statement and the GROUP BY clause is necessary.
So with this data:
column1 column2
qwerty 1
qwerty 1
asdfgh 2
asdfgh 2
I want to get:
qwerty 1
asdfgh 2
Any idea how this could be done?
A CLOB value cannot be used for grouping or inside a distinct clause.
The only chance you have is to convert the CLOB to a varchar but that means you cannot compare the complete contents of the column (note: those are columns, not rows). If you are certain that all your CLOB values are smaller than 8000 bytes, you can use something like this:
select min(dbms_lob.substr(column1)), column2
from foo
group by column2;
You can use sub-queries.
Add unique column ID and then:
SELECT t1.col2,t2.col1
FROM
(SELECT max(ID) as IDM, col2 FROM Table1 GROUP BY col2) t1
LEFT JOIN
Table1 t2
ON t1.IDM=t2.ID
Try something like this:
SELECT DISTINCT row1, row2 FROM Table1;
My select-statement looks like:
Select column1, column2
From Table1
Group By column2
column1 is a CLOB and I want to receive one of the values that is part of one group. I know they are all the same so it doesn't matter which one I get. I've tried functions like MIN and MAX but they don't accept CLOB as a type.
To be clear I don't want to aggregate the CLOBs just pick one of them.
This is a simplification of the actual SELECT statement and the GROUP BY clause is necessary.
So with this data:
column1 column2
qwerty 1
qwerty 1
asdfgh 2
asdfgh 2
I want to get:
qwerty 1
asdfgh 2
Any idea how this could be done?
A CLOB value cannot be used for grouping or inside a distinct clause.
The only chance you have is to convert the CLOB to a varchar but that means you cannot compare the complete contents of the column (note: those are columns, not rows). If you are certain that all your CLOB values are smaller than 8000 bytes, you can use something like this:
select min(dbms_lob.substr(column1)), column2
from foo
group by column2;
You can use sub-queries.
Add unique column ID and then:
SELECT t1.col2,t2.col1
FROM
(SELECT max(ID) as IDM, col2 FROM Table1 GROUP BY col2) t1
LEFT JOIN
Table1 t2
ON t1.IDM=t2.ID
Try something like this:
SELECT DISTINCT row1, row2 FROM Table1;