Solved

Query selectint item when it should not

Posted on 2010-11-23
19
247 Views
Last Modified: 2012-05-10
What i am trying to do is select a vehcile from tblGSAVehicles based on its location, vehcile type and that it is not in use in tblSchedule.  The problem is that I can limit it to one vehicle but it always pick it no matter if it is scheduled for those dates .  How can I limit it to only if its not scheduled for a start date or end date in tblschedule.  The code below I thought would work.


So with code below it pulls up the same vehicle 3 times I put in a distinct and this goes away, but at this point I am  confused and to why the vehicle is being pulled when its scheduled in tblSchedue and shoould pull nothing.
SELECT	 v.intVehicleID, 

		t.strVehicleType, 

		v.intYear, 

		v.strMake, 

		v.strModel, 

		v.strColor, 

		v.intPass 

FROM	tblGSAVehicles AS v LEFT JOIN 

		tblSchedule AS s ON v.intVehicleID = s.intVehicleID INNER JOIN 

		tblVehicleType AS t ON v.intVehicleTypeID = t.intVehicleTypeID 

WHERE	v.intLocID = 1 AND 

		t.intVehicleTypeID = 4 AND 

		v.bitInUse = 1 AND NOT EXISTS (Select * from tblschedule where

		((Convert(varchar(10), s.dtStartDate, 101) NOT BETWEEN '11/23/2010' AND '11/25/2010') OR s.dtStartDate IS NULL) AND 

		((Convert(varchar(10), s.dtStartDate, 101) NOT BETWEEN '11/23/2010' AND '11/25/2010') OR s.dtEndDate IS NULL))

Open in new window

0
Comment
Question by:kdeutsch
  • 8
  • 6
  • 4
  • +1
19 Comments
 
LVL 12

Expert Comment

by:ill
Comment Utility
-- you mean like removing LEFT from LEFT JOIN on line 8 ?
SELECT       v.intVehicleID,
            t.strVehicleType,
            v.intYear,
            v.strMake,
            v.strModel,
            v.strColor,
            v.intPass
FROM      tblGSAVehicles AS v JOIN
            tblSchedule AS s ON v.intVehicleID = s.intVehicleID INNER JOIN
            tblVehicleType AS t ON v.intVehicleTypeID = t.intVehicleTypeID
WHERE      v.intLocID = 1 AND
            t.intVehicleTypeID = 4 AND
            v.bitInUse = 1 AND NOT EXISTS (Select * from tblschedule where
            ((Convert(varchar(10), s.dtStartDate, 101) NOT BETWEEN '11/23/2010' AND '11/25/2010') OR s.dtStartDate IS NULL) AND
            ((Convert(varchar(10), s.dtStartDate, 101) NOT BETWEEN '11/23/2010' AND '11/25/2010') OR s.dtEndDate IS NULL))
0
 
LVL 51

Expert Comment

by:HainKurt
Comment Utility
try

FROM      tblGSAVehicles AS v LEFT JOIN
-->
FROM      tblGSAVehicles AS v JOIN
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
please evaluate this:
SELECT   v.intVehicleID, 

                t.strVehicleType, 

                v.intYear, 

                v.strMake, 

                v.strModel, 

                v.strColor, 

                v.intPass 

FROM    tblGSAVehicles AS v 

LEFT JOIN  tblSchedule AS s 

  ON v.intVehicleID = s.intVehicleID 

 AND (s.dtStartDate IS NULL OR  s.dtStartDate < CONVERT(datetime, '11/25/2010', 101) + 1)

 AND (s.dtEndDateIS NULL OR s.dtEndDate >= CONVERT(datetime, '11/25/2010', 101) )



INNER JOIN  tblVehicleType AS t 

  ON v.intVehicleTypeID = t.intVehicleTypeID 

WHERE v.intLocID = 1 

  AND t.intVehicleTypeID = 4 

  AND v.bitInUse = 1 

  AND s.intVehiculeID IS NULL

Open in new window

0
 
LVL 51

Assisted Solution

by:HainKurt
HainKurt earned 125 total points
Comment Utility
try this
SELECT	 v.intVehicleID, 

		t.strVehicleType, 

		v.intYear, 

		v.strMake, 

		v.strModel, 

		v.strColor, 

		v.intPass 

FROM	tblGSAVehicles AS v INNER JOIN 

		tblSchedule AS s ON v.intVehicleID = s.intVehicleID INNER JOIN 

		tblVehicleType AS t ON v.intVehicleTypeID = t.intVehicleTypeID 

WHERE	v.intLocID = 1 AND 

		t.intVehicleTypeID = 4 AND 

		v.bitInUse = 1 AND 

		(s.dtStartDate IS NULL OR s.dtStartDate NOT BETWEEN convert(datetime, '11/23/2010', 101) AND convert(datetime, '11/25/2010', 101)) 

		AND 

		(s.dtEndDate IS NULL OR s.dtEndDate NOT BETWEEN convert(datetime, '11/23/2010', 101) AND convert(datetime, '11/25/2010', 101))

Open in new window

0
 
LVL 51

Expert Comment

by:HainKurt
Comment Utility
formatted
SELECT v.intVehicleID, t.strVehicleType, v.intYear, v.strMake, v.strModel, v.strColor, v.intPass 

  FROM tblGSAVehicles AS v INNER JOIN 

       tblSchedule AS s ON v.intVehicleID = s.intVehicleID INNER JOIN 

       tblVehicleType AS t ON v.intVehicleTypeID = t.intVehicleTypeID 

 WHERE v.intLocID = 1 AND 

       v.bitInUse = 1 AND 

       t.intVehicleTypeID = 4 AND 

       (s.dtStartDate IS NULL OR s.dtStartDate NOT BETWEEN convert(datetime, '11/23/2010', 101) AND convert(datetime, '11/25/2010', 101)) 

       AND 

       (s.dtEndDate IS NULL OR s.dtEndDate NOT BETWEEN convert(datetime, '11/23/2010', 101) AND convert(datetime, '11/25/2010', 101))

Open in new window

0
 

Author Comment

by:kdeutsch
Comment Utility
angelIII:
This gives me 4 results, which would not be correct becuase it should be limiting by intLocId and intVehicleTypeId as well as if it si scheduled in tblSchedule.  It comes up with this.  4 is scheduled for those dates and the rest should not show since they are not the correct vehicleId.

VechicleId    type              year                 make            modle           color            pass
4      Van      2002      Chevrolet      CG3300      White      15
5      Van      2009      Chevrolet      CG3300      White      15
8      Van      2008      Chevrolet      CG3300      Tan      15
9      Van      2008      Chevrolet      CG3300      Black      15
0
 

Author Comment

by:kdeutsch
Comment Utility
ill:,
Still selct wrong it produces this result.  Which is 3 of the same vehicle but nothing should appear as this vechile is schedules in tblSchedule.  Otherwise its limiting to correct vehicle.


4      Van      2002      Chevrolet      CG3300      White      15
4      Van      2002      Chevrolet      CG3300      White      15
4      Van      2002      Chevrolet      CG3300      White      15
0
 

Author Comment

by:kdeutsch
Comment Utility
HainKurt,
Ist query produces same resulst but 2 of ame vehicle.  this same vehicle is scheduled already with those dates in tblSchedule so it should not show up.

4      Van      2002      Chevrolet      CG3300      White      15
4      Van      2002      Chevrolet      CG3300      White      15
0
 
LVL 51

Expert Comment

by:HainKurt
Comment Utility
try this
SELECT v.intVehicleID, t.strVehicleType, v.intYear, v.strMake, v.strModel, v.strColor, v.intPass 

  FROM tblGSAVehicles AS v INNER JOIN 

       tblVehicleType AS t ON v.intVehicleTypeID = t.intVehicleTypeID 

 WHERE v.intLocID = 1 AND 

       v.bitInUse = 1 AND 

       t.intVehicleTypeID = 4 AND 

       v.intVehicleID not in (

         select s.intVehicleID 

           FROM tblSchedule AS s

           WHERE (s.dtStartDate IS NULL OR s.dtStartDate NOT BETWEEN convert(datetime, '11/23/2010', 101) AND convert(datetime, '11/25/2010', 101)) 

                 AND 

                 (s.dtEndDate IS NULL OR s.dtEndDate NOT BETWEEN convert(datetime, '11/23/2010', 101) AND convert(datetime, '11/25/2010', 101))

       )

Open in new window

0
Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 375 total points
Comment Utility
I think I had a wrong parameter:
SELECT   v.intVehicleID, 

                t.strVehicleType, 

                v.intYear, 

                v.strMake, 

                v.strModel, 

                v.strColor, 

                v.intPass 

FROM    tblGSAVehicles AS v 

LEFT JOIN  tblSchedule AS s 

  ON v.intVehicleID = s.intVehicleID 

 AND (s.dtStartDate IS NULL OR  s.dtStartDate < CONVERT(datetime, '11/25/2010', 101) + 1)

 AND (s.dtEndDateIS NULL OR s.dtEndDate >= CONVERT(datetime, '11/23/2010', 101) )



INNER JOIN  tblVehicleType AS t 

  ON v.intVehicleTypeID = t.intVehicleTypeID 

WHERE v.intLocID = 1 

  AND t.intVehicleTypeID = 4 

  AND v.bitInUse = 1 

  AND s.intVehiculeID IS NULL

Open in new window

0
 

Author Comment

by:kdeutsch
Comment Utility
I am uploading the tblSructure for 3 tables.  basically what I am trying to do is get a vechile base4d on location and vehciletype that is not scheduled in tblschedule.
TblSctructure.xlsx
0
 
LVL 51

Expert Comment

by:HainKurt
Comment Utility
AngelIII,

tblGSAVehicles AS v LEFT JOIN  tblSchedule AS s

does not eliminate any vehicle from resultset if tehre is a schedule for that car... so all queries in this form will give wrong result

...
FROM    tblGSAVehicles AS v LEFT JOIN  tblSchedule AS s ON v.intVehicleID = s.intVehicleID ...
0
 

Author Comment

by:kdeutsch
Comment Utility
All,
Actuallu both queries are returning the same exact result.
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
HainKurt:
  you overlook the condition in the WHERE clause ... WHERE s.xxxx IS NULL  ...
0
 
LVL 51

Expert Comment

by:HainKurt
Comment Utility
yes I missed that part AngelIII ;) but what happened to interval?

AND (s.dtStartDate IS NULL OR  s.dtStartDate < CONVERT(datetime, '11/25/2010', 101) + 1)

-->

AND (s.dtStartDate IS NULL OR  (s.dtStartDate >= CONVERT(datetime, '11/23/2010', 101) and s.dtStartDate < CONVERT(datetime, '11/25/2010', 101) + 1)

query should eliminate the schedules between 23 & 25 also should eliminate the null values...
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
here are my conditions:
 AND (s.dtStartDate IS NULL OR  s.dtStartDate < CONVERT(datetime, '11/25/2010', 101) + 1)
 AND (s.dtEndDateIS NULL OR s.dtEndDate >= CONVERT(datetime, '11/23/2010', 101) )

Open in new window


so, this will match IF:
* there is a schedule that either starts before the "end date" of the schedule interval, or has no start date at all
AND IF
* there is a schedule that either ends after the "start date" of the schedule interval, or has no end date at all

so, if you find a schedule with start+end date both NULL, that will match for the JOIN
so, if you find a schedule with start date <= 25/11 (or start date null) and end date >= 23/11 (or NULL), that will also match for the JOIN
if you have a schedule with start date > 25/11 (with hence end date > 25/11 also) OR end_date < 23/11 (and hence start date < 23/11 also) , it will not match the JOIN

the additional condition WHERE s.<somefield> IS NULL will make that if the (LEFT) JOIN had a match, the actual data row will NOT be returned.
so, if the LEFT JOIN had no match, the actual row WILL by returned

I have worked with this kind of conditions quite some time, I am confident it shall work...
if the understanding of the requests is correct
0
 

Author Comment

by:kdeutsch
Comment Utility
All,
We have done further testing and here is what is happening with both sets.

hainKurt,
We can schedule but for some reason originally we thought code was working but it keeps coming up with the same 3 vehicles 5, 8, 9 and we thought it should show 4 as well but it does not.  But is we schedule the other vehicles they still come up.  as we schedule them they still come up as not being scheduled.

angelIII:
I every situation that we have run the vehicle scheduler the code has worked to cut out the vehicle if it is scheduled in tblSchedule.  Meeting all the requirements of location and type of vehicle.

We are still doing more extensive testing to make sure every situation passes.
0
 

Author Comment

by:kdeutsch
Comment Utility
All,

Ok we have gotten both statements to work in all sistuation for
HainKurt solution weused the same conditions statement as angellls and it works just fine.
Tahnsk for the help.
0
 

Author Closing Comment

by:kdeutsch
Comment Utility
Thanks for the help.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now