将连续记录分组,保存最后一个字符

时间:2013-09-07 20:03:56

标签: sql oracle

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

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

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

e.g。 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条记录,除了最后一个字符之外,IPInventoryVAS值与IP相同。我需要排除这个最后一个字符,以使其成为一个中间列(colVAS)值=(row1中的值)/(row2中的值)的单行。


我尝试过Gordon的回答并得到以下内容:

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

2 个答案:

答案 0 :(得分: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,最大值为'/'

答案 1 :(得分: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

也许它可以进一步完善!