简体   繁体   English

所有行的 LISTAGG 并在一次选择中生成哈希

[英]LISTAGG for all rows and generate hash in one select

I have a query.我有一个疑问。 This query looks for records from a table and writes them to a string.此查询从表中查找记录并将它们写入字符串。 But the problem for me is this script only works for one key ( where t.keys = 'STIG6JGK' ) and not for all keys that are tables.但对我来说问题是这个脚本只适用于一个键(其中 t.keys = 'STIG6JGK' )而不适用于所有表键。 I can't fix it.我无法修复它。

select LISTAGG(t.DEVNAME , ',') WITHIN GROUP (ORDER BY 1) as alldev
from ( select t.DEVNAME,   
              row_number () over (partition by keys order by SEQUENCE) rn_asc     
       from ASU_DEVICES t
       where t.keys = 'STIG6JGK' 
) t 

But then I want it that will be in the "alldev" column, convert it to hash and write it in the column但后来我希望它将在“alldev”列中,将其转换为哈希并将其写入列中

SELECT DBMS_OBFUSCATION_TOOLKIT.md5 (input => UTL_RAW.cast_to_raw(alldev)) md5_val
FROM DUAL;

And then I want to write this file to the "HASH" table, in column 'key', and the 'hash'.然后我想将此文件写入“HASH”表中的“key”列和“hash”。

insert into hesh(key,hesh) VALUES ( t.keys ,md5_val)

This is all I want to do with one request or two, but for all keys at the same time.这就是我想要对一两个请求做的所有事情,但要同时针对所有键。 I would appreciate your help.我会很感激你的帮助。

I just can't merge three different queries into one.我只是无法将三个不同的查询合并为一个。

You can combine all of them using the following for all the KEYS :您可以使用以下所有KEYS组合所有这些:

INSERT INTO HESH (KEY, HESH)
    SELECT
        KEYS,
        DBMS_OBFUSCATION_TOOLKIT.MD5(INPUT => UTL_RAW.CAST_TO_RAW(ALLDEV))
    FROM
        ( SELECT T.KEYS,
                 LISTAGG(T.DEVNAME, ',') WITHIN GROUP( ORDER BY 1) AS ALLDEV
            FROM ASU_DEVICES T
        GROUP BY T.KEYS
        );

Update更新

You can use merge to achieve to update in the ecisting record as follows:您可以使用merge来实现在ecisting记录中的更新,如下所示:

MERGE INTO HESH H
USING ( SELECT T.KEYS,
                     LISTAGG(T.DEVNAME, ',') WITHIN GROUP( ORDER BY 1) AS ALLDEV
                FROM ASU_DEVICES T
            GROUP BY T.KEYS
            ) SRC
ON (H.KEY = SRC.KEY)
WHEN MATCHED THEN
UPDATE SET H.HESH = DBMS_OBFUSCATION_TOOLKIT.MD5(INPUT => UTL_RAW.CAST_TO_RAW(ALLDEV));

I did it this way but didn't make a table entry我是这样做的,但没有做表条目

 with txn as ( select keys, hesh from ( select keys , LISTAGG(t.DEVNAME , ',') WITHIN GROUP (ORDER BY 1) as hesh from ( select t.keys, t.DEVNAME, row_number () over (partition by keys order by TO_NUMBER(SEQUENCE, '99')) rn_asc from ASU_DEVICES t ) t group by keys )) SELECT DBMS_OBFUSCATION_TOOLKIT.md5 (input => UTL_RAW.cast_to_raw(txn.hesh)) md5_val , txn.keys FROM txn;
As one solution to the problem, I think so, but it does not work 作为该问题的一种解决方案,我认为是这样,但它不起作用

 UPDATE ASU_ROYTE SET hesh SELECT DBMS_OBFUSCATION_TOOLKIT.md5 (input => UTL_RAW.cast_to_raw(txn.hesh)) md5_val FROM txn WHERE ASU_ROYTE.keys = txn.keys

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM