SQL, call UDF in the select statement with group function and parameter

Hi Experts, I want to call UDF with group function and parameter. I wrote the query below , but I know that calling function part is not correct. Can you help me correct my query? Thank you.


declare @StartDate date

declare @EndDate date

declare @Dealerid int

declare @count int

set @StartDate ='2015-05-01'

set @EndDate='2015-05-27'

set @Dealerid= 326

set @count= (select count(*) from MMD_Feed..Deal where alternativedeal is null and Dealerid = @Dealerid

and cast(submittedtimestamp as date) between @StartDate and @EndDate);
 
 

with cte as (select @count as dealstarts,

dealerid,

sum(case when AlternativeDeal='1'then 1 else 0 end)as AlternateDeals,

sum(case when Manualsend='1'then 1 else 0 end) as CreatedDeals

from MMD_Feed..Deal

where dealerid =@Dealerid

and cast(submittedtimestamp as date) between @StartDate and @EndDate

and dealerid in (select dealerid from MMD_Feed..Dart)

group by dealerid)
 
 



select sum(ad.[WidgetInteraction]) as pencils,

cte.dealstarts,

cte.alternatedeals,

cte.createddeals,
 
select count(*) as uniqueopp from [dbo].[GetUniqueOppbyDealer] (@StartDate, @EndDate, @DealerId)

from makemydeal_com.[dbo].[adobe_Standard] ad
 
inner join cte cte on cte.dealerid=ad.dealerid
 
where cte.dealerid= @Dealerid
 
and cast(date as date) between @StartDate and @EndDate
 
group by cte.dealstarts, cte.alternatedeals, cte.createddeals,select count(*) as uniqueopp from [dbo].[GetUniqueOppbyDealer] (@StartDate, @EndDate, @DealerId)
tanj1035Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Deepak ChauhanSQL Server DBACommented:
Try this, however i did not test it.

declare @StartDate date
declare @EndDate date
declare @Dealerid int
declare @count int
set @StartDate ='2015-05-01'
set @EndDate='2015-05-27'
set @Dealerid= 326
set @count= (select count(*) from MMD_Feed..Deal where alternativedeal is null and Dealerid = @Dealerid
and cast(submittedtimestamp as date) between @StartDate and @EndDate)
 
;with cte as (select @count as dealstarts,
dealerid,
sum(case when AlternativeDeal='1'then 1 else 0 end)as AlternateDeals,
sum(case when Manualsend='1'then 1 else 0 end) as CreatedDeals
from MMD_Feed..Deal
where dealerid =@Dealerid
and cast(submittedtimestamp as date) between @StartDate and @EndDate
and dealerid in (select dealerid from MMD_Feed..Dart)
group by dealerid)

select sum(ad.[WidgetInteraction]) as pencils, cte.dealstarts, cte.alternatedeals, cte.createddeals,
(select count(*) as uniqueopp from [dbo].[GetUniqueOppbyDealer] (@StartDate, @EndDate, @DealerId))
from makemydeal_com.[dbo].[adobe_Standard] ad inner join cte cte on cte.dealerid=ad.dealerid
where cte.dealerid= @Dealerid and cast(date as date) between @StartDate and @EndDate
group by cte.dealstarts, cte.alternatedeals, cte.createddeals,
(select count(*) as uniqueopp from [dbo].[GetUniqueOppbyDealer] (@StartDate, @EndDate, @DealerId))
declare @StartDate date
declare @EndDate date
declare @Dealerid int
declare @count int
set @StartDate ='2015-05-01'
set @EndDate='2015-05-27'
set @Dealerid= 326
set @count= (select count(*) from MMD_Feed..Deal where alternativedeal is null and Dealerid = @Dealerid
and cast(submittedtimestamp as date) between @StartDate and @EndDate)
  
;with cte as (select @count as dealstarts,
dealerid,
sum(case when AlternativeDeal='1'then 1 else 0 end)as AlternateDeals, 
sum(case when Manualsend='1'then 1 else 0 end) as CreatedDeals
from MMD_Feed..Deal 
where dealerid =@Dealerid
and cast(submittedtimestamp as date) between @StartDate and @EndDate
and dealerid in (select dealerid from MMD_Feed..Dart)
group by dealerid)

select sum(ad.[WidgetInteraction]) as pencils, cte.dealstarts, cte.alternatedeals, cte.createddeals,
(select count(*) as uniqueopp from [dbo].[GetUniqueOppbyDealer] (@StartDate, @EndDate, @DealerId))
from makemydeal_com.[dbo].[adobe_Standard] ad inner join cte cte on cte.dealerid=ad.dealerid
where cte.dealerid= @Dealerid and cast(date as date) between @StartDate and @EndDate
group by cte.dealstarts, cte.alternatedeals, cte.createddeals, 
(select count(*) as uniqueopp from [dbo].[GetUniqueOppbyDealer] (@StartDate, @EndDate, @DealerId))

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Brian CroweDatabase AdministratorCommented:
Is your function scalar or table-valued?
0
tanj1035Author Commented:
Hi, thanks for  your reply. Your query worked, just one minor change of removing (select count(*) as uniqueopp from [dbo].[GetUniqueOppbyDealer] (@StartDate, @EndDate, @DealerId)) in group by statement.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

tanj1035Author Commented:
It is a table-valued function. May I know what the difference between them? Thx.
0
Deepak ChauhanSQL Server DBACommented:
Scalar valued function return only single value and Table valued return table. More info with expamples check this link.

https://technet.microsoft.com/en-us/library/ms177499(v=sql.105).aspx
0
tanj1035Author Commented:
Thank you for the link and I will read through.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.