Wednesday, March 7, 2012

How to compare 3 dates

I have 3 different dates from same table same coloumn. I want to
compare these 3 dates and find out the latest date. How can I do this?
Thanks for help!SELECT MAX(date_column) FROM table
<superbaby@.gmail.com> wrote in message
news:1171639291.878469.56900@.m58g2000cwm.googlegroups.com...
>I have 3 different dates from same table same coloumn. I want to
> compare these 3 dates and find out the latest date. How can I do this?
> Thanks for help!
>|||On Feb 16, 4:32 pm, "Aaron Bertrand [SQL Server MVP]"
<ten...@.dnartreb.noraa> wrote:
> SELECT MAX(date_column) FROM table
> <superb...@.gmail.com> wrote in message
> news:1171639291.878469.56900@.m58g2000cwm.googlegroups.com...
>
> >I have 3 different dates from same table same coloumn. I want to
> > compare these 3 dates and find out the latest date. How can I do this?
> > Thanks for help!- Hide quoted text -
> - Show quoted text -
Thank you very much!, but I need to update this max(date_coloumn) to
one of coloumn in another table. So I write like this:
update compnay
set datefield3 = max(a.freedateField_02)
inner join aTabel on aTable.customerID=compnayID and aTable.type in
(200, 203, 205)
But It returns an error: An aggregate may not appear in the set list
of an UPDATE statement.
Sorry for my stupid question. I am really bad in SQL.|||> Thank you very much!, but I need to update this max(date_coloumn) to
> one of coloumn in another table. So I write like this:
> update compnay
> set datefield3 = max(a.freedateField_02)
> inner join aTabel on aTable.customerID=compnayID and aTable.type in
> (200, 203, 205)
> But It returns an error: An aggregate may not appear in the set list
> of an UPDATE statement.
> Sorry for my stupid question. I am really bad in SQL.
Why would you need to do this? Are you going to run this UPDATE statement
every time any row in aTabel changes? You can always get that MAX date from
a query, so I see absolutely no need to replicate it to another table every
time it changes. Just include that join in your queries or create a view
that does the same.
A|||On Feb 16, 5:27 pm, "Aaron Bertrand [SQL Server MVP]"
<ten...@.dnartreb.noraa> wrote:
> > Thank you very much!, but I need to update this max(date_coloumn) to
> > one of coloumn in another table. So I write like this:
> > update compnay
> > set datefield3 = max(a.freedateField_02)
> > inner join aTabel on aTable.customerID=compnayID and aTable.type in
> > (200, 203, 205)
> > But It returns an error: An aggregate may not appear in the set list
> > of an UPDATE statement.
> > Sorry for my stupid question. I am really bad in SQL.
> Why would you need to do this? Are you going to run this UPDATE statement
> every time any row in aTabel changes? You can always get that MAX date from
> a query, so I see absolutely no need to replicate it to another table every
> time it changes. Just include that join in your queries or create a view
> that does the same.
> A
Yes, I have a report which is hard coded to use the coloumn only from
table called Company. But the date inforamtion is stored into another
table (aTable). I actually need to schdeule it and run it
automatically.
In the aTable, there 3 different type records contains the different
date for the same company. I need to update the lastest date to the
Company table which has the same companyID in the aTable.CustomerID.|||> Yes, I have a report which is hard coded to use the coloumn only from
> table called Company.
Then I would suggest changing the report. Or creating a new one and using
that one instead.
> I actually need to schdeule it and run it automatically.
And it's okay if the report returns incorrect data in between your schedule?
A|||You cannot use an aggregate function on the LHS of the SET clause in an
UPDATE statement. As an alternative, you could try:
UPDATE Company
SET date3 = ( SELECT MAX( a1.freedate2 )
FROM atable a1
WHERE a1.CompanyId = Company.CompanyId
AND a1.type IN ( 200, 203, 205 ) )
WHERE EXISTS ( SELECT *
FROM atable a1
WHERE a1.CompanyId = Company.CompanyId
AND a1.type IN ( 200, 203, 205 ) ) ;
--
Anith|||On Feb 16, 7:54 pm, "Anith Sen" <a...@.bizdatasolutions.com> wrote:
> You cannot use an aggregate function on the LHS of the SET clause in an
> UPDATE statement. As an alternative, you could try:
> UPDATE Company
> SET date3 = ( SELECT MAX( a1.freedate2 )
> FROM atable a1
> WHERE a1.CompanyId = Company.CompanyId
> AND a1.type IN ( 200, 203, 205 ) )
> WHERE EXISTS ( SELECT *
> FROM atable a1
> WHERE a1.CompanyId = Company.CompanyId
> AND a1.type IN ( 200, 203, 205 ) ) ;
> --
> Anith
Thank you, it works very well.

No comments:

Post a Comment