如何利用这个SQL进行删除这些公司 出现次数-1的数量,只剩下一家这些公司SELECT company_name, COUNT(*) AS count FROM ( SELECT company_name, DENSE_RANK() OVER (ORDER BY company_name) AS company_rank FROM your_table_name ) AS subquery GROUP BY company_name HAVING COUNT(*) > 1;
时间: 2024-03-17 17:46:42 浏览: 162
要删除出现次数-1的数量,可以使用以下SQL语句:
```
DELETE FROM your_table_name
WHERE company_name IN (
SELECT company_name
FROM (
SELECT company_name, COUNT(*) AS count
FROM (
SELECT company_name, DENSE_RANK() OVER (ORDER BY company_name) AS company_rank
FROM your_table_name
) AS subquery
GROUP BY company_name
) AS subquery2
WHERE count = 1
);
```
这个语句会先将表中所有公司按照名称进行排序,并计算它们的排名。然后,将它们分组并计算每个公司出现的次数。最后,将出现次数为1的公司名称提取出来,并从原表中删除这些公司的所有记录。这样就可以保留只出现一次的公司,而将其他公司删除。
相关问题
如何利用这个SQL进行删除出现次数>2的公司,使>2的公司出现次数为1:SELECT company_name, COUNT() AS count FROM ( SELECT company_name, DENSE_RANK() OVER (ORDER BY company_name) AS company_rank FROM your_table_name ) AS subquery GROUP BY company_name HAVING COUNT() > 1;
要将出现次数大于2的公司出现次数变为1,可以使用以下SQL语句:
```
UPDATE your_table_name
SET company_name = 'new_name'
WHERE company_name IN (
SELECT company_name
FROM (
SELECT company_name, COUNT(*) AS count
FROM (
SELECT company_name, DENSE_RANK() OVER (ORDER BY company_name) AS company_rank
FROM your_table_name
) AS subquery
GROUP BY company_name
HAVING COUNT(*) > 2
) AS subquery2
);
DELETE FROM your_table_name
WHERE company_name IN (
SELECT company_name
FROM (
SELECT company_name, COUNT(*) AS count
FROM (
SELECT company_name, DENSE_RANK() OVER (ORDER BY company_name) AS company_rank
FROM your_table_name
) AS subquery
GROUP BY company_name
HAVING COUNT(*) > 2
) AS subquery3
);
```
这个语句会先将表中所有公司按照名称进行排序,并计算它们的排名。然后,将它们分组并计算每个公司出现的次数。接着,将出现次数大于2的公司名称提取出来,并将这些公司的名称更新为一个新的名称,例如'new_name'。最后,将这些公司的所有记录从原表中删除。这样就可以将出现次数大于2的公司出现次数变为1。
重写这段代码 CREATE OR REPLACE PROCEDURE ord_dev AS bill_date VARCHAR2(10); v_sql LONG; v_count NUMBER; v_time VARCHAR2(20); v_d VARCHAR2(2); v_m VARCHAR2(2); CURSOR c_tb IS SELECT * FROM b_cur_names; c_row b_cur_names%ROWTYPE; BEGIN EXECUTE IMMEDIATE 'truncate table b_cur_names'; v_sql := 'insert into b_cur_names Select TABLE_NAME from SYS.ALL_TABLES a Where a.owner =upper(''jour1'') And a.table_name Like upper(''om_subscriber_202___'') and Substr(a.table_name,-6) > 202205 order by 1'; COMMIT; EXECUTE IMMEDIATE 'DROP table ord_dev'; ---------------- v_sql := 'insert into ord_dev Select a.order_id,a.done_date, a.cust_id, a.subscriber_ins_id, a.access_num, c.dev_id, c.dev_name, c.corp_org, c.company_name, c.department_code, c.department_name, c.remarks From JOUR1.Om_Subscriber_202301 a Left Join JOUR1.om_order_202301 b On a.order_id = b.order_id Left Join params1.sec_developer c On b.dev_id = c.dev_id Where 1 =2 '; COMMIT; ------------- FOR c_row IN c_tb LOOP v_sql := ' insert into ord_dev Select a.order_id,a.done_date, a.cust_id, a.subscriber_ins_id, a.access_num, c.dev_id, c.dev_name, c.corp_org, c.company_name, c.department_code, c.department_name, c.remarks From JOUR1.Om_Subscriber_' || substr(c_row.table_name, -6) || ' a Left Join JOUR1.om_order_' || substr(c_row.table_name, -6) || ' b On a.order_id = b.order_id Left Join params1.sec_developer c On b.dev_id = c.dev_id Where 1 = 1 And a.access_num Like ''192%'' And a.action = 0'; COMMIT; END LOOP; END;
下面是重写后的优化代码:
CREATE OR REPLACE PROCEDURE ord_dev AS
v_sql VARCHAR2(32767);
v_count NUMBER;
v_time VARCHAR2(20);
v_d VARCHAR2(2);
v_m VARCHAR2(2);
BEGIN
-- Truncate b_cur_names table
EXECUTE IMMEDIATE 'TRUNCATE TABLE b_cur_names';
-- Get the table names matching the criteria
v_sql := 'INSERT INTO b_cur_names
SELECT TABLE_NAME FROM SYS.ALL_TABLES a
WHERE a.owner = UPPER(:owner)
AND a.table_name LIKE UPPER(:table_name_pattern)
AND SUBSTR(a.table_name,-6) > :table_name_suffix
ORDER BY 1';
EXECUTE IMMEDIATE v_sql USING 'jour1', 'OM_SUBSCRIBER_202___', '202205';
-- Drop ord_dev table if exists
BEGIN
EXECUTE IMMEDIATE 'DROP TABLE ord_dev';
EXCEPTION
WHEN OTHERS THEN NULL;
END;
-- Create ord_dev table
v_sql := 'CREATE TABLE ord_dev AS
SELECT a.order_id, a.done_date,
a.cust_id,
a.subscriber_ins_id,
a.access_num,
c.dev_id,
c.dev_name,
c.corp_org,
c.company_name,
c.department_code,
c.department_name,
c.remarks
FROM JOUR1.Om_Subscriber_202301 a
LEFT JOIN JOUR1.om_order_202301 b
ON a.order_id = b.order_id
LEFT JOIN params1.sec_developer c
ON b.dev_id = c.dev_id
WHERE 1 = 2';
EXECUTE IMMEDIATE v_sql;
-- Insert data into ord_dev table
FOR c_row IN (SELECT * FROM b_cur_names)
LOOP
v_sql := 'INSERT INTO ord_dev
SELECT a.order_id, a.done_date,
a.cust_id,
a.subscriber_ins_id,
a.access_num,
c.dev_id,
c.dev_name,
c.corp_org,
c.company_name,
c.department_code,
c.department_name,
c.remarks
FROM JOUR1.Om_Subscriber_' || SUBSTR(c_row.table_name, -6) || ' a
LEFT JOIN JOUR1.om_order_' || SUBSTR(c_row.table_name, -6) || ' b
ON a.order_id = b.order_id
LEFT JOIN params1.sec_developer c
ON b.dev_id = c.dev_id
WHERE 1 = 1
AND a.access_num LIKE ''192%''
AND a.action = 0';
EXECUTE IMMEDIATE v_sql;
COMMIT;
END LOOP;
END;
/
重写后的代码进行了以下优化:
1. 使用绑定变量:将动态SQL中的字面量值改为绑定变量,以提高执行计划的复用和安全性。
2. 使用异常处理:在DROP TABLE语句中添加异常处理机制,以处理表不存在的情况,避免抛出异常。
3. 使用CREATE TABLE语句:替换了使用INSERT INTO SELECT 1=2 的方式来创建空表,直接使用CREATE TABLE语句创建ord_dev表。
4. 使用FOR循环优化插入:使用FOR循环遍历b_cur_names表,动态构建INSERT INTO语句,将数据插入ord_dev表。
请根据实际需求进行测试和验证,确保代码逻辑正确并满足预期的功能。
阅读全文