如何使用sql选择差异?

发布于 2024-10-21 21:40:08 字数 607 浏览 3 评论 0原文

我编写了一个 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) 返回 1200MAX (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 is
MAX (b.filledqty) retuns 1200
and
MAX (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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

请持续率性 2024-10-28 21:40:08

假设 1000 是一个神奇的值,确实应该进行硬编码:

SELECT MAX(filledqty) - MAX(CASE WHEN price < 1000 THEN filledqty ELSE NULL END)
  FROM clientordermas
  WHERE clordid='w9110126';

您可能希望将 ELSE NULL 替换为 ELSE 0,具体取决于您想要的结果(如果没有小于值)超过 1000。

编辑意识到我部分地误读了这个问题,所以我答案的第二部分是不相关的。在适当的情况下将上述内容更改为参考价格

Assuming that 1000 is a magic value that should indeed be hardcoded:

SELECT MAX(filledqty) - MAX(CASE WHEN price < 1000 THEN filledqty ELSE NULL END)
  FROM clientordermas
  WHERE clordid='w9110126';

You might want to replace ELSE NULL with ELSE 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.

梦毁影碎の 2024-10-28 21:40:08

检查 max() < 的子句是什么业务规则? 1000在做什么?您要查找 1000 以上的最大值减去 1000 以下的最大值吗?

如果是这种情况,那么:

select a.price - b.price
from (select clorid, max(price) as price from  clientordermas a WHERE a.clordid = 'w9110126' and price > 1000) a,
join (select clorid, max(price) as price from  clientordermas a WHERE a.clordid = 'w9110126' and price < 1000) b
on a.clorid = b.clorid;

虽然您需要扩展业务规则,但我认为可以处理 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:

select a.price - b.price
from (select clorid, max(price) as price from  clientordermas a WHERE a.clordid = 'w9110126' and price > 1000) a,
join (select clorid, max(price) as price from  clientordermas a WHERE a.clordid = 'w9110126' and price < 1000) b
on a.clorid = b.clorid;

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.

回忆躺在深渊里 2024-10-28 21:40:08

我猜你在 a 和 b 中有相同的记录。所以 1200 - 1200 的结果等于 0。

SELECT (MAX (b.filledqty) - MAX (a.filledqty)) AS filledtoday   
FROM clientordermas a, 
     clientordermas b  
WHERE a.clordid = 'w9110126'    
  AND b.clordid = 'w9110126'    
  AND a.price < 1000;

这对你来说有什么不同吗?

i'm guessing that you have the same records in a and b. So the result of 1200 - 1200 equals 0.

SELECT (MAX (b.filledqty) - MAX (a.filledqty)) AS filledtoday   
FROM clientordermas a, 
     clientordermas b  
WHERE a.clordid = 'w9110126'    
  AND b.clordid = 'w9110126'    
  AND a.price < 1000;

Does this do any difference for you ?

冷血 2024-10-28 21:40:08

一种方法是(选择前 2 个不同的填充量之间的差异):

SELECT ( ( SELECT max(c.filledqty) 
             FROM clientordermas c 
             WHERE c.clordid = 'w9110126'
         )
         - MAX (a.filledqty)
       ) AS filledtoday   
  FROM clientordermas a 
  WHERE a.clordid = 'w9110126'    
    AND a.filledqty <
        ( SELECT max(b.filledqty) 
            FROM clientordermas b 
            WHERE b.filledqty= 'w9110126'
        )
;

但是 Dave Costa 为您提供了一种在 Oracle 中效果更好的方法。

One way to do it (Select the difference between the Top 2 DISTINCT filledqties):

SELECT ( ( SELECT max(c.filledqty) 
             FROM clientordermas c 
             WHERE c.clordid = 'w9110126'
         )
         - MAX (a.filledqty)
       ) AS filledtoday   
  FROM clientordermas a 
  WHERE a.clordid = 'w9110126'    
    AND a.filledqty <
        ( SELECT max(b.filledqty) 
            FROM clientordermas b 
            WHERE b.filledqty= 'w9110126'
        )
;

But Dave Costa gave you a method for this that works better in Oracle.

你丑哭了我 2024-10-28 21:40:08

我认为你的 SQL 概念是错误的。

实际发生的情况是,您得到一个 3x3 = 9 行交叉联接表,其中包含重复的字段。

您的最后一个 where 子句是固定结果。所以这意味着 MAX(b.filledqty) - MAX(a.filledqty) 是在这个 9 行表上进行操作的。当然,两个最大值是相同的(即 1200)。结果为零。

如果您想要找到最大值和最小值之间的差异,请尝试如下操作:

SELECT TOP 1 (b.filledqty - a.filledqty) AS filledtoday
FROM clientordermas a INNER JOIN clientordermas b ON (a.clordid=b.clordid)
WHERE a.clordid = '...'
ORDER BY a.filledqty, b.filledqty DESC

此查询首先执行内部自连接,复制必要的列,然后对一列升序排序,另一列降序排序。第一个结果组合将具有最大值和最小值。

如果您想查找最大值和第二个最大值之间的差异,请尝试以下操作:

SELECT TOP 1 (b.filledqty - a.filledqty) AS filledtoday
FROM clientordermas a INNER JOIN clientordermas b ON (a.clordid=b.clordid)
WHERE a.filledqty <> b.filledqty    (*this filters out max-max combo*)
WHERE a.clordid = '...'
ORDER BY a.filledqty DESC, b.filledqty DESC

结果集中的顶行包含最大值和第二个最大值 - 第二个最大值是取与最大值不同的最高值。

如果你想找到最大值和最高值之间的差值 < 1000,试试这个:

SELECT TOP 1 (b.filledqty - a.filledqty) AS filledtoday
FROM clientordermas a INNER JOIN clientordermas b ON (a.clordid=b.clordid)
WHERE a.filledqty <> b.filledqty AND b.filledqty < 1000
WHERE a.clordid = '...'
ORDER BY a.filledqty DESC, b.filledqty DESC

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:

SELECT TOP 1 (b.filledqty - a.filledqty) AS filledtoday
FROM clientordermas a INNER JOIN clientordermas b ON (a.clordid=b.clordid)
WHERE a.clordid = '...'
ORDER BY a.filledqty, b.filledqty DESC

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:

SELECT TOP 1 (b.filledqty - a.filledqty) AS filledtoday
FROM clientordermas a INNER JOIN clientordermas b ON (a.clordid=b.clordid)
WHERE a.filledqty <> b.filledqty    (*this filters out max-max combo*)
WHERE a.clordid = '...'
ORDER BY a.filledqty DESC, b.filledqty DESC

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:

SELECT TOP 1 (b.filledqty - a.filledqty) AS filledtoday
FROM clientordermas a INNER JOIN clientordermas b ON (a.clordid=b.clordid)
WHERE a.filledqty <> b.filledqty AND b.filledqty < 1000
WHERE a.clordid = '...'
ORDER BY a.filledqty DESC, b.filledqty DESC
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文