Solved

SQL Select Query help

Posted on 2016-11-14
3
50 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
3 Comments
 
LVL 39

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 40

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.

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