Solved

QUERY QUESTION

Posted on 2016-09-09
3
68 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
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 28

Expert Comment

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

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

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…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

809 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