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. Variable column SQL statments

Variable column SQL statments

Scheduled Pinned Locked Moved Database
database
3 Posts 2 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.
  • W Offline
    W Offline
    Wombaticus
    wrote on last edited by
    #1

    SO I have two tables - one of "tasks" and another to log the time they are carried out each day - viz:

    table "tasks"
    ID - autoincrement PK
    sJob - varchar task name

    table "tasks_log"
    ID - autoincrement PK
    JobID - mapped to tasks.ID
    dDateTime - date and time task is performed

    Now, what I want to do is produce in one SQL statement a result set in which each row consists of the task (sJob), followed by the time it was carried out on each day within a given range So... the result set will have variable columns depending on the date range, and the SQL sdtatement will need to be built dynamically. A (probably not the best!) partial solution is as follows Suppose we want the results for three days, Dec 1 to Dec 3 SELECT T.sJob, L1.dDateTime AS Dec_1, L2.dDateTime AS Dec_2, L2.dDateTime AS Dec_3 FROM tasks AS T LEFT JOIN tasks_log AS L1 ON L1.JobID = T.ID LEFT JOIN tasks_log AS L2 ON L2.JobID = T.ID LEFT JOIN tasks_log AS L3 ON L3.JobID = T.ID WHERE DATE(L1.dDate) = '2013-12-01' AND DATE(L2.dDate) = '2013-12-02' AND DATE(L3.dDate) = '2013-12-03' ORDER BY T.sJob One obvious drawback of this is that it will be limited to a relatively small date range, but worse is that there is something I haven't told you! :) That is, that not all the tasks are carried out on all days. Given this, the result set will only consist of those tasks that were, in fact, carried out on each day within the given date range. What we need, if it's not asking the impossible, is to list them all, with NULLs showing on days any particular task was not performed. Obviously there are other approaches, but it wuild be nice if there was simple (...) SQL statement that could do it all in one go... :)

    M 1 Reply Last reply
    0
    • W Wombaticus

      SO I have two tables - one of "tasks" and another to log the time they are carried out each day - viz:

      table "tasks"
      ID - autoincrement PK
      sJob - varchar task name

      table "tasks_log"
      ID - autoincrement PK
      JobID - mapped to tasks.ID
      dDateTime - date and time task is performed

      Now, what I want to do is produce in one SQL statement a result set in which each row consists of the task (sJob), followed by the time it was carried out on each day within a given range So... the result set will have variable columns depending on the date range, and the SQL sdtatement will need to be built dynamically. A (probably not the best!) partial solution is as follows Suppose we want the results for three days, Dec 1 to Dec 3 SELECT T.sJob, L1.dDateTime AS Dec_1, L2.dDateTime AS Dec_2, L2.dDateTime AS Dec_3 FROM tasks AS T LEFT JOIN tasks_log AS L1 ON L1.JobID = T.ID LEFT JOIN tasks_log AS L2 ON L2.JobID = T.ID LEFT JOIN tasks_log AS L3 ON L3.JobID = T.ID WHERE DATE(L1.dDate) = '2013-12-01' AND DATE(L2.dDate) = '2013-12-02' AND DATE(L3.dDate) = '2013-12-03' ORDER BY T.sJob One obvious drawback of this is that it will be limited to a relatively small date range, but worse is that there is something I haven't told you! :) That is, that not all the tasks are carried out on all days. Given this, the result set will only consist of those tasks that were, in fact, carried out on each day within the given date range. What we need, if it's not asking the impossible, is to list them all, with NULLs showing on days any particular task was not performed. Obviously there are other approaches, but it wuild be nice if there was simple (...) SQL statement that could do it all in one go... :)

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

      What you need is a pivot query. This article [^]may help. The ides is to prepare your data before pivoting. Create a table with the dates you want to report, and in your pivot join those date to the data table and then pivot the result on the dates.

      Never underestimate the power of human stupidity RAH

      W 1 Reply Last reply
      0
      • M Mycroft Holmes

        What you need is a pivot query. This article [^]may help. The ides is to prepare your data before pivoting. Create a table with the dates you want to report, and in your pivot join those date to the data table and then pivot the result on the dates.

        Never underestimate the power of human stupidity RAH

        W Offline
        W Offline
        Wombaticus
        wrote on last edited by
        #3

        Thanks!

        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