Avatar of akiles99
akiles99
Flag for India asked on

mssql help...

Hi guys ,

I will tell clearly what i want...

How to use aggregate function such as AVG,SUM on column which has a varchar(20) data type...
Actually  my need is i want to store this AVG(col1)& SUM(col1) into another table(column) using triggers..

But when using avg,sum in varchar it throws error....

Also i want to find the date difference...And average of datedifference...

So i need 4 queries...

Please dontsend any link...

Thanks & Regards,
akiles
Microsoft SQL Server 2005

Avatar of undefined
Last Comment
akiles99

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
chapmandew

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
akiles99

ASKER
chapmandew

:)  I think I've answered both (hopefully).  Let me know if you have questions on either.
akiles99

ASKER
Ok nice...

But send me a sample trigger say the 4 queries to be stored in 4 variables namely @a,@b,@c,@d in table 1..

I want to update a 4 different columns col 1,col2,col3,col4  in another table2...
ie ..when a insert or update occurs on table1 @a value has to be stored in col1 and so on ...

Your help has saved me hundreds of hours of internet surfing.
fblack61
chapmandew

You've lost me...Im not sure what you're trying to do in a trigger here.
akiles99

ASKER
Oh ok no probs i will work it....Thanks for ur time..

Best Regards
akiles
chapmandew

Ok.  Hopefully the queries above will steer you in the right direction...if you get stuck, just let me know.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
akiles99

ASKER
Query 2 & 4 throws error....
chapmandew

post the queries you're using and the error
akiles99

ASKER
i have a column which stores the (datetime)....I want to retreive only the last year...how come?
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
akiles99

ASKER
suppose i have a column name udate..

UDATE
1/7/2008 12:00:00 AM
3/29/2008 12:00:00 AM
6/10/2008 12:00:00 AM
4/11/2008 12:00:00 AM

I want to find the Year to Date for the current year....
chapmandew

hmmm, so for the data you provided, what would you expect the outcome to be?
akiles99

ASKER
select avg(datediff(day, Max(ondate), getdate())) from conthist where rectype='S';
Msg 130, Level 15, State 1, Line 1
Cannot perform an aggregate function on an expression containing an aggregate or a subquery.

sum(cast(ondate as decimal)) from conthist where rectype='S'
Msg 102, Level 15, State 1, Line 2
Incorrect syntax near 'cast'.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
akiles99

ASKER
For the Year to date question:
 If a set of sales occured before 1/1/2008, then it would not be part of this year to date sales, would it?
so for all sales done between 1/1/2008 and 31/12/2008 i need a total?
the data would move to this field in another table  ULYRSALES field  when the System date turned past 1/1/2009 etc using trigger........
akiles99

ASKER
thanks