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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 SUM

Author  Topic 

kwikwisi
Constraint Violating Yak Guru

283 Posts

Posted - 2010-03-03 : 04:55:41
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
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-03-03 : 04:57:49
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
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2010-03-03 : 04:59:23
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

283 Posts

Posted - 2010-03-03 : 05:02:26
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

22864 Posts

Posted - 2010-03-03 : 05:08:43


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

283 Posts

Posted - 2010-03-03 : 19:32:38
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

Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-03-04 : 01:05:20
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

283 Posts

Posted - 2010-03-04 : 06:07:16
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

283 Posts

Posted - 2010-03-04 : 19:05:59
Can pls anyone tell me how to do it ?
Go to Top of Page

haroon2k9
Constraint Violating Yak Guru

328 Posts

Posted - 2010-03-05 : 01:55:03
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

kwikwisi
Constraint Violating Yak Guru

283 Posts

Posted - 2010-03-05 : 02:20:13
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

52326 Posts

Posted - 2010-03-05 : 09:57:26
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
   

- Advertisement -