0

假设我有一个表格,如下所示:

InventoryVAS     colVAS     IP

fvas1               5       172.21.4.97
fvas2               6       172.21.4.97
abcd                4       10.4.6.89
ef1                 7       169.21.5.67
ef2                 8       169.21.5.67

每当有 2 个相同的连续IPs 时,InventoryVAS除最后一个字符外,列将始终具有相同的值。

例如 fvas1 和 fvas2 中的 fvas 用于 172.21.4.97

我需要我的决赛桌是这样的:

InventoryVAS     colVAS     IP

fvas                5/6     172.21.4.97
abcd                4       10.4.6.89
ef                  7/8     169.21.5.67

在实际场景中,我的表有 100 条记录,除了最后一个字符外,它们IP的 s 和InventoryVAS值相同。IP我需要排除最后一个字符,以使其成为具有中间列(colVAS)值=(row1中的值)/(row2中的值)的单行。


我试过戈登的回答并得到以下信息:

create table VAS(
InventoryVAS varchar2(40),
colVAS       number,
IP           varchar2(40)
)

insert into VASInventory values ('fvas1',5,172.21.4.97);
insert into VASInventory values ('fvas2',6,172.21.4.97);
insert into VASInventory values ('abcd',4,10.4.6.89);
insert into VASInventory values ('ef1',7,169.21.5.67);
insert into VASInventory values ('ef2',8,169.21.5.67);
select * from VAS;

select substr(InventoryVas, length(InventoryVas) - 1) as InventoryVas,
       (case when min(colvas) <> max(colvas) then min(colVas)||'/'||max(colVas)
             else min(colvas)
        end) as colVas,
       ip
from VAS
group by substr(InventoryVas, length(InventoryVas) - 1), ip;
---------------------------------------------------------------------------
ERROR :
ORA-00932: inconsistent datatypes: expected CHAR got NUMBER
00932. 00000 -  "inconsistent datatypes: expected %s got %s"
*Cause:    
*Action:
Error at Line: 24 Column: 19

create table VAS(
InventoryVAS varchar2(40),
colVAS       number,
IP           varchar2(40)
)
drop table VAS;
insert into VASInventory values ('fvas1',5,172.21.4.97);
insert into VASInventory values ('fvas2',6,172.21.4.97);
insert into VASInventory values ('abcd',4,10.4.6.89);
insert into VASInventory values ('ef1',7,169.21.5.67);
insert into VASInventory values ('ef2',8,169.21.5.67);
select * from VAS;

select substr(InventoryVas, length(InventoryVas) - 1) as InventoryVas,
       (case when min(colvas) <> max(colvas) then min(colVas)||'/'||max(colVas)
             else min(colvas)
        end) as colVas,
       ip
from VAS
group by substr(InventoryVas, length(InventoryVas) - 1), ip;
---------------------------------------------------------------------------
ERROR :
ORA-00932: inconsistent datatypes: expected CHAR got NUMBER
00932. 00000 -  "inconsistent datatypes: expected %s got %s"
*Cause:    
*Action:
Error at Line: 24 Column: 19
4

2 回答 2

0

这是否满足您的需求:

select substr(InventoryVas, len(InventoryVas) - 1) as InventoryVas,
       (case when min(colvas) <> max(colvas) then min(colVas)||'/'||max(colVas)
             else min(colvas)
        end) as colVas,
       ip
from t
group by substr(InventoryVas, len(InventoryVas) - 1), ip;

这使得中间列具有最小值colvas和最大值,由 a 分隔'/'

于 2013-09-07T21:42:00.823 回答
0

如果没有重复的 IP,这将为您提供正确的 INVENTORYVAS 'abcd'。顶部聚合函数可以是 MIN/MAX。它只有在那里才能使分组工作。

SELECT MIN ((CASE WHEN cnt = 1 THEN v2.InventoryVAS ELSE v1.InventoryVAS END)) AS InventoryVAS,
     v1.colvas,
     v1.ip
FROM (SELECT SUBSTR (InventoryVas, 0, LENGTH (InventoryVas) - 1) AS InventoryVas,
                  (CASE
                      WHEN MIN (colvas) <> MAX (colvas)
                      THEN
                         MIN (colVas) || '/' || MAX (colVas)
                      ELSE
                         TO_CHAR (MIN (colvas))
                   END) AS colVas,
                  IP,
                  COUNT (*) AS cnt
             FROM VAS
         GROUP BY SUBSTR (InventoryVas, 0, LENGTH (InventoryVas) - 1), ip) v1
     JOIN VAS v2 ON v2.IP = v1.IP
GROUP BY v1.ip, v1.colvas

也许它可以进一步完善!

于 2013-09-09T07:33:33.497 回答