Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Script Help

Author  Topic 

easy_goer
Starting Member

21 Posts

Posted - 2013-10-13 : 14:55:54

Hello. I have a portion of a table that looks like what I've listed below. This is just a small subset of the data that I'm attempting to retrieve, but this displays what the structure looks like. What I would like to do is have a script that can pull all but the top two lines. So, I want to get everything that's below the line that contains "E-Mail". Hope this makes sense.

Right now, I can pull lines 3 and 4 by using the script below, but I would like also to capture the last line in my results. Any ideas?

select Column1, Column2, Column3, Column4
from Table
where Column2 in
(select Column1 from Table
where Column1 = '22701')


Column1 Column2 Column3 Column4
8106 NULL Top 8106
22701 8111 Test2 8106
26892 22701 Extra1 8106
26893 22701 Extra2 8106
26894 26892 ExtraSub1 8106

Thank you!

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-10-13 : 20:27:20
quote:
Originally posted by easy_goer


Hello. I have a portion of a table that looks like what I've listed below. This is just a small subset of the data that I'm attempting to retrieve, but this displays what the structure looks like. What I would like to do is have a script that can pull all but the top two lines. So, I want to get everything that's below the line that contains "E-Mail". Hope this makes sense.

Right now, I can pull lines 3 and 4 by using the script below, but I would like also to capture the last line in my results. Any ideas?

select Column1, Column2, Column3, Column4
from Table
where Column2 in
(select Column1 from Table
where Column1 = '22701')


Column1 Column2 Column3 Column4
8106 NULL Top 8106
22701 8111 Test2 8106
26892 22701 Extra1 8106
26893 22701 Extra2 8106
26894 26892 ExtraSub1 8106

Thank you!

It is hard to deduce the rule that you want to use just from the example you posted. It could be any , or perhaps none of the following. Can you describe the rule that you want to use? I don't see a row with "e-mail" in the example data
select Column1, Column2, Column3, Column4
from Table
where Column1 > 26000;

select TOP (3) Column1, Column2, Column3, Column4
from Table
order by Column1 desc

select Column1, Column2, Column3, Column4
from Table
order by Column2 desc

select Column1, Column2, Column3, Column4
from Table
where Column3 not in ('Top','Test2');
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-10-14 : 01:52:01
looks like this to me

select Column1, Column2, Column3, Column4
from Table t
where Column1 <> Column4
and not exists (select 1
from Table where Column2 = t.Column1 and Column4 = t.Column4)


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

easy_goer
Starting Member

21 Posts

Posted - 2013-10-14 : 11:02:47
Hello. I apologize that my original post was a bit unclear. Based on the table in my original post, I am able to pull the following

26892 22701 Extra1 8106
26893 22701 Extra2 8106

with this script..

select Column1, Column2, Column3, Column4
from Table
where Column2 in
(select Column1 from Table
where Column1 = '22701')


However, I would like to come up with a script that will display the following results..

26892 22701 Extra1 8106
26893 22701 Extra2 8106
26894 26892 ExtraSub1 8106


Does this make more sense?
Go to Top of Page

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-10-14 : 12:19:21
quote:
Originally posted by easy_goer

Hello. I apologize that my original post was a bit unclear. Based on the table in my original post, I am able to pull the following

26892 22701 Extra1 8106
26893 22701 Extra2 8106

with this script..

select Column1, Column2, Column3, Column4
from Table
where Column2 in
(select Column1 from Table
where Column1 = '22701')


However, I would like to come up with a script that will display the following results..

26892 22701 Extra1 8106
26893 22701 Extra2 8106
26894 26892 ExtraSub1 8106


Does this make more sense?


That you want to get the last 3 rows was clear from your original post. What is not clear to me is the rule that you used to disqualify the first two rows, and qualify the remaining 3 rows.

Any of the examples I posted in my earlier post would eliminate the first two rows and give you the last 3 rows. Yet, I suspect none of them is what you are after. There is something else about the data that you are using to eliminate the first two rows. We need to program that rule - but first you have to describe what that rule is.
Go to Top of Page
   

- Advertisement -