Solved

How can I retrieve these rows?

Posted on 2014-04-29
13
207 Views
Last Modified: 2014-05-07
I have a table that looks like this:

sortorder    statusid   workqueueroleid
1                    NULL          NULL
2                    NULL          NULL
3                    NULL          NULL
4                    30                10
5                    30                10
6                    NULL          NULL
7                    NULL          NULL

What would my select statement look like if I wanted to retrieve all the rows above row #4. In other words, I don't want rows 5, 6 or 7. I only want the rows 1,2 and 3.

How would I do it?
0
Comment
Question by:brucegust
[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
  • 2
  • 2
  • 2
  • +6
13 Comments
 
LVL 66

Assisted Solution

by:Jim Horn
Jim Horn earned 105 total points
ID: 40030075
Use the WHERE clause to filter the rows returned:
SELECT sortorder, statusid, workqueueroleid
FROM your_table
WHERE sortorder <= 3

Open in new window

0
 
LVL 34

Assisted Solution

by:Brian Crowe
Brian Crowe earned 50 total points
ID: 40030086
What is your logic for defining the cut-off?  Is it the first non-null value? Is it just the sortorder value as Jim defines above?
0
 
LVL 22

Accepted Solution

by:
plusone3055 earned 99 total points
ID: 40030091
JH logic is correct. an alternative way would also be

SELECT    sortorder
              ,statusid
              ,workqueroleid
FROM      YOUR TABLE
WHERE    sortorder IN(1,2,3)

This alternative would be if you wanted SPECIFIC row numbers versus where JH would show everything less than or equal to 3

variety :)
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 22

Assisted Solution

by:plusone3055
plusone3055 earned 99 total points
ID: 40030094
his logic  
WHERE sortorder <= 3  

means  where the sortorder is less than or equal to 3

my logic  IN(1,2,3)

means when the sort order number is 1 or 2 or 3
0
 
LVL 31

Assisted Solution

by:Marco Gasi
Marco Gasi earned 49 total points
ID: 40030136
if you wanto only the first three records you can use LIMIT:

SELECT    sortorder ,statusid ,workqueroleid FROM  table LIMIT 3
0
 
LVL 32

Assisted Solution

by:awking00
awking00 earned 98 total points
ID: 40030266
If you're trying to retrieve all of the rows that contain null statusids and workqueueroleids that exist prior non-null values based onthe sort order, then

select sortorder, statusid, workqueueroleid from
(select sortorder, statusid, workqueueroleid,
 row_number() over (order by statusid desc, workqueueroleid desc, sortorder) rn
 from table1) as x
where rn = sortorder;
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 40030284
@MarqusG: I think LIMIT is a MySQL construct, not MS SQL.  But that aside, if we know the data criteria we can help craft a WHERE clause.  I just have this nagging feeling that the question has some subtext that we cannot find in the literal text.
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 40030298
Oh, I didn't make attention to the topic! But I should... :)
0
 
LVL 66

Assisted Solution

by:Jim Horn
Jim Horn earned 105 total points
ID: 40030302
brucegust - As you can tell, we have multiple excellent answers for you.  It would help if you could confirm for us that they work, and if not give us some more details.
0
 
LVL 32

Assisted Solution

by:awking00
awking00 earned 98 total points
ID: 40030369
brucegust, what would you want to retrieve if the rows were like this?
sortorder    statusid   workqueueroleid
1                    NULL          NULL
2                    NULL          NULL
3                    NULL          NULL
4                    NULL          NULL
5                    30                10
6                    30                10
7                    NULL          NULL
0
 
LVL 69

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 49 total points
ID: 40030659
SELECT *
FROM dbo.tablename
WHERE
    sortorder < (SELECT MIN(sortorder) FROM dbo.tablename WHERE statusid IS NOT NULL)
0
 
LVL 15

Assisted Solution

by:Vikas Garg
Vikas Garg earned 50 total points
ID: 40031437
Hi,

You can try this using top and orderby

SELECT top 3 sortorder, statusid, workqueueroleid
FROM your_table
WHERE statusid is null and workqueueroleid is null
order by sortorder
0
 

Author Comment

by:brucegust
ID: 40047673
Guys, as always, thanks for the assistance. I apologize for not staying tuned in. I wound up going with a different approach that involved a nested table and it worked great.

I'll distribute points based on the obvious time and trouble you took to help. Thanks again!
0

Featured Post

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to count occurrences of each item in an array.
Viewers will learn how the fundamental information of how to create a table.

623 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