How to group rows with same value in sql?
data1 123 12/03/2009
124 15/09/2009
data2 333 02/09/2010
323 02/11/2010
673 02/09/2014
444 05/01/2010
How to group rows with same value in sql?
data1 123 12/03/2009
124 15/09/2009
data2 333 02/09/2010
323 02/11/2010
673 02/09/2014
444 05/01/2010
Try this
This gives the following output
PARTITION BY
is grouping the data with the given column name, and a row number is generated in that group based on theorder by
.Here is the SQL Fiddle
I have created another fiddle based on the schema provided .fiddle2
You can use something like this:
Hope it helps you
I assume you have multiple same records as in below query. To select the distinct ones, you can either use
GROUP BY
orDISTINCT
as below:Using GROUP BY:
USING DISTINCT:
For both Queries,
ORIGINAL DATA
is :Query
OUTPUT DATA
is:EDIT: Refer Fiddle: http://sqlfiddle.com/#!3/4e3e80/9 created by Nithesh
This query would obtain the distinct records too:
Hope it helps.!