SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 SUM
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

kwikwisi
Constraint Violating Yak Guru

280 Posts

Posted - 03/03/2010 :  04:55:41  Show Profile  Reply with Quote
SUM(CASE WHEN @days <= Convert(Int,(col1 from table1 where Name='AA') THEN 1 ELSE 0 END) as Test

Above query generates error
'Incorrect syntax near the keyword 'from'.'

Anyone tell me where it is wrong ??? Tks.

webfred
Flowing Fount of Yak Knowledge

Germany
8765 Posts

Posted - 03/03/2010 :  04:57:49  Show Profile  Visit webfred's Homepage  Reply with Quote
The shown statement is part of a query and we need to see the query.


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

bklr
Flowing Fount of Yak Knowledge

India
1693 Posts

Posted - 03/03/2010 :  04:59:23  Show Profile  Reply with Quote
quote:
Originally posted by kwikwisi

SUM(CASE WHEN @days <= Convert(Int,(col1 from table1 where Name='AA') THEN 1 ELSE 0 END) as Test

Above query generates error
'Incorrect syntax near the keyword 'from'.'

Anyone tell me where it is wrong ??? Tks.



u cant use the from clause there
can u explain the requirement briefly.
Go to Top of Page

kwikwisi
Constraint Violating Yak Guru

280 Posts

Posted - 03/03/2010 :  05:02:26  Show Profile  Reply with Quote
select @days=dbo.TestFunction()
, SUM(CASE WHEN @days <= Convert(Int,(col1 from table1 where Name='AA') THEN 1 ELSE 0 END) as Test
,...
,...
from Tbl
Where Tbl.testcol = '0', ...,..

quote:
Originally posted by webfred

The shown statement is part of a query and we need to see the query.


No, you're never too old to Yak'n'Roll if you're too young to die.

Go to Top of Page

madhivanan
Premature Yak Congratulator

India
22761 Posts

Posted - 03/03/2010 :  05:08:43  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote


select dbo.TestFunction()
, SUM(CASE WHEN @days <= Convert(Int,col1) and Name='AA' THEN 1 ELSE 0 END) as Test
,...
,...
from Tbl
Where Tbl.testcol = '0', ...,..


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

kwikwisi
Constraint Violating Yak Guru

280 Posts

Posted - 03/03/2010 :  19:32:38  Show Profile  Reply with Quote
Col1 and Name should come from another table.

tks.

quote:
Originally posted by madhivanan



select dbo.TestFunction()
, SUM(CASE WHEN @days <= Convert(Int,col1) and Name='AA' THEN 1 ELSE 0 END) as Test
,...
,...
from Tbl
Where Tbl.testcol = '0', ...,..


Madhivanan

Failing to plan is Planning to fail


Edited by - kwikwisi on 03/03/2010 20:20:50
Go to Top of Page

madhivanan
Premature Yak Congratulator

India
22761 Posts

Posted - 03/04/2010 :  01:05:20  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
quote:
Originally posted by kwikwisi

Col1 and Name should come from another table.

tks.

quote:
Originally posted by madhivanan



select dbo.TestFunction()
, SUM(CASE WHEN @days <= Convert(Int,col1) and Name='AA' THEN 1 ELSE 0 END) as Test
,...
,...
from Tbl
Where Tbl.testcol = '0', ...,..


Madhivanan

Failing to plan is Planning to fail




Join that table and use that column inside SUM

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

kwikwisi
Constraint Violating Yak Guru

280 Posts

Posted - 03/04/2010 :  06:07:16  Show Profile  Reply with Quote
Just want to compare @days with OthreTable.Column in SUM .

Eg.

SUM(CASE WHEN @days <= 100 THEN 1 ELSE 0 END) AS Y

Above, I dont want to hardcode as 100. It should come from another table.I think cannot use join coz there is no relation bet 2 tables.

Tks.


quote:
Originally posted by madhivanan

quote:
Originally posted by kwikwisi

Col1 and Name should come from another table.

tks.

quote:
Originally posted by madhivanan



select dbo.TestFunction()
, SUM(CASE WHEN @days <= Convert(Int,col1) and Name='AA' THEN 1 ELSE 0 END) as Test
,...
,...
from Tbl
Where Tbl.testcol = '0', ...,..


Madhivanan

Failing to plan is Planning to fail




Join that table and use that column inside SUM

Madhivanan

Failing to plan is Planning to fail

Go to Top of Page

kwikwisi
Constraint Violating Yak Guru

280 Posts

Posted - 03/04/2010 :  19:05:59  Show Profile  Reply with Quote
Can pls anyone tell me how to do it ?

Edited by - kwikwisi on 03/05/2010 01:36:22
Go to Top of Page

haroon2k9
Constraint Violating Yak Guru

328 Posts

Posted - 03/05/2010 :  01:55:03  Show Profile  Reply with Quote
quote:
Originally posted by kwikwisi

Can pls anyone tell me how to do it ?



Could you please execute this and see,this is what u required.iam a beginner..

declare @table1 table
(
col int,
name varchar(5)
)
insert into @table1
values (1,'test')
declare @col int,@name varchar(5)

select @col=col,@name=name from @table1
print @col
print @name


then use this params at when u summing up..

quote:

Just want to compare @days with OthreTable.Column in SUM .

Eg.

SUM(CASE WHEN @days <= 100 THEN 1 ELSE 0 END) AS Y



SUM(CASE WHEN @days <= @col THEN 1 ELSE 0 END) AS Y

Edited by - haroon2k9 on 03/05/2010 01:58:29
Go to Top of Page

kwikwisi
Constraint Violating Yak Guru

280 Posts

Posted - 03/05/2010 :  02:20:13  Show Profile  Reply with Quote
Thanks.

quote:
Originally posted by haroon2k9

quote:
Originally posted by kwikwisi

Can pls anyone tell me how to do it ?



Could you please execute this and see,this is what u required.iam a beginner..

declare @table1 table
(
col int,
name varchar(5)
)
insert into @table1
values (1,'test')
declare @col int,@name varchar(5)

select @col=col,@name=name from @table1
print @col
print @name


then use this params at when u summing up..

quote:

Just want to compare @days with OthreTable.Column in SUM .

Eg.

SUM(CASE WHEN @days <= 100 THEN 1 ELSE 0 END) AS Y



SUM(CASE WHEN @days <= @col THEN 1 ELSE 0 END) AS Y

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 03/05/2010 :  09:57:26  Show Profile  Reply with Quote
quote:
Originally posted by kwikwisi

Just want to compare @days with OthreTable.Column in SUM .

Eg.

SUM(CASE WHEN @days <= 100 THEN 1 ELSE 0 END) AS Y

Above, I dont want to hardcode as 100. It should come from another table.I think cannot use join coz there is no relation bet 2 tables.

Tks.


quote:
Originally posted by madhivanan

quote:
Originally posted by kwikwisi

Col1 and Name should come from another table.

tks.

quote:
Originally posted by madhivanan



select dbo.TestFunction()
, SUM(CASE WHEN @days <= Convert(Int,col1) and Name='AA' THEN 1 ELSE 0 END) as Test
,...
,...
from Tbl
Where Tbl.testcol = '0', ...,..


Madhivanan

Failing to plan is Planning to fail




Join that table and use that column inside SUM

Madhivanan

Failing to plan is Planning to fail




if they have no relationship then whats the meanng of using it in sum() ?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.12 seconds. Powered By: Snitz Forums 2000