Solved

QUERY QUESTION

Posted on 2016-09-09
3
79 Views
Last Modified: 2016-09-13
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
Comment
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
  • Learn & ask questions
3 Comments
 
LVL 34

Accepted Solution

by:
Brian Crowe earned 250 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

Open in new window

1
 
LVL 29

Assisted Solution

by:Olaf Doschke
Olaf Doschke earned 250 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')

Open in new window


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 29

Expert Comment

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

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how the fundamental information of how to create a table.

688 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