Solved

SQL Select Query help

Posted on 2016-11-14
3
31 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 18

Accepted Solution

by:
Pawan Kumar Khowal 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
SQL Query Assistance 4 31
Record extraction 3 14
how to fix this error 14 48
Difference in number of minutes between 2 timestamps 16 26
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 …
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

759 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

24 Experts available now in Live!

Get 1:1 Help Now