Oracle SQL:在插入中使用序列与 Select 语句

Oracle SQL: Use sequence in insert with Select Statement(Oracle SQL:在插入中使用序列与 Select 语句)
本文介绍了Oracle SQL:在插入中使用序列与 Select 语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我想运行以下查询:

Basically I want to run the following query:

INSERT INTO historical_car_stats (historical_car_stats_id, year, month, make, model, region, avg_msrp, count)
  SELECT
    my_seq.nextval,
    '2010',
    '12',
    'ALL',
    'ALL',
    region,
    sum(avg_msrp * count) / sum(count),
    sum(count)
  FROM historical_car_stats
  WHERE year = '2010'
        AND month = '12'
        AND make != 'ALL'
  GROUP BY region;

它不起作用,因为此处不允许序列号"SQL 错误.我怎样才能写这个让 Oracle 让我做我想做的事?

It doesn't work because "sequence number not allowed here" SQL error. How can I write this so Oracle will let me do what I want to do?

推荐答案

假设你想在用序列生成键之前对数据进行分组,听起来你想要类似的东西

Assuming that you want to group the data before you generate the key with the sequence, it sounds like you want something like

INSERT INTO HISTORICAL_CAR_STATS (
    HISTORICAL_CAR_STATS_ID, 
    YEAR,
    MONTH, 
    MAKE,
    MODEL,
    REGION,
    AVG_MSRP,
    CNT) 
SELECT MY_SEQ.nextval,
       year,
       month,
       make,
       model,
       region,
       avg_msrp,
       cnt
  FROM (SELECT '2010' year,
               '12' month,
               'ALL' make,
               'ALL' model,
               REGION,
               sum(AVG_MSRP*COUNT)/sum(COUNT) avg_msrp,
               sum(cnt) cnt
          FROM HISTORICAL_CAR_STATS
         WHERE YEAR = '2010' 
           AND MONTH = '12'
           AND MAKE != 'ALL' 
         GROUP BY REGION)

这篇关于Oracle SQL:在插入中使用序列与 Select 语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Execute complex raw SQL query in EF6(在EF6中执行复杂的原始SQL查询)
Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)