Solved

QUERY QUESTION

Posted on 2016-09-09
3
45 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 17

Expert Comment

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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
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.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
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.

705 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

16 Experts available now in Live!

Get 1:1 Help Now