combine the results from the function and the select statement

I have the function that returns 4 columns i am asked to return 5 vendor id from the select statement that tests the function.  I have the following function and the select statement however i dont get 5 colmuns I only get one that comes from the vendors table.
/*
CREATE function fnDateRange
(@DateMin varchar(50), @datemax varchar(50))
returns table

return
(SELECT InvoiceTotal, InvoiceNumber, InvoiceDate,InvoiceTotal - CreditTotal - PaymentTotal AS Balance

 FROM Invoices
 WHERE InvoiceDate BETWEEN @DateMin AND @DateMax)

*/
--drop function fnDateRange
--/*
select VENDORNAME, (SELECT * FROM dbo.fndaterange('april 10 2002', 'april 18 2002')AS Q
where o.VENDORID = Q.VENDORID)as maxe
FROM VENDORS AS o
--*/


tagtekinAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

Guy Hengel [angelIII / a3]Billing EngineerCommented:
CREATE function fnDateRange
(@DateMin varchar(50), @datemax varchar(50))
returns table ( InvoiceTotal as numeric(12,4), InvoiceNumber as int, Invoicedate as date, Balance as numeric(12,4))

return
(SELECT InvoiceTotal, InvoiceNumber, InvoiceDate,InvoiceTotal - CreditTotal - PaymentTotal AS Balance
 FROM Invoices
 WHERE InvoiceDate BETWEEN @DateMin AND @DateMax)



select o.VENDORNAME, q.*
from vendors o
join dbo.fndaterange('april 10 2002', 'april 18 2002') AS Q
on o.VENDORID = Q.VENDORID
Aneesh RetnakaranDatabase AdministratorCommented:
Hi tagtekin,

I think You should use  INNER JOIN between tour table and function.
But the problem here is i didn't dind any proper joining keye in your function. It's better to return one unique key, something like VendorId from your funcion and do a join

SELECT VendorName, t.*
FROM Vendors
INNER JOIN  dbo.fnDateRange (...) t
on t.VendorID = Vendors.vendorID


Aneesh
Guy Hengel [angelIII / a3]Billing EngineerCommented:
good point:

CREATE function fnDateRange
(@DateMin varchar(50), @datemax varchar(50))
returns table ( VendorIDas int , InvoiceTotal as numeric(12,4), InvoiceNumber as int, Invoicedate as date, Balance as numeric(12,4))

return
(SELECT Vendorid, InvoiceTotal, InvoiceNumber, InvoiceDate,InvoiceTotal - CreditTotal - PaymentTotal AS Balance
 FROM Invoices
 WHERE InvoiceDate BETWEEN @DateMin AND @DateMax)
SolarWinds® VoIP and Network Quality Manager(VNQM)

WAN and VoIP monitoring tools that can help with troubleshooting via an intuitive web interface. Review quality of service data, including jitter, latency, packet loss, and MOS. Troubleshoot call performance and correlate call issues with WAN performance for Cisco and Avaya calls

Aneesh RetnakaranDatabase AdministratorCommented:
CREATE function fnDateRange
(@DateMin varchar(50), @datemax varchar(50))
returns table ( VendorID as int , InvoiceTotal as numeric(12,4), InvoiceNumber as int, Invoicedate as date, Balance as numeric(12,4)) -- missed s space here

return
(SELECT Vendorid, InvoiceTotal, InvoiceNumber, InvoiceDate,InvoiceTotal - CreditTotal - PaymentTotal AS Balance
 FROM Invoices
 WHERE InvoiceDate BETWEEN @DateMin AND @DateMax)
tagtekinAuthor Commented:
yes but this function created returns the vendor id directly from the function i needed to return that column from the select statement.

Thanks
Aneesh RetnakaranDatabase AdministratorCommented:
> but this function created returns the vendor id directly from the function i needed to return that column from the select

both are the same, since you are joing the the two, again if you wish you can use the following

SELECT Vendors.VendorName, Vendors.VendorID, t.InvoiceTotal, t.InvoiceNumber, t.InvoiceDate,t.balance
FROM Vendors
INNER JOIN  dbo.fnDateRange (...) t
on t.VendorID = Vendors.vendorID

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
tagtekinAuthor Commented:
I did this and add it made the change with your comment and it worked. THank you. I m posting another one with my other problem. Hope to see your comments.

/*
CREATE function fnDateRange
(@DateMin varchar(50), @datemax varchar(50))
returns table

return
(SELECT Vendorid, InvoiceTotal, InvoiceNumber, InvoiceDate,InvoiceTotal - CreditTotal - PaymentTotal AS Balance

 FROM Invoices
 WHERE InvoiceDate BETWEEN @DateMin AND @DateMax)
*/
--*/
--drop function fnDateRange
--select * from dbo.fndaterange('april 10 2002', 'april 18 2002')

SELECT Vendors.VendorName, Vendors.VendorID, t.InvoiceTotal, t.InvoiceNumber, t.InvoiceDate,t.balance
FROM Vendors
INNER JOIN  dbo.fnDateRange ('april 10 2002', 'april 18 2002')as t on t.VendorID = Vendors.vendorID
Aneesh RetnakaranDatabase AdministratorCommented:
are you getting any error ?
tagtekinAuthor Commented:
no I was getting since there was no vendorid coming from the function but i added andsolved the problem.
Aneesh RetnakaranDatabase AdministratorCommented:
Ok.. now it seems that you are solved the problem
tagtekinAuthor Commented:
yes thank you very much
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
Databases

From novice to tech pro — start learning today.