Solved

MS SQL join, include all in table 1 and show null when table 2..

Posted on 2013-01-07
7
312 Views
Last Modified: 2013-01-07
MS SQL to include all users and include null when no assigned taskdetail and value when there is a task detail. See below;

Users
User_ID   name
1               Joe
2               Mary
3               James
4               Mathew
5               Sam


TaskDetail
TaskDetail_ID  FK_User_ID  FK_TaskName_ID

1                          1            2
2                          4            1
3                          3            2
4                          1            3
5                            1                1
6                            1                5

I want to have all users returned for a specific task whether they are assigned to it or not.

Result for when FK_TaskName_ID = 2 should result as follows

Name             FK_User_ID        FK_TaskName_ID
Joe                          1                  2
Mary                  2                  null
James                  3                  2
Mathew                  4                  null                  
Sam                          5                  null
0
Comment
Question by:petel2k
[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
  • 3
  • 3
7 Comments
 
LVL 65

Expert Comment

by:Jim Horn
ID: 38751038
Two things:

(1)  That would be a LEFT JOIN, which means 'return all rows in the table LEFT of the join, and include only values from the right of the LEFT join where they exist.

(2)  >I want to have all users returned for a specific task...
The below CASE block handles that, although I'm sure you'll want to change the hard-coded 2 to include a @variable, or however you're determining the FK_TaskName_ID value.

SELECT users.Name, TaskDetail.FK_User_ID, CASE WHEN TaskDetail.FK_TaskName_ID = 2 THEN 2 ELSE NULL END as FK_TaskName_ID
FROM users
LEFT JOIN TaskDetail ON users.user_id = TaskDetail.FK_USER_ID
0
 

Author Comment

by:petel2k
ID: 38751082
error
Msg 102, Level 15, State 1, Line 3
Incorrect syntax near '.'.

'FROM users'
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 38751144
Check the spelling of all table and column names to make sure they match.
The T-SQL I posted looks correct as far as syntax.
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 12

Accepted Solution

by:
sachitjain earned 500 total points
ID: 38751148
select U.Name, T.FK_User_Id, T.FK_TaskName_ID
from Users U left join TaskDetail T on U.UserId = T.FK_User_Id and T.FK_TaskName_ID = 2
0
 

Author Closing Comment

by:petel2k
ID: 38751173
Great job!!! Thanks for the help
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 38751369
How's that answer different from the one I posted earlier?  Just curious.
The CASE block does the same as the WHERE.
0
 

Author Comment

by:petel2k
ID: 38751384
Hey Jim..

In your case, your also worked. And I misspelled. Both solutions worked. I need to modify award as a split. Sorry.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sql query help 15 52
Copy data to New Year 9 32
Returning Message/Value Back to Access from a SQL Server trigger 3 34
SQL Syntax 6 29
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

756 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