【问题标题】:trying to find transactions with atleast 1 item with transaction amount <= $10尝试查找至少 1 件交易金额 <= 10 美元的交易
【发布时间】:2019-04-04 02:42:05
【问题描述】:

我正在尝试查找不同交易的计数、数量总和、美元价值总和以及不同个人 ID 的计数,其中至少一个项目的价值 >=10 美元。我创建了一个 sql 数据库。我也试图打破美元价值的范围

https://dbfiddle.uk/?rdbms=oracle_11.2&fiddle=e1ee5e9a6264d758f4a7480fa8d6b964

样本数据:

个人ID |美元价值我们 |数量 |交易号 |货号 | txn_date | brand_org_code | is_merch |货币代码 | line_item_amt_type_cd ------------: | --------------: | --------: | -----------------: | :----- | :----------------- | :------------- | --------: | :------------ | :-------------------- 1 | 10 | 30 |第567章903633 | 02/01/2019 00:00:00 |品牌 | 1 |美元 |小号 1 | 20 | 30 |第567章123767 | 02/01/2019 00:00:00 |品牌 | 1 |美元 |小号 2 | 11 | 80 | 910 | 363635 | 2019 年 2 月 11 日 00:00:00 |品牌 | 1 |美元 |小号 3 | 9 | 62 |第855章678364 | 2019 年 2 月 12 日 00:00:00 |品牌 | 1 |美元 |小号

这是输出的样例:

【问题讨论】:

  • 你的预期输出是什么?
  • 您标记了您的问题 oracle,但您提供了 SQLServer fiddlde。你用的是哪一个?
  • 我正在使用 oracle。但是我错误地在 sql server 上创建了一个示例,对不起,
  • edit your question 为您的示例数据提供预期输出。
  • 感谢您更新您的问题以显示预期的结果结构,这使事情更加清晰。现在您还需要向我们展示结构中的实际数据,它对应于您的示例数据...

标签: sql oracle


【解决方案1】:

使用聚合:

select Dollar_Range, count(distinct individual_id), count(*), sum(quantity)
from (select t.*,
             (case when dollar_value_us < 10 then 'below $10'
                   when dollar_value_us < 20 then '$10 - $20'
                   when dollar_value_us < 30 then '$20 - $30'
                   when dollar_value_us < 40 then '$30 - $40'
                   when dollar_value_us < 50 then '$40 - $50'
                   else '$50 - above'
            end) as dollar_range
      from t
     ) t
group by dollar_range
order by min(dollar_value_us);

【讨论】:

  • @SomiyaL 。 . .以及它如何不起作用。您的评论没有帮助。
【解决方案2】:
select 'below $10' as Dollar_Range count(individual_id), count(transaction_number), sum(quantity) from myData where dollar_value_us < 10
union
select '$10 - $20' as Dollar_Range count(individual_id), count(transaction_number), sum(quantity) from myData where dollar_value_us >=10 and dollar_value_us < 20
union
select '$20 - $30' as Dollar_Range count(individual_id), count(transaction_number), sum(quantity) from myData where dollar_value_us >=20 and dollar_value_us < 30
union
select '$30 - $40' as Dollar_Range count(individual_id), count(transaction_number), sum(quantity) from myData where dollar_value_us >=30 and dollar_value_us < 40
union
select '$40 - $50' as Dollar_Range count(individual_id), count(transaction_number), sum(quantity) from myData where dollar_value_us >= 40 and dollar_value_us < 50
union
select '$50 - above' as Dollar_Range count(individual_id), count(transaction_number), sum(quantity) from myData where dollar_value_us >50

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-24
    • 1970-01-01
    • 1970-01-01
    • 2013-10-21
    • 1970-01-01
    • 2020-07-10
    • 1970-01-01
    • 2022-08-17
    相关资源
    最近更新 更多