Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Max for duplicates

Author  Topic 

jfm
Posting Yak Master

145 Posts

Posted - 2013-07-30 : 11:49:05
Hi there,

I got many duplicates in T_1

I have Name_col and Date_col.

Each Name_col has several different dates and I need the Max date of each name_row. So i will keep the name of my client and the last date instead of the name plus the historical dates.

Any tips?

Thanks!!

Bustaz Kool
Master Smack Fu Yak Hacker

1834 Posts

Posted - 2013-07-30 : 12:11:22
[CODE]select Name_col, max(Date_col) as maxDate
from MyTable
group by Name_col[/CODE]

=================================================
The cure for anything is salt water -- sweat, tears, or the sea. -Isak Dinesen
Go to Top of Page

jfm
Posting Yak Master

145 Posts

Posted - 2013-07-30 : 13:11:55
Perfect.

Thanks a lot.

I have another question: how im able to subtract one month from a date and create a new column?

Imagine i have 2013-07-18 and I need to use a query in SQL that gives me 2013-06-18


Thank you!!

quote:
Originally posted by Bustaz Kool

[CODE]select Name_col, max(Date_col) as maxDate
from MyTable
group by Name_col[/CODE]

=================================================
The cure for anything is salt water -- sweat, tears, or the sea. -Isak Dinesen

Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2013-07-30 : 13:32:15
You can use the DATEADD function for that.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

jfm
Posting Yak Master

145 Posts

Posted - 2013-07-30 : 14:02:29
Hello

I have tried but I cannot reach the solution.

Could you type a query?

Thank you


quote:
Originally posted by tkizer

You can use the DATEADD function for that.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

Go to Top of Page

jfm
Posting Yak Master

145 Posts

Posted - 2013-07-30 : 14:06:00
The matter is that I need to subtract one month in millions of dates, is not just one.

For one date, I can do the query, but it needs to process all the dates using one query

Any tip?

Thanks



quote:
Originally posted by tkizer

You can use the DATEADD function for that.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2013-07-30 : 14:15:49
There is no issue with using DATADD with millions of dates. Just past it the column name.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

jfm
Posting Yak Master

145 Posts

Posted - 2013-07-30 : 14:33:59
Select col1, col2, col3, col4, col5, col6, col7, col8, col9, col10, dateadd(month, coldate, -1)
into new_table
from My_table
group by col1, col2, col3, col4, col5, col6, col7, col8, col9, col10

What im doing wrong?

Thank you



quote:
Originally posted by tkizer

There is no issue with using DATADD with millions of dates. Just past it the column name.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2013-07-30 : 14:36:31
You don't have the parameters in the correct order: http://msdn.microsoft.com/en-us/library/ms186819.aspx

dateadd(month, -1, coldate) as NewColumnName

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

jfm
Posting Yak Master

145 Posts

Posted - 2013-07-31 : 09:01:20
Thank you



quote:
Originally posted by tkizer

You don't have the parameters in the correct order: http://msdn.microsoft.com/en-us/library/ms186819.aspx

dateadd(month, -1, coldate) as NewColumnName

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

Go to Top of Page
   

- Advertisement -