SQL qn:- 比较行中的数据

发布于 2024-08-05 06:53:29 字数 187 浏览 2 评论 0原文

我想比较行中的数字数据。例如,我有一个表,其中有一列:-

Number
======
 1.88
 9.99
 8.76
 9.88

我想将第二个值、第三个值、第四个值与第一个值进行比较。然后第三个、第四个值到第二个。然后是第四到第三。

我怎样才能构造一个sql来做到这一点?

i would like to compare numeric data in rows. for eg, i have a table that has a column as such:-

Number
======
 1.88
 9.99
 8.76
 9.88

I want to compare 2nd value, 3rd value, 4th value to the 1st value. And then 3rd, 4th value to the 2nd. then 4th to 3rd.

How can i construct an sql to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

夜血缘 2024-08-12 06:53:29

您需要有某种方法为行分配序列号。

考虑下表:

seq  number
---  ------
  1    1.88
  2    9.99
  3    8.76
  4    9.88

创建为:

drop table a;
commit;
create table a (seq integer,num float);                                     
insert into a (seq,num) values (1,1.88);
insert into a (seq,num) values (2,9.99);
insert into a (seq,num) values (3,8.76);
insert into a (seq,num) values (4,9.88);
commit;                                 

然后您可以执行:

select
    ta.seq as aseq,
    tb.seq as bseq,
    ta.num as anum,
    tb.num as bnum,
    ta.num - tb.num as diff
from a ta, a tb
where tb.seq > ta.seq;

这将为您提供:

aseq  bseq  anum  bnum   diff
----  ----  ----  ----  -----
   1     2  1.88  9.99  -8.11
   1     3  1.88  8.76  -6.88
   1     4  1.88  9.88  -8.00
   2     3  9.99  8.76   1.23
   2     4  9.99  9.88   0.11
   3     4  8.76  9.88  -1.12

You need to have some way of assigning sequence numbers to the rows.

Consider the following table:

seq  number
---  ------
  1    1.88
  2    9.99
  3    8.76
  4    9.88

created with:

drop table a;
commit;
create table a (seq integer,num float);                                     
insert into a (seq,num) values (1,1.88);
insert into a (seq,num) values (2,9.99);
insert into a (seq,num) values (3,8.76);
insert into a (seq,num) values (4,9.88);
commit;                                 

You can then execute:

select
    ta.seq as aseq,
    tb.seq as bseq,
    ta.num as anum,
    tb.num as bnum,
    ta.num - tb.num as diff
from a ta, a tb
where tb.seq > ta.seq;

which will give you:

aseq  bseq  anum  bnum   diff
----  ----  ----  ----  -----
   1     2  1.88  9.99  -8.11
   1     3  1.88  8.76  -6.88
   1     4  1.88  9.88  -8.00
   2     3  9.99  8.76   1.23
   2     4  9.99  9.88   0.11
   3     4  8.76  9.88  -1.12
听,心雨的声音 2024-08-12 06:53:29

不确定这个的实用性,但也许这就是您正在寻找的......
比较值的一种更简单的方法是使用 ORDER BY 子句。

我冒昧地向表中添加了一个 id 列,我将其称为 tblTst。这可以方便地选择一些特定值(请参阅 (...) 子句中的 WHERE id),并且它还用于仅避免叉积中的重复。要比较整个表,只需省略 WHERE 子句即可。连接条件可以更改为严格小于 (<),以防止将值与自身进行比较(只是方便测试我的“=”)

表中有 100 个值,矩阵的底部(或顶部)三角形将包括 5,000比较。 (再次,我很困惑这如何有用或有趣......)

select T1.value AS Operand1,
     CASE WHEN T1.Value < T2.Value THEN '<'
          WHEN T1.Value = T2.Value Then '='
          ELSE '>'
     END AS Comp,
     T2.Value AS Operand2
FROM tblTst T1
JOIN tblTst T2 ON T1.id <= T2.id
WHERE T1.id in (1,2,3,4)

Sample output     
Operand1 Comp  Operand2
1.88    =   1.88
1.88    <   9.99
1.88    <   8.76
1.88    <   9.88
9.99    =   9.99
9.99    >   8.76
9.99    >   9.88
8.76    =   8.76
8.76    <   9.88
9.88    =   9.88

Not sure of the utility of this but maybe that is what you are looking for...
An easier way to compare values is to use the ORDER BY clause.

I took the liberty of adding an id column to the table which I call tblTst. This is convenient to select some specific values (see the WHERE id in (...) clause), and also it is used to only avoid duplicates in the cross product. To compare the whole table, simply omit the WHERE clause. The join condition can be changed to strictly less than (<) to prevent comparing values with themselves (was just convenient to test my '=')

With 100 values in the table the bottom (or top) triangle of the matrix will include 5,000 comparisons. (again, I'm puzzled as to how this can be useful or fun...)

select T1.value AS Operand1,
     CASE WHEN T1.Value < T2.Value THEN '<'
          WHEN T1.Value = T2.Value Then '='
          ELSE '>'
     END AS Comp,
     T2.Value AS Operand2
FROM tblTst T1
JOIN tblTst T2 ON T1.id <= T2.id
WHERE T1.id in (1,2,3,4)

Sample output     
Operand1 Comp  Operand2
1.88    =   1.88
1.88    <   9.99
1.88    <   8.76
1.88    <   9.88
9.99    =   9.99
9.99    >   8.76
9.99    >   9.88
8.76    =   8.76
8.76    <   9.88
9.88    =   9.88
幻梦 2024-08-12 06:53:29

为了我的理解,只是将 @paxdiablo 的工作简化为 t-sql 用语:


declare @t table 
(
    id integer identity (1, 1)
)
-- sample data
insert @t default values
insert @t default values
insert @t default values
insert @t default values

select
    a.id as a_id,
    b.id as b_id
from 
    @t as a 
cross join 
    @t as b
where 
    a.id < b.id
order by
    1, 2

给出


a_id        b_id
----------- -----------
1           2
1           3
1           4
2           3
2           4
3           4

(6 row(s) affected)

Just simplifying @paxdiablo's work into t-sql parlance for my understanding:


declare @t table 
(
    id integer identity (1, 1)
)
-- sample data
insert @t default values
insert @t default values
insert @t default values
insert @t default values

select
    a.id as a_id,
    b.id as b_id
from 
    @t as a 
cross join 
    @t as b
where 
    a.id < b.id
order by
    1, 2

gives


a_id        b_id
----------- -----------
1           2
1           3
1           4
2           3
2           4
3           4

(6 row(s) affected)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文