除了 SQL*Plus 中指定的列之外,是否可以 BREAK ON 一列?

发布于 2024-07-23 22:42:10 字数 3025 浏览 8 评论 0原文

这个很难解释,所以我将尝试使用示例来展示我的意思。 请注意,我并不是在询问是否可以在 BREAK 语句中使用多个列 - 我知道可以。

假设我有一个如下所示的查询:

SELECT  invoice_no, invoice_date, vendor, account, amount
FROM    invoice
ORDER   BY vendor, invoice_no, account

假设结果集是:

INVOICE_NO INVOICE_DATE VENDOR  ACCOUNT AMOUNT
---------- ------------ ------- ------- ------
0001       30-JAN-2009  Alpha   1000     50.00
0003       30-JAN-2009  Alpha   1000    125.00
0003       30-JAN-2009  Alpha   3000     33.33
0006       02-FEB-2009  Alpha   2000     40.00
0005       31-JAN-2009  Bravo   1000     40.00
0002       30-JAN-2009  Charlie 2000    120.75
0002       30-JAN-2009  Charlie 3000      5.00
0004       30-JAN-2009  Charlie 1000    900.50

因此您可以看到一些供应商有多个发票,而一些发票有多个帐户。

要隐藏重复的供应商名称和发票编号,我可以使用 SQL*Plus 的 BREAK 命令如下所示:

BREAK ON vendor ON invoice_no

产生此结果集:

INVOICE_NO INVOICE_DATE VENDOR  ACCOUNT AMOUNT
---------- ------------ ------- ------- ------
0001       30-JAN-2009  Alpha   1000     50.00
0003       30-JAN-2009          1000    125.00
           30-JAN-2009          3000     33.33
0006       02-FEB-2009          2000     40.00
0005       31-JAN-2009  Bravo   1000     40.00
0002       30-JAN-2009  Charlie 2000    120.75
           30-JAN-2009          3000      5.00
0004       30-JAN-2009          1000    900.50

到目前为止,一切顺利。 我还想隐藏重复的发票日期,以便仅显示每张发票的第一个日期。 但是,如果我使用此命令:

BREAK ON vendor ON invoice_no ON invoice_date

它会走得太远并隐藏发票 0003 和 0004 的日期,只是因为它们与各自供应商的先前发票相同:

INVOICE_NO INVOICE_DATE VENDOR  ACCOUNT AMOUNT
---------- ------------ ------- ------- ------
0001       30-JAN-2009  Alpha   1000     50.00
0003                            1000    125.00
                                3000     33.33
0006       02-FEB-2009          2000     40.00
0005       31-JAN-2009  Bravo   1000     40.00
0002       30-JAN-2009  Charlie 2000    120.75
                                3000      5.00
0004                    Charlie 1000    900.50

我真正想要的是像这样的命令语法以下(我创造了术语AND):

BREAK ON vendor ON invoice_no AND invoice_date

目的是,每当它在invoice_no上中断时,也会在invoice_date上中断(因为我知道一个发票号码不能有两个发票日期) :

INVOICE_NO INVOICE_DATE VENDOR  ACCOUNT AMOUNT
---------- ------------ ------- ------- ------
0001       30-JAN-2009  Alpha   1000     50.00
0003       30-JAN-2009          1000    125.00
                                3000     33.33
0006       02-FEB-2009          2000     40.00
0005       31-JAN-2009  Bravo   1000     40.00
0002       30-JAN-2009  Charlie 2000    120.75
                                3000      5.00
0004       30-JAN-2009  Charlie 1000    900.50

现在发票 0003 和 0004 的日期已正确显示。

在 SQL*Plus 中是否有任何方法可以完成此操作?

This one's tough to explain, so I'll try to show what I'm after using examples. Please note that I'm not asking if it's possible to use multiple columns in a BREAK statement--I know that it is.

Suppose I have a query like the following:

SELECT  invoice_no, invoice_date, vendor, account, amount
FROM    invoice
ORDER   BY vendor, invoice_no, account

And suppose the result set from that is:

INVOICE_NO INVOICE_DATE VENDOR  ACCOUNT AMOUNT
---------- ------------ ------- ------- ------
0001       30-JAN-2009  Alpha   1000     50.00
0003       30-JAN-2009  Alpha   1000    125.00
0003       30-JAN-2009  Alpha   3000     33.33
0006       02-FEB-2009  Alpha   2000     40.00
0005       31-JAN-2009  Bravo   1000     40.00
0002       30-JAN-2009  Charlie 2000    120.75
0002       30-JAN-2009  Charlie 3000      5.00
0004       30-JAN-2009  Charlie 1000    900.50

So you can see that some vendors have multiple invoices, and some invoices have multiple accounts.

To hide the duplicate vendor names and invoice numbers, I can use SQL*Plus's BREAK command like so:

BREAK ON vendor ON invoice_no

Which produces this result set:

INVOICE_NO INVOICE_DATE VENDOR  ACCOUNT AMOUNT
---------- ------------ ------- ------- ------
0001       30-JAN-2009  Alpha   1000     50.00
0003       30-JAN-2009          1000    125.00
           30-JAN-2009          3000     33.33
0006       02-FEB-2009          2000     40.00
0005       31-JAN-2009  Bravo   1000     40.00
0002       30-JAN-2009  Charlie 2000    120.75
           30-JAN-2009          3000      5.00
0004       30-JAN-2009          1000    900.50

So far, so good. I would also like to hide duplicate invoice dates, so that just the first date for each invoice is shown. However, if I use this command:

BREAK ON vendor ON invoice_no ON invoice_date

It will go too far and hide the date for invoices 0003 and 0004, just because they are the same as the previous invoices from their respective vendors:

INVOICE_NO INVOICE_DATE VENDOR  ACCOUNT AMOUNT
---------- ------------ ------- ------- ------
0001       30-JAN-2009  Alpha   1000     50.00
0003                            1000    125.00
                                3000     33.33
0006       02-FEB-2009          2000     40.00
0005       31-JAN-2009  Bravo   1000     40.00
0002       30-JAN-2009  Charlie 2000    120.75
                                3000      5.00
0004                    Charlie 1000    900.50

What I'd really like is a command syntax like the following (I made up the term AND):

BREAK ON vendor ON invoice_no AND invoice_date

The intention is that, whenever it breaks on invoice_no, to also break on invoice_date (since I know that a single invoice number can't have two invoice dates):

INVOICE_NO INVOICE_DATE VENDOR  ACCOUNT AMOUNT
---------- ------------ ------- ------- ------
0001       30-JAN-2009  Alpha   1000     50.00
0003       30-JAN-2009          1000    125.00
                                3000     33.33
0006       02-FEB-2009          2000     40.00
0005       31-JAN-2009  Bravo   1000     40.00
0002       30-JAN-2009  Charlie 2000    120.75
                                3000      5.00
0004       30-JAN-2009  Charlie 1000    900.50

And now the dates are correctly displayed for invoices 0003 and 0004.

Is there any way to accomplish this in SQL*Plus?

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

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

发布评论

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

评论(3

冰雪之触 2024-07-30 22:42:10

你想要这样的东西:

列虚拟无打印
打破虚拟供应商发票号
SELECT CONCAT(供应商,发票编号)DUMMY,发票编号,发票日期,供应商,帐户,金额
来自发票
按供应商、发票编号、帐户排序

您将列 DUMMY 设置为不打印的 帐户
然后在您的选择中将其定义为您需要检查的两个字段的串联。

DUMMY 只是一个任意名称,但通常用于这样的情况,即您需要计算某些内容但不显示它

额外的“ONvendorONinvoice_no”允许您控制与虚拟休息。 同样,您可以使用 DUMMY 进行计算以获得总计等。

You want something like this:

column DUMMY NOPRINT
BREAK ON DUMMY ON vendor ON invoice_no
SELECT CONCAT (vendor, invoice_no) DUMMY, invoice_no, invoice_date, vendor, account,amount
FROM invoice
ORDER BY vendor, invoice_no, account

where you are setting the column DUMMY as not printed
and then defining it in your select as a concatenation of the two fields you need to check.

DUMMY is just an arbitrary name, but common use for cases like this where you need to calculate something but not display it

The extra "ON vendor ON invoice_no" allow you to control dups on those cols separate from the DUMMY break. Similarly you could compute using DUMMY to get totals etc.

栖竹 2024-07-30 22:42:10

您可以 BREAK ON 一个表达式,因此通过将感兴趣的字段转换为字符串并将它们与 || 连接起来,您应该能够组合一个表达式来让您继续“他们的价值观”。

You can BREAK ON an expression, so by turning the fields of interest into strings and concatenating them with || you should be able to put together an expression that lets you break on "both of their values".

深海少女心 2024-07-30 22:42:10

SQL*Plus 似乎无法做到这一点。 当发票号码为空时,我最终使用 sed 来删除发票日期:

sed -re 's/^( {11})[0-9A-Z-]{11}(.+)$/\1           \2/'

It doesn't seem like SQL*Plus can do the trick. I ended up using sed to get rid of the invoice dates whenever the invoice number was blank:

sed -re 's/^( {11})[0-9A-Z-]{11}(.+)$/\1           \2/'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文