在resilt表

时间:2016-09-16 07:50:41

标签: sql oracle group-by

我需要从结果表中选择一组行。我写了select但有错误信息ORA-00913。如何写一个正确的选择?我有一个 结果表:

DROP TABLE MONEY_ACTIVITY;
CREATE TABLE MONEY_ACTIVITY (
       ID                   NUMBER(15) NOT NULL,
       DOCUMENT_NUMBER      VARCHAR2(13) NULL,
       ID_CUSTOMER          NUMBER NULL,
       BASE_AMOUNT          NUMBER(5,2) NULL
);

并插入:

INSERT INTO MONEY_ACTIVITY  VALUES (1, 'input_saldo', 111222, 300);
INSERT INTO MONEY_ACTIVITY  VALUES (2, 'input_saldo', 222333, 600);
INSERT INTO MONEY_ACTIVITY  VALUES (3, 'ouput_saldo', 333444, 400);
INSERT INTO MONEY_ACTIVITY  VALUES (4, 'ouput_saldo', 444555, 600);
INSERT INTO MONEY_ACTIVITY  VALUES (5, 'TN0001', 444555, 600);
INSERT INTO MONEY_ACTIVITY  VALUES (6, 'TN0002', 444555, 700);
INSERT INTO MONEY_ACTIVITY  VALUES (7, 'TN0003', 333444, 600);
INSERT INTO MONEY_ACTIVITY  VALUES (8, 'TN0004', 333444, 700);
INSERT INTO MONEY_ACTIVITY  VALUES (9, 'TN0005', 111444, 600);
INSERT INTO MONEY_ACTIVITY  VALUES (10, 'TN0006', 222444, 700);

我需要获得结果表:

input_saldo 900
ouput_saldo 1000
TN0001  600
TN0002  700
TN0003  600
TN0004  700
TN0005  600
TN0006  700

我正在尝试:

select 
case when DOCUMENT_NUMBER = 'input_saldo' then  (select DOCUMENT_NUMBER, SUM(BASE_AMOUNT)
                                                 from MONEY_ACTIVITY   
                                                 where DOCUMENT_NUMBER = 'input_saldo'
                                                 group by DOCUMENT_NUMBER) end case,   
case when DOCUMENT_NUMBER = 'ouput_saldo' then  (select DOCUMENT_NUMBER, SUM(BASE_AMOUNT)
                                                 from MONEY_ACTIVITY   
                                                 where DOCUMENT_NUMBER = 'ouput_saldo'
                                                 group by DOCUMENT_NUMBER) end case,                                                 
DOCUMENT_NUMBER, 
BASE_AMOUNT 
from MONEY_ACTIVITY
where DOCUMENT_NUMBER NOT IN ('input_saldo', 'output_saldo');

但是ora-00913的值太多了

如何写出正确的“选择”?

抱歉,有一些新的条件。该表可以包含具有相同Document_number和不同ID_customer的行。这些行不得加总。

示例:

1 input_saldo 111222 300 2 input_saldo 222333 600 4 ouput_saldo 444555 600 3 ouput_saldo 333444 400 11 TN0001 333444 600 - !!!!!!! 5 TN0001 444555 600 - !!!!!!! 6 TN0002 444555 700 ......

我尝试执行:

SELECT DOCUMENT_NUMBER, ID_CUSTOMER, SUM(BASE_AMOUNT)
FROM MONEY_ACTIVITY
GROUP BY DOCUMENT_NUMBER, ID_CUSTOMER;

返回结果是:

DOCUMENT_NUMBER ID_CUSTOMER SUM(BASE_AMOUNT)

input_saldo 222333  600
input_saldo 111222  300
ouput_saldo 333444  400
ouput_saldo 444555  600
TN0001  444555  600
TN0001  333444  600
TN0002  444555  700
TN0003  333444  600
TN0004  333444  700
TN0005  111444  600
TN0006  222444  700

但我需要:

input_saldo 111222  900
ouput_saldo 444555  1000
TN0001  444555  600
TN0001  333444  600
TN0002  444555  700
TN0003  333444  600
TN0004  333444  700
TN0005  111444  600
TN0006  222444  700

8 个答案:

答案 0 :(得分:1)

只需使用GROUP BY -

/

答案 1 :(得分:1)

您可以应用汇总功能,例如:

SELECT DOCUMENT_NUMBER, SUM(BASE_AMOUNT)
FROM MONEY_ACTIVITY
GROUP BY DOCUMENT_NUMBER
ORDER BY 1

答案 2 :(得分:0)

我认为这个错误是因为你的子查询应该返回一个值(sum(BASE_AMOUNT))。 从子查询中删除列DOCUMENT_NUMBER。

答案 3 :(得分:0)

不是通过查询逻辑的优点限制我自己只报告错误..子查询返回两列,而结果应该只包含一个

select 
case when DOCUMENT_NUMBER = 'input_saldo' then  (select  SUM(BASE_AMOUNT)
                                                 from MONEY_ACTIVITY   
                                                 where DOCUMENT_NUMBER = 'input_saldo'
                                                 group by DOCUMENT_NUMBER) end case,   
case when DOCUMENT_NUMBER = 'ouput_saldo' then  (select  SUM(BASE_AMOUNT)
                                                 from MONEY_ACTIVITY   
                                                 where DOCUMENT_NUMBER = 'ouput_saldo'
                                                 group by DOCUMENT_NUMBER) end case,                                                 
DOCUMENT_NUMBER, 
BASE_AMOUNT 
from MONEY_ACTIVITY
where DOCUMENT_NUMBER NOT IN ('input_saldo', 'output_saldo');

答案 4 :(得分:0)

从您的数据样本中,一个简单的聚合可以完成工作:

// Product Details 
dataLayer.push({
  'ecommerce': {
    'currencyCode': 'EUR',
    'detail': {
      'products': [{
        'name': 'Tetris', // Name or ID is required.
        'id': 'g395',
        'sku': 'g395',
        'category': 'Toys & Games',
        'brand': 'Mojo-jojo!'
      }]
    }
  }
});
//// Transaction

dataLayer.push({
  "ecommerce": {
    "currencyCode": "EUR",
    "checkout": {
      "actionField": {
        "step": 4
      }
    },
    "purchase": {
      "actionField": {
        "id": 13202,
        "affiliation": "www.mojo-jojo.com",
        "revenue": 0.01
      },
      "products": [{
        "id": "g105",
        "sku": "g105",
        "name": "Duracell AA",
        "brand": "Mojo-jojo!",
        "category": "Accessories",
        "price": 0.7,
        "quantity": 1
      }]
    }
  },
  "transactionId": 13202,
  "transactionTotal": 0.01,
  "transactionProducts": [{
    "id": "g105",
    "sku": "g105",
    "name": "Duracell AA",
    "brand": "Mojo-jojo!",
    "category": "Accessories",
    "price": 0.7,
    "quantity": 1
  }],
  "event": "transaction",
  "product_ids": [105],
  "num_items": 1,
  "conversion_value": 0.01
});

如果您需要更复杂的逻辑,请编辑您的问题以更好地解释您的需求

答案 5 :(得分:0)

为什么不使用简单查询?

SELECT DOCUMENT_NUMBER,SUM(BASE_AMOUNT)
FROM MONEY_ACTIVITY
GROUP BY DOCUMENT_NUMBER 

答案 6 :(得分:0)

您有一个子查询来填充标量值,子查询返回两个值。 根据上面的MarinC回答,你可以从子查询中删除DOCUMENT_NUMBER。

您的查询也看起来有误,因为您似乎拼写错误' output_saldo'而且你明确地排除了两个......骂人的话。值因此它们永远不会出现在结果集中。

这样做可以在不使用子查询的情况下提出要求;

SELECT document_number, SUM(base_amount) 
  FROM money_activity
GROUP BY document_number
ORDER BY document_number desc

答案 7 :(得分:0)

select 
    case when DOCUMENT_NUMBER in ('input_saldo', 'ouput_saldo')  then DOCUMENT_NUMBER else DOCUMENT_NUMBER_1 end DOCUMENT_NUMBER,
    case when DOCUMENT_NUMBER in ('input_saldo', 'ouput_saldo')  then BASE_AMOUNT else BASE_AMOUNT_1 end BASE_AMOUNT,
    case when DOCUMENT_NUMBER in ('input_saldo', 'ouput_saldo') then null else ID_CUSTOMER_1 end ID_CUSTOMER
from 
    (select DOCUMENT_NUMBER, 
        sum(BASE_AMOUNT) BASE_AMOUNT,  
        DOCUMENT_NUMBER_1, 
        BASE_AMOUNT_1, 
        ID_CUSTOMER_1
    from 
        (select 
            case when DOCUMENT_NUMBER in ('input_saldo', 'ouput_saldo')  then DOCUMENT_NUMBER else null end DOCUMENT_NUMBER,
            case when DOCUMENT_NUMBER in ('input_saldo', 'ouput_saldo') then BASE_AMOUNT else null end BASE_AMOUNT,
            case when DOCUMENT_NUMBER in ('input_saldo', 'ouput_saldo') then  null else ID end ID_1,
            case when DOCUMENT_NUMBER in ('input_saldo', 'ouput_saldo') then null else DOCUMENT_NUMBER end DOCUMENT_NUMBER_1,
            case when DOCUMENT_NUMBER in ('input_saldo', 'ouput_saldo') then  null else BASE_AMOUNT end BASE_AMOUNT_1,
            case when DOCUMENT_NUMBER in ('input_saldo', 'ouput_saldo') then  null else ID_CUSTOMER end ID_CUSTOMER_1          
        from MONEY_ACTIVITY)
    group by DOCUMENT_NUMBER, DOCUMENT_NUMBER_1, BASE_AMOUNT_1, ID_CUSTOMER_1);