Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

# QUERY QUESTION

Posted on 2016-09-09
Medium Priority
87 Views
Lets say i submit a query that says
select all from billing where accountnumber in
('1111',
'2222',
'3333')

there are actually only matches on '1111' and '3333' so it returns only two rows.
i would like for it to return those two matches with a blank in the middle signifying that there was a missing match on that account number and some sort of reference that the query was looking for '2222'
is that possible
0
Question by:jamesmetcalf74
[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

LVL 34

Accepted Solution

Brian Crowe earned 1000 total points
ID: 41791947
You would need to join your "set of possible matches" as a separate data source.

``````DECLARE @Source TABLE
(
AccountNumber	VARCHAR(10)
);

INSERT @Source (AccountNumber)
VALUES ('1111'),
('2222'),
('3333');

SELECT S.AccountNumber, B.*
FROM @Source AS S
LEFT OUTER JOIN Billing AS B
ON S.AccountNumber = B.AccountNumber
``````
1

LVL 30

Assisted Solution

Olaf Doschke earned 1000 total points
ID: 41791991
To not need such a table variable, you can of course use the account table itself and keep it at an IN (...) filter via:

``````SELECT A.AccountNumber As AccountSearched, B.*
FROM Accounts AS A
LEFT OUTER JOIN Billing AS B
ON A.AccountNumber = B.AccountNumber
WHERE A.AccountNumber in ('1111','2222','3333')
``````

On the other side, it's quite easy to use a splitter/tally table solution to split a list (one string!) '1111,2222,3333' into such a @Source table and go back to Brians query. In the newest SQL Server you even have STRING_SPLIT() as native table valued function.

Bye, Olaf.
0

LVL 32

Expert Comment

ID: 41792256
@Author - Can you update us? Is your problem solved?
0

## Featured Post

Question has a verified solution.

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

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 ?
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
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.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
###### Suggested Courses
Course of the Month9 days, 10 hours left to enroll