Solved

QUERY QUESTION

Posted on 2016-09-09
3
57 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 25

Expert Comment

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

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

863 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now