?
Solved

Improve query...

Posted on 2010-01-13
1
Medium Priority
?
183 Views
Last Modified: 2012-05-08
Hello,
Ive got this query:

SELECT  T.jobid,T.taskid,T.description,J.Job,Tk.Task FROM timesheet as T
LEFT JOIN Jobs as J ON T.JobID = J.ID
LEFT JOIN Tasks as Tk ON T.TaskID = Tk.ID
WHERE CONVERT(NVARCHAR,StartTime,101) >=  '10/19/2009'
 AND  CONVERT(NVARCHAR,StartTime,101) <=  '10/25/2009'
GROUP BY t.jobid,t.taskid,t.description,j.job,tk.task,T.TenderID

What i want to do is, if the restult of JobID is 0 or less, bring the ID from another table and place it in the same column T.jobID and the same for the column J.Job

This is what ive got and it gives me what im after but i was wondering if there is another way to do it

SELECT
      CASE
            WHEN T.JobID <=0 THEN T.TenderID
            ELSE T.JobID
      END as JobID,
      CASE
            WHEN T.JobID <=0 THEN (SELECT TenderNumber FROM Tender WHERE ID = T.TenderID)
            ELSE J.Job
      END,
 T.jobid,T.taskid,T.description,J.Job,Tk.Task FROM timesheet as T
LEFT JOIN Jobs as J ON T.JobID = J.ID
LEFT JOIN Tasks as Tk ON T.TaskID = Tk.ID
WHERE CONVERT(NVARCHAR,StartTime,101) >=  '10/19/2009'
 AND  CONVERT(NVARCHAR,StartTime,101) <=  '10/25/2009'
GROUP BY t.jobid,t.taskid,t.description,j.job,tk.task,T.TenderID


Thanks!
0
Comment
Question by:arcross
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 6

Accepted Solution

by:
FVER earned 2000 total points
ID: 26305632
Your solution is OK but :
1 - GROUP BY by clause could be replaced by DISTINCT KEYWORD
2 - You could also consider a left join with Tender table instead of inline select
3 - The way you compare dates is wrong : you should convert strings to dates, not the other way. Alphabetical order is not the same as date chronology with date format 101. At least you shoul use a Year-month-date format, but converting strings to dates is better.
SELECT DISTINCT
      CASE
            WHEN T.JobID <=0 THEN T.TenderID
            ELSE T.JobID
      END as JobID,
      CASE 
            WHEN T.JobID <=0 THEN Te.TenderNumber
            ELSE J.Job
      END,
 T.jobid,T.taskid,T.description,J.Job,Tk.Task
FROM timesheet as T
LEFT JOIN Jobs as J ON T.JobID = J.ID 
LEFT JOIN Tasks as Tk ON T.TaskID = Tk.ID
LEFT JOIN Tender as Te ON Te.ID = T.TenderID
WHERE StartTime BETWEEN CONVERT(Datetime,'10/19/2009',101) AND CONVERT(Datetime,'10/25/2009',101)

Open in new window

0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

770 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