如何在SQL Server 2008中选择每个组的最后一个id(其中id> 0) [英] How to select last id(where id >0) fod each group in SQL server 2008

查看:89
本文介绍了如何在SQL Server 2008中选择每个组的最后一个id(其中id> 0)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。我想选择我的表的价格字段的最后一条记录,其中我的字段值大于零。我怎么能这样做?

我的存储过程是:

Hi All.I want to select last record of price field of my table where my value of field is greater than zero. how can i do that?
my stored procedure is :

SELECT id, name, price
FROM messages
WHERE id IN (
    SELECT MAX(id)
    FROM messages
   WHERE  price > 0
    GROUP BY name









问题在于:

此代码选择max id,即大于零而不是最后一个id。表示选择id = 2和id = 6

但是组(frank)价格的最后一个ID是零,但这个strored程序选择id = 2而我想要存储过程只选择id = 6

id name price

1 frank 1000

2 frank 500

3 frank 0

4 john 200

5 john 100

6约翰20



我的尝试:



怎么样选择最后一个id(其中id> 0)fod每个gro在sql server 2008中





the problem is that :
this code select max id that price is greater than zero not last id . means select id=2 and id=6
but in last id of group (frank) price is zero but this strored procedure select id=2 while I want stored procedure select only id =6
id name price
1 frank 1000
2 frank 500
3 frank 0
4 john 200
5 john 100
6 john 20

What I have tried:

how to select last id(where id >0) fod each group in sql server 2008

推荐答案

last的问题在于它不准确:除非您通过ORDER BY语句(或类似)明确选择排序顺序SQL可以自由地以任何方便的方式返回行。通常情况下,这是先进先出,所以一个香草SELECT,如

The problem with "last" is that it's not precise: unless you specifically select a sort order via an ORDER BY statement (or similar) SQL is at liberty to return rows in any order it finds convenient. Normally, that works out as "first in, first out", so a vanilla SELECT such as
SELECT * FROM MyTable

将按照它们被插入的顺序返回行。

但是......它不是一定如此!为了确定你的最后行,你需要明确告诉SQL要订购什么。

所以......添加一个InsertDate或TimeStamp列,并在你INSERT(最简单的方法是提供一个DATETIME列并将它的DEFAULT设置为GETDATE() - 然后SQL将为你提供它而不需要你的外部代码更改)。

然后你有一个列可以使用GROUP BY子句来指定每个组中的最后一行。

will return rows in the order that they were INSERTED.
But...it's not necessarily so! In order to be certain of your "last" row, you need to tell SQL explicitly what to order by.
So...add a "InsertDate" or "TimeStamp" column, and set it when you INSERT (the easiest way is to provide a DATETIME column and set it's DEFAULT to GETDATE() - then SQL will supply it for you without your external code having to change).
Then you have a column that can work with the GROUP BY clause to specify the "last" row in each group.


如果我已经正确理解了你的问题,这样的事情应该有效:

If I've understood your question properly, something like this should work:
WITH cteMessages As
(
    SELECT
        id,
        name,
        price,
        ROW_NUMBER() OVER (PARTITION BY name ORDER BY id DESC) As RN
    FROM
        messages
)
SELECT 
    id, 
    name, 
    price
FROM
    cteMessages
WHERE
    RN = 1
And
    price > 0
;



这将找到 id最高的行为每个名称,然后只返回价格大于零的行。


That will find the row with the highest id for each name, and then only return those rows where the price is greater than zero.


这篇关于如何在SQL Server 2008中选择每个组的最后一个id(其中id> 0)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆