Solved

SQL Select Query help

Posted on 2016-11-14
3
59 Views
Last Modified: 2016-11-16
Hello EE,

I have a select Query that, when a user search for a range of numbers lets say :

FROM ID 1 to ID 15

right now in my query, if ID 9 and 10 does not exist at all, in my result I will show only the IDs are obviously exists like this

1
2
3
4
5
6
7
8
11
12

what I would like, is that, I would like to show 9 and 10 with a note "DELETED"  so that the user knows why its not there anymore.


my query starts with SELECT * from myMainIDTable m and some other inner join / left join for other informaiton
and in my WHERE clause I do  WHERE m.ID >= txtFrom.Value and m.ID <= txtTO.Value

so if the ID is not in M, it will not show, but I would like to show with a note.

how can I do that ?
0
Comment
Question by:PhilippeRenaud
[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 40

Expert Comment

by:lcohan
ID: 41886725
You will need to use a LEFT JOIN clause and CASE where that missing row IS NULL to "Deleted".
0
 
LVL 41

Expert Comment

by:Sharath
ID: 41886766
Generate a number table of whatever size you want and then LEFT JOIN your query to that table.
with   cte0 as (select 1 as c union all select 1),       
       cte1 as (select 1 as c from cte0 a, cte0 b),       
       cte2 as (select 1 as c from cte1 a, cte1 b),
	   cte3 as (select 1 as c from cte2 a, cte2 b),
	   cte4 as (select 1 as c from cte3 a, cte3 b), 
	   nums as (select row_number() over (order by c) as n from cte4) 
select t1.n, t2.*
  from nums t1
  left join (SELECT * from myMainIDTable m and some other inner join / left join for other informaiton
and in my WHERE clause I do  WHERE m.ID >= txtFrom.Value and m.ID <= txtTO.Value) t2
where t1.n >= Value and t1.n <= Value

Open in new window

1
 
LVL 28

Accepted Solution

by:
Pawan Kumar earned 500 total points
ID: 41887153
Try..

CREATE TABLE SeqVal (VALUE INT not null CONSTRAINT PK_Val PRIMARY KEY);
go

INSERT INTO SeqVal VALUES(1);
INSERT INTO SeqVal VALUES(2);
INSERT INTO SeqVal VALUES(3);
INSERT INTO SeqVal VALUES(11);
INSERT INTO SeqVal VALUES(12);
INSERT INTO SeqVal VALUES(4)
INSERT INTO SeqVal VALUES(5);
INSERT INTO SeqVal VALUES(6);
INSERT INTO SeqVal VALUES(7);
INSERT INTO SeqVal VALUES(8);
GO

;WITH CTE AS
(
	SELECT v1 , v2  FROM 
	(
		SELECT Value+1 v1 ,LEAD(Value) OVER(ORDER BY Value)-1 v2
		FROM SeqVal
	)t
	WHERE v2>=v1
)
SELECT v1 , 'Deleted' Statex FROM CTE UNION SELECT v2 , 'Deleted' Statex  FROM CTE

Open in new window


O/p
-----------------------
v1      Statex
9      Deleted
10      Deleted


try this ---


;WITH CTE AS
(
	SELECT v1 , v2  FROM 
	(
		SELECT Value+1 v1 ,LEAD(Value) OVER(ORDER BY Value)-1 v2
		FROM (SELECT * from myMainIDTable m and some other inner join / left join for other informaiton
			and in my WHERE clause I do  WHERE m.ID >= txtFrom.Value and m.ID <= txtTO.Value) SeqVal
	)t
	WHERE v2>=v1
)
SELECT v1 , 'Deleted' Statex FROM CTE UNION SELECT v2 , 'Deleted' Statex  FROM CTE

Open in new window


Hope it helps!
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

Suggested Solutions

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

756 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