Oracle 按问题分组

发布于 2024-10-20 05:43:45 字数 1384 浏览 3 评论 0原文

我有以下查询。问题是最后一列productdesc 返回两条记录,并且查询由于不同而失败。现在我需要在选择查询的 where 子句中添加一列,以便它返回一条记录。问题是我需要的专栏 添加不应该是 group by 子句的一部分。

  SELECT product_billing_id,
         billing_ele,
         SUM(round(summary_net_amt_excl_gst/100)) gross,
         (SELECT DISTINCT description 
            FROM RES.tariff_nt 
           WHERE product_billing_id = aa.product_billing_id 
             AND billing_ele = aa.billing_ele) productdescr
    FROM bil.bill_sum aa
   WHERE file_id = 38613 --1=1
     AND line_type = 'D'
     AND (product_billing_id, billing_ele) IN (SELECT DISTINCT 
                                                    product_billing_id, 
                                                    billing_ele 
                                               FROM bil.bill_l2 )
     AND trans_type_desc <> 'Change'
GROUP BY product_billing_id, billing_ele

我想通过向 where 子句添加一个新的过滤器来将 select 语句修改为以下方式,以便它返回一条记录。

(SELECT DISTINCT description 
   FROM RRES.tariff_nt 
  WHERE product_billing_id = aa.product_billing_id 
    AND billing_ele = aa.billing_ele
    AND (rate_structure_start_date <= TO_DATE(aa.p_effective_date,'yyyymmdd') 
    AND rate_structure_end_date > TO_DATE(aa.p_effective_date,'yyyymmdd'))
) productdescr

aa.p_ effective_date 不应成为 GROUP BY 子句的一部分。我该怎么做呢? Oracle 是数据库。

I have the below query. The problem is the last column productdesc is returning two records and the query fails because of distinct. Now i need to add one more column in where clause of the select query so that it returns one record. The issue is that the column i need
to add should not be a part of group by clause.

  SELECT product_billing_id,
         billing_ele,
         SUM(round(summary_net_amt_excl_gst/100)) gross,
         (SELECT DISTINCT description 
            FROM RES.tariff_nt 
           WHERE product_billing_id = aa.product_billing_id 
             AND billing_ele = aa.billing_ele) productdescr
    FROM bil.bill_sum aa
   WHERE file_id = 38613 --1=1
     AND line_type = 'D'
     AND (product_billing_id, billing_ele) IN (SELECT DISTINCT 
                                                    product_billing_id, 
                                                    billing_ele 
                                               FROM bil.bill_l2 )
     AND trans_type_desc <> 'Change'
GROUP BY product_billing_id, billing_ele

I want to modify the select statement to the below way by adding a new filter to the where clause so that it returns one record .

(SELECT DISTINCT description 
   FROM RRES.tariff_nt 
  WHERE product_billing_id = aa.product_billing_id 
    AND billing_ele = aa.billing_ele
    AND (rate_structure_start_date <= TO_DATE(aa.p_effective_date,'yyyymmdd') 
    AND rate_structure_end_date > TO_DATE(aa.p_effective_date,'yyyymmdd'))
) productdescr

The aa.p_effective_date should not be a part of GROUP BY clause. How can I do it? Oracle is the Database.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

絕版丫頭 2024-10-27 05:43:45

因此,给定的product_billing_id/billing_ele 有多个RES.tariff 记录,按开始/结束日期进行区分。

您需要包含bil.bill_sum 中的“p_ effective_date”的记录描述。更关键的是你不能(或不想)将其包含在分组依据中。这表明 bil.bill_sum 中有多行具有不同的有效日期。

问题是,如果您要汇总具有不同日期的多行,您希望发生什么。您希望使用其中哪一个日期作为获取描述的日期。

如果没关系,只需使用 MIN(aa.p_ effective_date) 或 MAX 即可。

So there are multiple RES.tariff records for a given product_billing_id/billing_ele, differentiated by the start/end dates

You want the description for the record that encompasses the 'p_effective_date' from bil.bill_sum. The kicker is that you can't (or don't want to) include that in the group by. That suggests you've got multiple rows in bil.bill_sum with different effective dates.

The issue is what do you want to happen if you are summarising up those multiple rows with different dates. Which of those dates do you want to use as the one to get the description.

If it doesn't matter, simply use MIN(aa.p_effective_date), or MAX.

留一抹残留的笑 2024-10-27 05:43:45

你研究过Oracle的分析功能吗?这是很好的链接示例分析函数

Have you looked into the Oracle analytical functions. This is good link Analytical Functions by Example

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文