Click here to Skip to main content
16,016,301 members
Home / Discussions / Database
   

Database

 
Questionsearch based on free text(multiple words entered by user) Pin
rakeshjena9-Dec-13 18:16
rakeshjena9-Dec-13 18:16 
AnswerRe: search based on free text(multiple words entered by user) Pin
Mycroft Holmes9-Dec-13 18:38
professionalMycroft Holmes9-Dec-13 18:38 
AnswerRe: search based on free text(multiple words entered by user) Pin
Eddy Vluggen10-Dec-13 8:10
professionalEddy Vluggen10-Dec-13 8:10 
Questionsearch based on free text(multiple words entered by user Pin
rakeshjena9-Dec-13 18:09
rakeshjena9-Dec-13 18:09 
QuestionTempTables Pin
tsunamigang8-Dec-13 18:23
tsunamigang8-Dec-13 18:23 
AnswerRe: TempTables Pin
Abhinav S8-Dec-13 19:19
Abhinav S8-Dec-13 19:19 
GeneralRe: TempTables Pin
tsunamigang9-Dec-13 18:02
tsunamigang9-Dec-13 18:02 
QuestionVariable column SQL statments Pin
Wombaticus8-Dec-13 6:13
Wombaticus8-Dec-13 6:13 
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! Smile | :) 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... Smile | :)
AnswerRe: Variable column SQL statments Pin
Mycroft Holmes8-Dec-13 11:47
professionalMycroft Holmes8-Dec-13 11:47 
GeneralRe: Variable column SQL statments Pin
Wombaticus8-Dec-13 12:39
Wombaticus8-Dec-13 12:39 
Questionms access select query? Pin
murali_utr8-Dec-13 4:17
murali_utr8-Dec-13 4:17 
AnswerRe: ms access select query? Pin
PIEBALDconsult8-Dec-13 4:38
mvePIEBALDconsult8-Dec-13 4:38 
QuestionRe: ms access select query? Pin
murali_utr8-Dec-13 4:47
murali_utr8-Dec-13 4:47 
AnswerRe: ms access select query? Pin
PIEBALDconsult8-Dec-13 5:04
mvePIEBALDconsult8-Dec-13 5:04 
QuestionBackup & restore of data Like organization chart in sqlserver Pin
mehdin698-Dec-13 1:46
mehdin698-Dec-13 1:46 
AnswerRe: Backup & restore of data Like organization chart in sqlserver Pin
Mycroft Holmes8-Dec-13 11:50
professionalMycroft Holmes8-Dec-13 11:50 
QuestionMYSQL - right join - where Pin
nelsonpaixao7-Dec-13 8:46
nelsonpaixao7-Dec-13 8:46 
AnswerRe: MYSQL - right join - where Pin
Mycroft Holmes7-Dec-13 12:22
professionalMycroft Holmes7-Dec-13 12:22 
Questionhow to make remote sql query analyzer in asp.net Pin
raj kumar tamang6-Dec-13 9:19
raj kumar tamang6-Dec-13 9:19 
AnswerRe: how to make remote sql query analyzer in asp.net Pin
Mycroft Holmes6-Dec-13 13:15
professionalMycroft Holmes6-Dec-13 13:15 
Questionms access 2007 update query? Pin
murali_utr5-Dec-13 22:08
murali_utr5-Dec-13 22:08 
AnswerRe: ms access 2007 update query? Pin
Eddy Vluggen5-Dec-13 22:26
professionalEddy Vluggen5-Dec-13 22:26 
AnswerRe: ms access 2007 update query? Pin
Dineshshp5-Dec-13 22:37
professionalDineshshp5-Dec-13 22:37 
GeneralRe: ms access 2007 update query? Pin
Jörgen Andersson5-Dec-13 22:54
professionalJörgen Andersson5-Dec-13 22:54 
QuestionINTERSECT vs SELF-JOINS Pin
Joe Woodbury5-Dec-13 12:16
professionalJoe Woodbury5-Dec-13 12:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.