Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Query in SQL Server or Oracles

Query in SQL Server or Oracles

Scheduled Pinned Locked Moved Database
databasesql-serveroraclesysadminhelp
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mrkeivan
    wrote on last edited by
    #1

    Hi Guys,
    I want to write a query in Oracle/SQL Server (doesn’t make a lot of difference)
    I have 3 tables:
    EMSINFO (EMSINFOID, DEVICEID, DATE, PlateNumber)
    Devices (DEVICEID, INOUT)
    Roads (ROADID, ROADNAME)

    EMSINFO Contains the enters and exits of vehicles and it’s captured by a device
    We have the device id,
    And roads has the lists of all the roads in the zone
    based on the INOUTwe know whether its enter or exit
    Now I want to select PlateNumber, total time in the zone (enter time – exit time) and
    Details of when entered and when exited in a text format in one record, because each vehicle can enter and exit multiple times in a particular day

    Return Fields of store procedure:
    PlateNo: Plate number
    text: Enter from (road name) at (time)
    Exited from (road name) at ( time)
    Duration: Total Duration of being in the zone

    My question is generating that text or enters and exits
    I appreciate the help

    Mr.K

    J 2 Replies Last reply
    0
    • M mrkeivan

      Hi Guys,
      I want to write a query in Oracle/SQL Server (doesn’t make a lot of difference)
      I have 3 tables:
      EMSINFO (EMSINFOID, DEVICEID, DATE, PlateNumber)
      Devices (DEVICEID, INOUT)
      Roads (ROADID, ROADNAME)

      EMSINFO Contains the enters and exits of vehicles and it’s captured by a device
      We have the device id,
      And roads has the lists of all the roads in the zone
      based on the INOUTwe know whether its enter or exit
      Now I want to select PlateNumber, total time in the zone (enter time – exit time) and
      Details of when entered and when exited in a text format in one record, because each vehicle can enter and exit multiple times in a particular day

      Return Fields of store procedure:
      PlateNo: Plate number
      text: Enter from (road name) at (time)
      Exited from (road name) at ( time)
      Duration: Total Duration of being in the zone

      My question is generating that text or enters and exits
      I appreciate the help

      Mr.K

      J Offline
      J Offline
      Jorgen Andersson
      wrote on last edited by
      #2

      There is no info on which road the device controls. Should possibly be: Devices (DEVICEID, INOUT, ROADID)

      Wrong is evil and must be defeated. - Jeff Ello[^]

      M 1 Reply Last reply
      0
      • J Jorgen Andersson

        There is no info on which road the device controls. Should possibly be: Devices (DEVICEID, INOUT, ROADID)

        Wrong is evil and must be defeated. - Jeff Ello[^]

        M Offline
        M Offline
        mrkeivan
        wrote on last edited by
        #3

        Yes, that's right, neglected to mention it, there is a ROADIDin Device table, now do you have any solution for this problem?

        M 1 Reply Last reply
        0
        • M mrkeivan

          Yes, that's right, neglected to mention it, there is a ROADIDin Device table, now do you have any solution for this problem?

          M Offline
          M Offline
          Mycroft Holmes
          wrote on last edited by
          #4

          There are a number of solutions to this, you can use a CTE or 2 queries. Select all the records using an inner join on the foreign keys and order by platenumber road and date.This should give you the reads for each plate so you can get the max/min for each plate/road and do a simple timespan function on the dates.

          Never underestimate the power of human stupidity RAH

          1 Reply Last reply
          0
          • M mrkeivan

            Hi Guys,
            I want to write a query in Oracle/SQL Server (doesn’t make a lot of difference)
            I have 3 tables:
            EMSINFO (EMSINFOID, DEVICEID, DATE, PlateNumber)
            Devices (DEVICEID, INOUT)
            Roads (ROADID, ROADNAME)

            EMSINFO Contains the enters and exits of vehicles and it’s captured by a device
            We have the device id,
            And roads has the lists of all the roads in the zone
            based on the INOUTwe know whether its enter or exit
            Now I want to select PlateNumber, total time in the zone (enter time – exit time) and
            Details of when entered and when exited in a text format in one record, because each vehicle can enter and exit multiple times in a particular day

            Return Fields of store procedure:
            PlateNo: Plate number
            text: Enter from (road name) at (time)
            Exited from (road name) at ( time)
            Duration: Total Duration of being in the zone

            My question is generating that text or enters and exits
            I appreciate the help

            Mr.K

            J Offline
            J Offline
            Jorgen Andersson
            wrote on last edited by
            #5

            Try something like this:

            WITH Entries AS (
            SELECT PlateNumber
            ,DATE
            ,RoadName
            FROM EMSINFO e
            ,Devices d
            ,Roads r
            WHERE e.deviceID = d.DeviceID
            AND d.RoadID = r.RoadID
            AND d.InOut = 'IN'
            )
            ,Exits AS (
            SELECT e.PlateNumber
            ,e2.DATE EntryTime
            ,e2.RoadName EntryRoad
            ,Min(DATE) ExitTime
            FROM Entries e2
            ,EMSINFO e
            ,Devices d
            WHERE e2.platenumber = e.platenumber
            AND e.deviceID = d.DeviceID
            AND d.InOut = 'OUT'
            AND e.DATE > e2.DATE
            GROUP BY e.PlateNumber
            ,e2.DATE
            ,e2.RoadName
            )
            SELECT e.PlateNumber
            ,EntryTime
            ,EntryRoad
            ,ExitTime
            ,r.RoadName ExitRoad
            FROM Exits e
            ,EMSINFO e2
            ,devices d
            ,Roads r
            WHERE e.platenumber = e2.platenumber
            AND e.ExitTime = e2.DATE
            AND e2.DeviceID = d.DeviceID
            AND d.RoadID = r.RoadID

            There are probably better implementations to your problem, Search for sql+islands+and+gaps[^]. "Islands and Gaps" is the name of this classic problem.

            Wrong is evil and must be defeated. - Jeff Ello[^]

            M 1 Reply Last reply
            0
            • J Jorgen Andersson

              Try something like this:

              WITH Entries AS (
              SELECT PlateNumber
              ,DATE
              ,RoadName
              FROM EMSINFO e
              ,Devices d
              ,Roads r
              WHERE e.deviceID = d.DeviceID
              AND d.RoadID = r.RoadID
              AND d.InOut = 'IN'
              )
              ,Exits AS (
              SELECT e.PlateNumber
              ,e2.DATE EntryTime
              ,e2.RoadName EntryRoad
              ,Min(DATE) ExitTime
              FROM Entries e2
              ,EMSINFO e
              ,Devices d
              WHERE e2.platenumber = e.platenumber
              AND e.deviceID = d.DeviceID
              AND d.InOut = 'OUT'
              AND e.DATE > e2.DATE
              GROUP BY e.PlateNumber
              ,e2.DATE
              ,e2.RoadName
              )
              SELECT e.PlateNumber
              ,EntryTime
              ,EntryRoad
              ,ExitTime
              ,r.RoadName ExitRoad
              FROM Exits e
              ,EMSINFO e2
              ,devices d
              ,Roads r
              WHERE e.platenumber = e2.platenumber
              AND e.ExitTime = e2.DATE
              AND e2.DeviceID = d.DeviceID
              AND d.RoadID = r.RoadID

              There are probably better implementations to your problem, Search for sql+islands+and+gaps[^]. "Islands and Gaps" is the name of this classic problem.

              Wrong is evil and must be defeated. - Jeff Ello[^]

              M Offline
              M Offline
              mrkeivan
              wrote on last edited by
              #6

              Thanx, I will check it out

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups