Solved

SQL Select Query help

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

856 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