Link to home
Start Free TrialLog in
Avatar of 4eggheads
4eggheads

asked on

SQL Query result, need a subquery to validate

I have this query:

select datediff(minute,ti.dtupdated, getdate()) as 'Minutes Old',ti.dtCreated,ti.sTicket_Number,ti.dblTotal,
      ve.sPlate,ti.bHouseAccount, ti.Ticket_ID
            
            from tickets as ti
            inner join vehicles as ve on ve.lVehicleID = ti.lVehicleID
            where datediff(day,dtupdated, getdate()) < 1 and datediff(minute,dtupdated, getdate()) > 15 and bPaid=0 and dbltotal>2 and SUSERNAME is not null
            and (bDeleted=0 or bDeleted is null)

It returns results for unpaid tickets.  However if the ticket vehicleid shows up again in the same day, I don't want to return any records.

Please advise.
Avatar of Phillip Burton
Phillip Burton

So, if there is one on a particular day, you want one record to show.

But if there are two on a particular day, you want zero records to show in total?
Avatar of PortletPaul
Do the basic selection of tickets using a  common table expression (CTE), this allows us to re-use that selection because we not only want the details but want to exclude any vehicles that have multiple tickets amongst the details. We can exclude those vehicles by using GROUP BY and a HAVING clause of COUNT(*) = 1 i.e. a vehicle that has two or more records in the selected data will be excluded.

;WITH cte AS (
              SELECT * 
              FROM tickets
              WHERE dtupdated > DATEADD(minute,-15, GETDATE() )
              AND dtupdated < DATEADD(minute, -1, GETDATE() )
              AND bPaid = 0
              AND dbltotal > 2
              AND SUSERNAME IS NOT NULL
              AND (bDeleted = 0 OR bDeleted IS NULL)
            )
SELECT
      DATEDIFF(MINUTE, ti.dtupdated, GETDATE()) AS 'Minutes Old'
    , ti.dtCreated
    , ti.sTicket_Number
    , ti.dblTotal
    , ve.sPlate
    , ti.bHouseAccount
    , ti.Ticket_ID
FROM cte AS ti
INNER JOIN vehicles AS ve ON ve.lVehicleID = ti.lVehicleID
INNER JOIN (
           SELECT lVehicleID FROM cte GROUP BY lVehicleID
           HAVING COUNT(*) = 1
           ) st on ti.lVehicleID = st.lVehicleID

Open in new window


Notes:
I only recommend CTE's for 2 reasons: when you need recursion or you can re-use. Here we can re-use.
There may be error in this suggestion because it isn't clear which tables all the fields come from (this is why you should prefix fields by their table or alias). If there are fields in the cte not contained in the tickets table please amend the where clause in that cte and add a where clause to the query that follows the cte.

============
One last comment.

In a where clause it is more efficient to avoid using functions on every row of data. e.g.

instead of this:
                           where datediff(day,dtupdated, getdate()) < 1 and datediff(minute,dtupdated, getdate()) > 15

do this:
              where dtupdated > dateadd(minute,-15, getdate() ) and dtupdated < dateadd(minute, -1, getdate() )

this way [dtupdated] is not changed, instead we alter the criteria to suit the data
ASKER CERTIFIED SOLUTION
Avatar of JR2003
JR2003

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
4eggheads, do you still need help on this question?
Avatar of 4eggheads

ASKER

yes, i'm evaluating the options...been away this week.
jr2003 yours look the most promising.  I will test it soon.
Thanks everything good except varchar mispelled :-)