Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 SQL Query - Select same column twice

Author  Topic 

Gray_Wolf
Starting Member

2 Posts

Posted - 2011-06-09 : 17:08:36
I am wondering if someone can help me out here. I am trying to create a query that will use the same column twice based about different criteria from another column in the same table.

Here is what I have, but it isn't working. Any help would be great appreciated! Thanks.

SELECT Job_Operation.Job AS "Job No.",
Job_Operation.Sched_Start AS "Material Scheduled Start"
(WHERE Job_Operation.Work_Center = 'MATERIAL'),
Job_Operation.Sched_Start AS "Cutting Scheduled Start"
(WHERE Job_Operation.Work_Center = 'CUTTING'),
FROM Job_Operation
INNER JOIN Job ON Job_Operation.Job = Job.Job
WHERE Job.Status = 'Active'
AND DATEDIFF (d, "Material Scheduled Start", "Cutting Scheduled Start") > 7

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-06-09 : 17:27:07
You can use CASE instead.

CASE WHEN ... THEN ... ELSE ... END

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

Gray_Wolf
Starting Member

2 Posts

Posted - 2011-06-09 : 17:51:21
I am new to this SQL queries.

Can you help me out with the CASE THEN?

Thanks.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-06-09 : 17:56:25
Here's an example:

SELECT asdf, ghjk = CASE WHEN qwer = 'Some Value' THEN 'poiu' WHEN ghjk = 1 THEN 'lkjh' END
FROM ...

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -