August 25, 2006 at 1:20 pm
Using the simplified sql below I need the average freight charge between the dates for all of the ShipCountry's orders. (I have hard coded the dates for demo purposes only as it is these that I need referenced from the outer query's where clause.)
select
OrderDate,
ShipCountry,
ShipCity,
Freight,
/* how do I get to the outer query's date range ? */
(SELECT AVG(Freight) FROM Orders WHERE ShipCountry = O.ShipCountry AND OrderDate between '01-jan-1997' and '01-jan-2000') AS CountryAverageFreight
from
Northwind.dbo.Orders O
where
ShipCity = 'Paris' and OrderDate between '01-jan-1997' and '01-jan-2000'
Thanks
August 25, 2006 at 1:51 pm
will you be able to use variables, like this?:
declare @startDate datetime, @endDate datetime
set @startDate = '1/1/1997'
set @endDate = '1/1/2000'
select
OrderDate,
ShipCountry,
ShipCity,
Freight,
/* how do I get to the outer query's date range ? */
(
SELECT AVG(Freight)
FROM Orders
WHERE ShipCountry = O.ShipCountry
AND OrderDate between @startDate and @endDate
) AS CountryAverageFreight
from
Northwind.dbo.Orders O
where ShipCity = 'Paris'
and OrderDate between @startDate and @endDate
August 25, 2006 at 2:02 pm
Yes but unfortunately I can't use variables / stored procs because this view is to be used as a datasource for Reporting Services Report Builder which only allows tables and views.
August 25, 2006 at 2:07 pm
How are you going to get the dates in at all without variables or hard coding?
August 25, 2006 at 2:10 pm
How will you use the date range? Will you do something like:
select *
from vMyView v
where v.StartDate = '1/1/1997' and v.EndDate = '1/1/2000'
August 25, 2006 at 2:12 pm
by passing them to the view as a where clause.
ie select * from vwView where ShipCity = 'Paris' and OrderDate between '01-jan-1997' and '01-jan-2000' .
August 25, 2006 at 2:17 pm
No way you can do that in a view. When you query a view, you're querying static information just like querying a table; you can't calculate average on the fly like that.
August 25, 2006 at 2:18 pm
guys the main issue here is that the subquery needs to be able to reference the range of dates (not just a single date) of the outer query.
thanks
August 25, 2006 at 2:20 pm
Instead of using a view, why not just query the tables directly? You're doing a query anyway.
August 25, 2006 at 2:34 pm
>>for Reporting Services Report Builder which only allows tables and views.
Huh ? Since when did RS prevent use of stored procs ?
August 25, 2006 at 2:47 pm
Report Builder can only use tables / views as Model datasources.
August 26, 2006 at 12:21 am
I don't have access to SQL Server right now so I couldn't test this... I think it may work...
select
OrderDate,
ShipCountry,
ShipCity,
Freight,
/* how do I get to the outer query's date range ? */
(SELECT AVG(Freight) FROM Orders WHERE ShipCountry = O.ShipCountry AND OrderDate IN o.OrderDate) AS CountryAverageFreight
from
Northwind.dbo.Orders O
where
ShipCity = 'Paris' and OrderDate between '01-jan-1997' and '01-jan-2000'
--Jeff Moden
Change is inevitable... Change for the better is not.
August 26, 2006 at 3:12 am
Thanks Jeff but no - I see what you're getting at but it doesn't quite do it.
Viewing 13 posts - 1 through 12 (of 12 total)
You must be logged in to reply to this topic. Login to reply