Showing posts with label opposite. Show all posts
Showing posts with label opposite. Show all posts

Friday, March 9, 2012

opposite query

Hi,
Need some help with a query, this is what it looks like:
SELECT TOP 10 *
FROM [dbo].[R_Reenval]
Now how do i get the bottom/last 10 records? I tried replacing the
"Top" with "Bottom", "Last"; they both dnt work.
Any help would be highly appreciated. Thanx
TOP doesn't give you the "first" rows as such concept doesn't exist. Rows are not ordered. But since
TOP is applied after ORDER BY, you can do something like:
SELECT TOP 10 col1, col2
FROM tbl
ORDER BY col
To get "bottom" based on the same column order, you can do:
SELECT TOP 10 col1, col2
FROM tbl
ORDER BY col DESC
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"amatuer" <njoosub@.gmail.com> wrote in message
news:1160549556.997163.269380@.b28g2000cwb.googlegr oups.com...
> Hi,
> Need some help with a query, this is what it looks like:
> SELECT TOP 10 *
> FROM [dbo].[R_Reenval]
> Now how do i get the bottom/last 10 records? I tried replacing the
> "Top" with "Bottom", "Last"; they both dnt work.
> Any help would be highly appreciated. Thanx
>
|||Do a reverse sort.
SELECT TOP n
{ColumnList}
FROM MyTable
ORDER BY {SortCriteria}
Arnie Rowland, Ph.D.
Westwood Consulting, Inc
Most good judgment comes from experience.
Most experience comes from bad judgment.
- Anonymous
"amatuer" <njoosub@.gmail.com> wrote in message
news:1160549556.997163.269380@.b28g2000cwb.googlegr oups.com...
> Hi,
> Need some help with a query, this is what it looks like:
> SELECT TOP 10 *
> FROM [dbo].[R_Reenval]
> Now how do i get the bottom/last 10 records? I tried replacing the
> "Top" with "Bottom", "Last"; they both dnt work.
> Any help would be highly appreciated. Thanx
>
|||Thanx alot
Tibor Karaszi wrote:[vbcol=seagreen]
> TOP doesn't give you the "first" rows as such concept doesn't exist. Rows are not ordered. But since
> TOP is applied after ORDER BY, you can do something like:
> SELECT TOP 10 col1, col2
> FROM tbl
> ORDER BY col
> To get "bottom" based on the same column order, you can do:
> SELECT TOP 10 col1, col2
> FROM tbl
> ORDER BY col DESC
> --
> Tibor Karaszi, SQL Server MVP
> http://www.karaszi.com/sqlserver/default.asp
> http://www.solidqualitylearning.com/
>
> "amatuer" <njoosub@.gmail.com> wrote in message
> news:1160549556.997163.269380@.b28g2000cwb.googlegr oups.com...

opposite query

Hi,
Need some help with a query, this is what it looks like:
SELECT TOP 10 *
FROM [dbo].[R_Reenval]
Now how do i get the bottom/last 10 records? I tried replacing the
"Top" with "Bottom", "Last"; they both dnt work.
Any help would be highly appreciated. ThanxTOP doesn't give you the "first" rows as such concept doesn't exist. Rows are not ordered. But since
TOP is applied after ORDER BY, you can do something like:
SELECT TOP 10 col1, col2
FROM tbl
ORDER BY col
To get "bottom" based on the same column order, you can do:
SELECT TOP 10 col1, col2
FROM tbl
ORDER BY col DESC
--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"amatuer" <njoosub@.gmail.com> wrote in message
news:1160549556.997163.269380@.b28g2000cwb.googlegroups.com...
> Hi,
> Need some help with a query, this is what it looks like:
> SELECT TOP 10 *
> FROM [dbo].[R_Reenval]
> Now how do i get the bottom/last 10 records? I tried replacing the
> "Top" with "Bottom", "Last"; they both dnt work.
> Any help would be highly appreciated. Thanx
>|||Thanx alot :)
Tibor Karaszi wrote:
> TOP doesn't give you the "first" rows as such concept doesn't exist. Rows are not ordered. But since
> TOP is applied after ORDER BY, you can do something like:
> SELECT TOP 10 col1, col2
> FROM tbl
> ORDER BY col
> To get "bottom" based on the same column order, you can do:
> SELECT TOP 10 col1, col2
> FROM tbl
> ORDER BY col DESC
> --
> Tibor Karaszi, SQL Server MVP
> http://www.karaszi.com/sqlserver/default.asp
> http://www.solidqualitylearning.com/
>
> "amatuer" <njoosub@.gmail.com> wrote in message
> news:1160549556.997163.269380@.b28g2000cwb.googlegroups.com...
> > Hi,
> > Need some help with a query, this is what it looks like:
> >
> > SELECT TOP 10 *
> > FROM [dbo].[R_Reenval]
> >
> > Now how do i get the bottom/last 10 records? I tried replacing the
> > "Top" with "Bottom", "Last"; they both dnt work.
> >
> > Any help would be highly appreciated. Thanx
> >|||Do a reverse sort.
SELECT TOP n
{ColumnList}
FROM MyTable
ORDER BY {SortCriteria}
--
Arnie Rowland, Ph.D.
Westwood Consulting, Inc
Most good judgment comes from experience.
Most experience comes from bad judgment.
- Anonymous
"amatuer" <njoosub@.gmail.com> wrote in message
news:1160549556.997163.269380@.b28g2000cwb.googlegroups.com...
> Hi,
> Need some help with a query, this is what it looks like:
> SELECT TOP 10 *
> FROM [dbo].[R_Reenval]
> Now how do i get the bottom/last 10 records? I tried replacing the
> "Top" with "Bottom", "Last"; they both dnt work.
> Any help would be highly appreciated. Thanx
>

opposite query

Hi,
Need some help with a query, this is what it looks like:
SELECT TOP 10 *
FROM [dbo].[R_Reenval]
Now how do i get the bottom/last 10 records? I tried replacing the
"Top" with "Bottom", "Last"; they both dnt work.
Any help would be highly appreciated. ThanxTOP doesn't give you the "first" rows as such concept doesn't exist. Rows ar
e not ordered. But since
TOP is applied after ORDER BY, you can do something like:
SELECT TOP 10 col1, col2
FROM tbl
ORDER BY col
To get "bottom" based on the same column order, you can do:
SELECT TOP 10 col1, col2
FROM tbl
ORDER BY col DESC
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"amatuer" <njoosub@.gmail.com> wrote in message
news:1160549556.997163.269380@.b28g2000cwb.googlegroups.com...
> Hi,
> Need some help with a query, this is what it looks like:
> SELECT TOP 10 *
> FROM [dbo].[R_Reenval]
> Now how do i get the bottom/last 10 records? I tried replacing the
> "Top" with "Bottom", "Last"; they both dnt work.
> Any help would be highly appreciated. Thanx
>|||Do a reverse sort.
SELECT TOP n
{ColumnList}
FROM MyTable
ORDER BY {SortCriteria}
Arnie Rowland, Ph.D.
Westwood Consulting, Inc
Most good judgment comes from experience.
Most experience comes from bad judgment.
- Anonymous
"amatuer" <njoosub@.gmail.com> wrote in message
news:1160549556.997163.269380@.b28g2000cwb.googlegroups.com...
> Hi,
> Need some help with a query, this is what it looks like:
> SELECT TOP 10 *
> FROM [dbo].[R_Reenval]
> Now how do i get the bottom/last 10 records? I tried replacing the
> "Top" with "Bottom", "Last"; they both dnt work.
> Any help would be highly appreciated. Thanx
>|||Thanx alot
Tibor Karaszi wrote:[vbcol=seagreen]
> TOP doesn't give you the "first" rows as such concept doesn't exist. Rows
are not ordered. But since
> TOP is applied after ORDER BY, you can do something like:
> SELECT TOP 10 col1, col2
> FROM tbl
> ORDER BY col
> To get "bottom" based on the same column order, you can do:
> SELECT TOP 10 col1, col2
> FROM tbl
> ORDER BY col DESC
> --
> Tibor Karaszi, SQL Server MVP
> http://www.karaszi.com/sqlserver/default.asp
> http://www.solidqualitylearning.com/
>
> "amatuer" <njoosub@.gmail.com> wrote in message
> news:1160549556.997163.269380@.b28g2000cwb.googlegroups.com...

Opposite of union - Stupid question for a common problem

Hi there!.
I'm having a simple problem. I have a table with like 20 fields , this table
is always growing since it's a price table acumulator for a set of products
(with their features) , this is loaded from a text file.
When a new text file arrives , I upload it to a temp table prior to import
i to the real table.
What I need to do is to only insert in the main table only the NEW fields,
so basically any record that differs in any of the 20 fields from the one
inside the database.
It's a stupid thing when you think of it, but I cannot seem to find the solu
tion.
Any help will be appreciated!!!
VictorSorry.
Please provide DDL and sample data.
http://www.aspfaq.com/etiquette.asp?id=5006
AMB
"Victor Daicich" wrote:

> Hi there!.
> I'm having a simple problem. I have a table with like 20 fields , this tab
le
> is always growing since it's a price table acumulator for a set of product
s
> (with their features) , this is loaded from a text file.
> When a new text file arrives , I upload it to a temp table prior to import
> i to the real table.
> What I need to do is to only insert in the main table only the NEW fields,
> so basically any record that differs in any of the 20 fields from the one
> inside the database.
> It's a stupid thing when you think of it, but I cannot seem to find the so
lution.
> Any help will be appreciated!!!
> Victor
>
>|||As Alejandro said, the shortest path to a solution is to provide DDL, sample
data and expected results. Based on what you have given though, it sounds
like an INSERT INTO with a SELECT statement with a NOT EXISTS and one
*HUMONGOUS* WHERE clause. It's just a big WHERE clause with about 20 AND's
in it. Here's a sample that selects only the unique rows from table #b
(that don't currently exist in #a). I cut it down to just two columns, but
you can expand the SELECT subquery in the NOT EXISTS predicate to include as
many columns as you like:
CREATE TABLE #a (color1 VARCHAR(16),
color2 VARCHAR(16))
CREATE TABLE #b (color1 VARCHAR(16),
color2 VARCHAR(16))
INSERT INTO #a (color1, color2)
SELECT 'blue', 'red'
UNION SELECT 'red', 'green'
UNION SELECT 'black', 'yellow'
UNION SELECT 'black', 'blue'
INSERT INTO #b (color1, color2)
SELECT 'blue', 'green'
UNION SELECT 'black', 'yellow'
UNION SELECT 'yellow', 'purple'
UNION SELECT 'black', 'blue'
SELECT b.*
FROM #b b
WHERE NOT EXISTS
(
SELECT 1
FROM #a a
WHERE a.color1 = b.color1
AND a.color2 = b.color2
)
DROP TABLE #a
DROP TABLE #b
"Victor Daicich" <victordaicich@.hotmail.com> wrote in message
news:12613abe6ea18c85d1b76245e10@.msnews.microsoft.com...
> Hi there!.
> I'm having a simple problem. I have a table with like 20 fields , this
> table is always growing since it's a price table acumulator for a set of
> products (with their features) , this is loaded from a text file.
> When a new text file arrives , I upload it to a temp table prior to import
> i to the real table.
> What I need to do is to only insert in the main table only the NEW fields,
> so basically any record that differs in any of the 20 fields from the one
> inside the database.
> It's a stupid thing when you think of it, but I cannot seem to find the
> solution.
> Any help will be appreciated!!!
> Victor
>|||Hello Mike C#,
That's right Mike. It's like you said, a big where clause is what I need
then.
Thanks guys you've been very helpful with this issue. I can go on now and
finish it.
Thanks again,
Victor
> As Alejandro said, the shortest path to a solution is to provide DDL,
> sample data and expected results. Based on what you have given
> though, it sounds like an INSERT INTO with a SELECT statement with a
> NOT EXISTS and one *HUMONGOUS* WHERE clause. It's just a big WHERE
> clause with about 20 AND's in it. Here's a sample that selects only
> the unique rows from table #b (that don't currently exist in #a). I
> cut it down to just two columns, but you can expand the SELECT
> subquery in the NOT EXISTS predicate to include as many columns as you
> like:
> CREATE TABLE #a (color1 VARCHAR(16),
> color2 VARCHAR(16))
> CREATE TABLE #b (color1 VARCHAR(16),
> color2 VARCHAR(16))
> INSERT INTO #a (color1, color2)
> SELECT 'blue', 'red'
> UNION SELECT 'red', 'green'
> UNION SELECT 'black', 'yellow'
> UNION SELECT 'black', 'blue'
> INSERT INTO #b (color1, color2)
> SELECT 'blue', 'green'
> UNION SELECT 'black', 'yellow'
> UNION SELECT 'yellow', 'purple'
> UNION SELECT 'black', 'blue'
> SELECT b.*
> FROM #b b
> WHERE NOT EXISTS
> (
> SELECT 1
> FROM #a a
> WHERE a.color1 = b.color1
> AND a.color2 = b.color2
> )
> DROP TABLE #a
> DROP TABLE #b
> "Victor Daicich" <victordaicich@.hotmail.com> wrote in message
> news:12613abe6ea18c85d1b76245e10@.msnews.microsoft.com...
>|||If you are using 2005, then you have the "except" operator.
select... from A
except
Select ... from B
-Omnibuzz (The SQL GC)
http://omnibuzz-sql.blogspot.com/
"Victor Daicich" wrote:

> Hello Mike C#,
> That's right Mike. It's like you said, a big where clause is what I need
> then.
> Thanks guys you've been very helpful with this issue. I can go on now and
> finish it.
> Thanks again,
> Victor
>
>
>|||Good point, I assumed SQL 2000 (I always do when the OP doesn't mention the
platform) :)
"Omnibuzz" <Omnibuzz@.discussions.microsoft.com> wrote in message
news:EFA7D756-F8CA-4F4B-A614-81A78981ADEA@.microsoft.com...
> If you are using 2005, then you have the "except" operator.
> select... from A
> except
> Select ... from B
>
> --
> -Omnibuzz (The SQL GC)
> http://omnibuzz-sql.blogspot.com/
>
> "Victor Daicich" wrote:
>|||Oracle provides INTERSECT and MINUS operators (9i, maybe earlier).
SQL Server 2005 provides INTERSECT and EXCEPT.
Is there an ANSII SQL equivilant that anyone is aware of?
"Mike C#" <xyz@.xyz.com> wrote in message
news:OeBlwT$jGHA.4660@.TK2MSFTNGP05.phx.gbl...
> Good point, I assumed SQL 2000 (I always do when the OP doesn't mention
the
> platform) :)
> "Omnibuzz" <Omnibuzz@.discussions.microsoft.com> wrote in message
> news:EFA7D756-F8CA-4F4B-A614-81A78981ADEA@.microsoft.com...
need
and
you
>|||"Jim Underwood" <james.underwoodATfallonclinic.com> wrote in message
news:eUyihMJkGHA.3512@.TK2MSFTNGP03.phx.gbl...
> Oracle provides INTERSECT and MINUS operators (9i, maybe earlier).
> SQL Server 2005 provides INTERSECT and EXCEPT.
> Is there an ANSII SQL equivilant that anyone is aware of?
ANSI SQL:1999 defines INTERSECT and EXCEPT. Unfortunately SQL 2000 is only
compliant up to ANSI SQL:1992.|||INTERSECT, UNION and EXCEPT are in ANSI SQL. MINUS is not.
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"Jim Underwood" <james.underwoodATfallonclinic.com> wrote in message
news:eUyihMJkGHA.3512@.TK2MSFTNGP03.phx.gbl...
> Oracle provides INTERSECT and MINUS operators (9i, maybe earlier).
> SQL Server 2005 provides INTERSECT and EXCEPT.
> Is there an ANSII SQL equivilant that anyone is aware of?
>|||Good to know. Thanks.
"Tibor Karaszi" <tibor_please.no.email_karaszi@.hotmail.nomail.com> wrote in
message news:exwUSUJkGHA.3844@.TK2MSFTNGP02.phx.gbl...
> INTERSECT, UNION and EXCEPT are in ANSI SQL. MINUS is not.
> --
> Tibor Karaszi, SQL Server MVP
> http://www.karaszi.com/sqlserver/default.asp
> http://www.solidqualitylearning.com/
>
> "Jim Underwood" <james.underwoodATfallonclinic.com> wrote in message
> news:eUyihMJkGHA.3512@.TK2MSFTNGP03.phx.gbl...

Opposite of TopN

Is there a way to get "the rest" of a list? I have a TopN filter
working on one table showing the top 3 results. Now, I have another
table that I want to show everything but the top 3. This table will be
hidden by default, but that shouldn't matter. Is there a way to get
"not TopN =3"?
Also, how would I get the first 3, instead of top 3? My results would
be pre-sorted coming from the datasource and I just want to show the
first few rows.
ThanksAre you using a stored procedure? You could use a correlated subquery. For
example: select bookid from books where bookid not in (select top 3 bookid
from books).
"Steven" wrote:
> Is there a way to get "the rest" of a list? I have a TopN filter
> working on one table showing the top 3 results. Now, I have another
> table that I want to show everything but the top 3. This table will be
> hidden by default, but that shouldn't matter. Is there a way to get
> "not TopN =3"?
> Also, how would I get the first 3, instead of top 3? My results would
> be pre-sorted coming from the datasource and I just want to show the
> first few rows.
> Thanks
>

Opposite of TOP

Is there an opposite of TOP in sqlsvr? Only thing I can think to do is sort the opposite direction and use TOP but I have a big dynamically generated order by clause that I'd need to parse to reverse the sorting.

I wrote it in C# (http://www.dbforums.com/showthread.php?postid=3579565#post3579565) but I'm thinking now I need it in T-SQL.

Does anyone have the code for this or a better way of handling it? TIAYou already go the half way,
I think you can do this in T-SQL and let SQL reverse the sorting for you

select * from (
select top 10 * from T order by f1 desc, f2 desc, f3 desc
) a
order by f1, f2, f3|||Do you mean something like:

select top x field from table order by field desc|||Originally posted by rnealejr
Do you mean something like:

select top x field from table order by field desc
Yes but I don't know the OrderBy field list ahead of time (since its passed in as a parameter) so I was looking for some tsql code to parse it and add the "DESC" or "ASC"|||When you say tsql do you mean you want to make it a stored procedure - otherwise you can dynamically create the statement and change it based on the parameter (which you can do in a stored procedure as well).|||Originally posted by rnealejr
When you say tsql do you mean you want to make it a stored procedure - otherwise you can dynamically create the statement and change it based on the parameter (which you can do in a stored procedure as well).
Yes, a stored proc or maybe a function which I can use like this...

SELECT @.REVsql = ReverseOrderBy(@.pSQL)

...and the ReverseOrderBy function is what I'd like to know how to write.|||declare @.orderby varchar(100)
declare @.ind int

select @.orderby = 'f1 asc, f2 desc, f3 asc'

select @.orderby = replace(@.orderby, ' asc', ' asc1')
select @.orderby = replace(@.orderby, ' desc', ' asc')
select @.orderby = replace(@.orderby, ' asc1', ' desc')

select @.orderby

will return "f1 desc, f2 asc, f3 desc"|||I wrote the function for you, it should work.

create function ReverseOrderBy (@.orderby varchar(200))
returns varchar(200)
as
begin
declare @.reverse_orderby varchar(100)
declare @.sort_item varchar(100)

declare @.ind_start int
declare @.ind int

select @.ind_start = 1
select @.reverse_orderby = ''
select @.orderby = @.orderby + ','
while (1=1)
begin
select @.ind = charindex(',', @.orderby, @.ind_start)

if @.ind = 0
begin
break
end

select @.sort_item = substring(@.orderby, @.ind_start, @.ind - @.ind_start)

if charindex(' asc', @.sort_item) > 0
begin
select @.sort_item = replace(@.sort_item, ' asc', ' desc')
end
else
begin
if charindex(' desc', @.sort_item) > 0
begin
select @.sort_item = replace(@.sort_item, ' desc', '')
end
else
begin
select @.sort_item = @.sort_item + ' desc'
end
end

select @.reverse_orderby = @.reverse_orderby + @.sort_item + ', '
select @.ind_start = @.ind + 1

end
select @.reverse_orderby = substring(@.reverse_orderby, 1, len(@.reverse_orderby)-1)

return( @.reverse_orderby)
end

usage:
declare @.orderby varchar(100)
select @.orderby = 'f1, f2 desc, f3 asc, f4, f5, d6 desc'
select dbo.ReverseOrderBy(@.orderby)

will return

f1 desc, f2 , f3 desc, f4 desc, f5 desc, d6|||Ahh, that is cool. Thanks!

opposite of SELECT TOP?

Is there an opposite of the TOP keyword in a select statement?

I have the following query which works fine but it's not in the order I need because I have to use DESC to get the last 40 records.

select top 40 slabnumber, scarfcode, left(productname,6) as heatid, strandid, heatcutnumber, creationtime
from product
where scarfcode = 'V3'
order by creationtime desc

I need the returned records ordered by creationtime but not descending.

RandyDoes this work

select * from
(
select top 40 slabnumber, scarfcode, left(productname,6) as heatid, strandid, heatcutnumber, creationtime
from product
where scarfcode = 'V3'
order by creationtime desc
)
order by creationtime asc|||Have you tried:

select slabnumber, scarfcode, left(productname,6) as heatid, strandid, heatcutnumber, creationtime
from (select top 40 slabnumber, scarfcode, left(productname,6) as heatid, strandid, heatcutnumber, creationtime
from product
where scarfcode = 'V3'
order by creationtime desc) a
order by creationtime|||and.......what's wrong with that?|||Dang it all. Sniped. Why did I go back and replace "select *"? But, Silas missed one important piece. I will leave it to the interested reader to spot it.|||select * from
(
select top 40 slabnumber, scarfcode, left(productname,6) as heatid, strandid, heatcutnumber, creationtime
from product
where scarfcode = 'V3'
order by creationtime desc
)
order by creationtime asc

Tried this and get:

Server: Msg 156, Level 15, State 1, Line 6
Incorrect syntax near the keyword 'order'.

I probably should mention this is an old machine I'm running the query against, it's running 7.0. I'm thinking I may just have to do two queries... One to create a temporary table and one to pull the data out of the temporary table in the right order.

Randy|||or you could use Matt's

In any case, you're missing a name for the derived table|||I probably should mention this is an old machine I'm running the query against, it's running 7.0. I'm thinking I may just have to do two queries... One to create a temporary table and one to pull the data out of the temporary table in the right order.As Brett pointed out, just use the code provided by MCrowley. It will work in MS-SQL 7.0 (and subsequent releases).

-PatP|||mcrowley's won't work

the outer SELECT includes the expression left(productname,6)

the derived table has no such column

:)

fix this small error and it will work fine|||Thanks guys! Working great now!

Randy|||Yep. I cutted and pasted. Did not even notice the left function in there. The poster is happy, though.

opposite of "select top" > "select last"

hi all,

i can select the first three records, by using the "select top 3 ....."

but what if i want the three last records?

i know i can workaround it by first doing an ORDER BY DESC and then doing the TOP command.... but isn't there another (easier) way?

thanks

Select TOP is the only method (easy method too). It’s all depends on your sorting only. Having a DESC & TOP is not bad idea to get the last/bottom n records.

In SQL Server 2005, we can use the ROW_NUMBER function to achieve this.

(Alternate for TOP).

Here the sample,

Code Snippet

;With CTE

as

(

Select

Name

,ROW_NUMBER() OVER (Order BY name) as ForTop

,ROW_NUMBER() OVER (Order BY name Desc) as ForBottom

from

Sysobjects

)

Select Name From CTE Where ForBottom <=3 --ForTop<=3