SQL Select query having a table input for a condition

Hello,

In SQL SERVER 2008 I have a table called [Trips].

Select [iVehicleID],SUM(fTripDistance) AS KM
  FROM Trips
  WHERE [iVehicleID]=5 
and [dtDateTime]>='2011-11-17 19:01:46.300'
  GROUP BY [iVehicleID]

Open in new window

Results:
iVehicleID      KM
5                    9972

I have another table called [Conditions] where the vehicleID only appears once:

SELECT [iVehicleID],[dtDateTime]
  FROM [Conditions]

Open in new window

Results:
iVehicleID      dtDateTime
2                      2012-03-15 17:52:17.020
3                      2012-03-16 00:08:52.077
4                      2012-03-15 20:03:59.020
5                      2011-11-17 19:01:46.300
6                      2012-03-15 19:09:44.020

Question:

Conceptually, I'd like to do the following:

Select [iVehicleID],SUM(fTripDistance) AS KM
  FROM Trips
  
WHERE [iVehicleID]=(SELECT [iVehicleID] FROM [Conditions])

and [dtDateTime]>=(SELECT [dtDateTime] FROM [Conditions])

  GROUP BY [iVehicleID]

Open in new window


obviously, this is not allowed:
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

but how can I make this work and such that one record is returned for each row in [Conditions]:

Final Expected results:
iVehicleID      KM
2                    8974
3                    2654
4                    8954
5                    9972
6                    8564


Thanks in advanced
sequel_prequelAsked:
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.

micropc1Commented:
I believe this is what you want...

SELECT T.iVehicleID, SUM(T.fTripDistance) AS KM
FROM [Trips] T
INNER JOIN [Conditions] C
ON T.iVehicleID = C.iVehicleID
GROUP BY T.iVehicleID

Open in new window

sequel_prequelAuthor Commented:
thanks for the response.  I think you meant to Group by T.iVehicleID instead of SUM(T.fTripDistance).

Having done so the query works but how is the dtDatetime condition met?

tnx
micropc1Commented:
Lol...yes, I saw that and fixed it right before you posted...  :)

Added the date/time condition...

SELECT T.iVehicleID, SUM(T.fTripDistance) AS KM
FROM [Trips] T
INNER JOIN [Conditions] C
ON T.iVehicleID = C.iVehicleID
WHERE [C.dtDateTime]>='2011-11-17 19:01:46.300'
GROUP BY T.iVehicleID

Open in new window

Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

sequel_prequelAuthor Commented:
Yes you fixed the Group by but I still have the issue of satisfying the second condition:

and [dtDateTime]>= {the  [dtDateTime] in the Conditions table for that iVehicleid}
sequel_prequelAuthor Commented:
haha, no prob. but still not what I need.  The dtDatetime is not constant it needs to be the dtDatetime in the Conditions table:

iVehicleID      dtDateTime
2                      2012-03-15 17:52:17.020
3                      2012-03-16 00:08:52.077
4                      2012-03-15 20:03:59.020
5                      2011-11-17 19:01:46.300
6                      2012-03-15 19:09:44.020

For this example if I was to do things the long way:

Select * FROM
(

Select [iVehicleID],SUM(fTripDistance) AS KM
  FROM Trips
  
WHERE [iVehicleID]=2

and [dtDateTime]>='2012-03-15 17:52:17.020'

  GROUP BY [iVehicleID]
) a

UNION ALL

SELECT * FROM

(

Select [iVehicleID],SUM(fTripDistance) AS KM
  FROM Trips
  
WHERE [iVehicleID]=3

and [dtDateTime]>='2012-03-16 00:08:52.077'

  GROUP BY [iVehicleID]

) as b

.
.
.

Open in new window

micropc1Commented:
ah...i think I understand now. You need a subquery - not a join...

SELECT A.iVehicleID, (SELECT SUM(fTripDistance) from [Trips] B 
			where B.iVehicleID = A.iVehicleID
			and B.dtDateTime >= A.dtDateTime) AS KM
FROM [Conditions] A

Open in new window

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
sequel_prequelAuthor Commented:
Sure let me clarify.  Yes the Trips table does have a dtDatetime field.  Here is what the trips table will look like

iVehicleID,   dtDateTime,                      fTripDistance
3               2011-11-16 19:01:46.300    100
5               2011-11-17 19:01:46.300    200
3               2011-11-18 19:01:46.300    100
3               2011-11-19 19:01:46.300    200
5               2011-11-20 19:01:46.300    100
....

From my original post:
Select [iVehicleID],SUM(fTripDistance) AS KM
  FROM Trips
  WHERE [iVehicleID]=5 
and [dtDateTime]>='2011-11-17 19:01:46.300'
  GROUP BY [iVehicleID]

Open in new window


The above gives me the total KMs traveled by the vehicleid 5 since '2011-11-17 19:01:46.300'.  For results I get say [5, 9972] (see first post).


Now I have the Conditions table which is unique by vehicleid.  

iVehicleID      dtDateTime
2                      2012-03-15 17:52:17.020
3                      2012-03-16 00:08:52.077
4                      2012-03-15 20:03:59.020
5                      2011-11-17 19:01:46.300
6                      2012-03-15 19:09:44.020


What I want is the total KMs traveled by each vehcile in the Conditions table but the KMs added should only be since the Trips.dtDateTime>=Conditons.dtDateTime.

but... I just tried something working off your suggestion and it seemed to work :)

  SELECT T.iVehicleID, SUM(T.fTripDistance) AS KM

FROM [Trips] T

INNER JOIN [Conditions] C

ON T.iVehicleID = C.iVehicleID

AND T.dtDateTime>=C.dtDateTime

GROUP BY T.iVehicleID

Open in new window


I think the using WHERE instead of ANDing the innerjoin also works:

 SELECT T.iVehicleID, SUM(T.fTripDistance) AS KM

FROM [Trips] T

INNER JOIN [Conditions] C

ON T.iVehicleID = C.iVehicleID

WHERE T.dtDateTime>=C.dtDateTime

GROUP BY T.iVehicleID

Open in new window

micropc1Commented:
ha - I thought I had changed my previous comment fast enough before you actually saw it. O well... I think i understand now what you are asking. My previous post should be what you need. I hope...
sequel_prequelAuthor Commented:
Perfect. I like your's better as it returns all rows in Condition table where mine only returns the rows where the condition is satisfied. Thanks for your help.
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 2008

From novice to tech pro — start learning today.