如何使用sql选择差异?
我编写了一个 SQL 查询来检索数据,如下所示:
SELECT (MAX (b.filledqty) - MAX (a.filledqty)) AS filledtoday
FROM clientordermas a,
clientordermas b
WHERE a.clordid = 'w9110126'
AND b.clordid = 'w9110126'
AND (SELECT max(a.price)
FROM clientordermas a
WHERE a.clordid = 'w9110126') < 1000;
对于给定的 clodid
,表中有 3 条记录,价格值为 800、900 1200。
所以,我需要的是得到1200和900之间的差,即300。
但是,上面的语句总是返回0。我应该得到的是 MAX (b.filledqty) 返回 1200
和 MAX (a.filledqty) 返回 900
。
但这并没有发生。这不是我面临的确切问题,而是它的简化版本。
有人可以帮忙吗?
I wrote a sql query to retrieve data as follows:
SELECT (MAX (b.filledqty) - MAX (a.filledqty)) AS filledtoday
FROM clientordermas a,
clientordermas b
WHERE a.clordid = 'w9110126'
AND b.clordid = 'w9110126'
AND (SELECT max(a.price)
FROM clientordermas a
WHERE a.clordid = 'w9110126') < 1000;
There are three records in the table for the given clordid
with price values 800, 900
1200.
So, what I need is to get the difference between 1200 and 900 which is 300.
But, the above statement always returns 0. What I should get isMAX (b.filledqty) retuns 1200
andMAX (a.filledqty) retuns 900
.
But it is not happening. This is not the exact problem I am facing but a simplified version of it.
Can someone please help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
假设 1000 是一个神奇的值,确实应该进行硬编码:
您可能希望将
ELSE NULL
替换为ELSE 0
,具体取决于您想要的结果(如果没有小于值)超过 1000。编辑意识到我部分地误读了这个问题,所以我答案的第二部分是不相关的。在适当的情况下将上述内容更改为参考
价格
。Assuming that 1000 is a magic value that should indeed be hardcoded:
You might want to replace
ELSE NULL
withELSE 0
, depending on what result you want if there is no value less than 1000.Edit Realized I had partially misread the question so the second part of my answer was irrelevant. Changed the above to reference
price
where appropriate.检查 max() < 的子句是什么业务规则? 1000在做什么?您要查找 1000 以上的最大值减去 1000 以下的最大值吗?
如果是这种情况,那么:
虽然您需要扩展业务规则,但我认为可以处理 clorid 的值不超过 1000 的情况,或者它的值不低于 1000 的情况。
What business rule is the clause checking for max() < 1000 doing? You are looking for the max value above 1000 minus the max value below 1000?
If that is the case then:
although you need to expand the business rules I think to handle the case where a clorid doesn't have a value over 1000, or it doesn't have a vlaue under 1000.
我猜你在 a 和 b 中有相同的记录。所以 1200 - 1200 的结果等于 0。
这对你来说有什么不同吗?
i'm guessing that you have the same records in a and b. So the result of 1200 - 1200 equals 0.
Does this do any difference for you ?
一种方法是(选择前 2 个不同的填充量之间的差异):
但是 Dave Costa 为您提供了一种在 Oracle 中效果更好的方法。
One way to do it (Select the difference between the Top 2 DISTINCT filledqties):
But Dave Costa gave you a method for this that works better in Oracle.
我认为你的 SQL 概念是错误的。
实际发生的情况是,您得到一个 3x3 = 9 行交叉联接表,其中包含重复的字段。
您的最后一个 where 子句是固定结果。所以这意味着
MAX(b.filledqty) - MAX(a.filledqty)
是在这个 9 行表上进行操作的。当然,两个最大值是相同的(即 1200)。结果为零。如果您想要找到最大值和最小值之间的差异,请尝试如下操作:
此查询首先执行内部自连接,复制必要的列,然后对一列升序排序,另一列降序排序。第一个结果组合将具有最大值和最小值。
如果您想查找最大值和第二个最大值之间的差异,请尝试以下操作:
结果集中的顶行包含最大值和第二个最大值 - 第二个最大值是取与最大值不同的最高值。
如果你想找到最大值和最高值之间的差值 < 1000,试试这个:
I think you got your SQL concepts wrong.
What actually happened is that you get a 3x3 = 9 row cross join table, with duplicated fields.
Your last where clause is a fixed result. So which means that
MAX(b.filledqty) - MAX(a.filledqty)
is operated on this 9-row table. Of course, both max values are the same (i.e. 1200). The result is zero.If what you want is to find the difference between the max value and the min value, try something like this:
This query first does an inner self join which duplicates the necessary column, then sort one column ascending and the other column descending. The first resulting combination will have the max and min values.
If you want to find the difference between the max value and the second-to-max value, try this:
The top row in the result set contains the max and second-to-max value -- the second-to-max value is taken as the top value that is not the same as the max value.
If you want to find the difference between the max value and the top value that is < 1000, try this: