如何查询指定一行上下2行数据

各位大侠好:
     我有这样一个需求,就是把 alert 日志做成了外部表,表上只有一个字段‘text’ 对应alert日志中的每一行,以方便搜索ORA-开头的错误,但是ORA-开头的这一行上通常没有日期,日期在上面一两行,详情的trace路径在下面几行。所以就需要显示 ORA-开头这一行的上面两行到下面两行(也就是以这一行为中心的5行)。我用了最笨的方法写成下面这样:


with 
a as (select rownum liehao,text from alert_log ),
b as (select a.liehao lie from a where a.text like 'ORA-%')
select * from a where a.liehao in (select b.lie-2 from b)
union 
select * from a where a.liehao in (select b.lie-1 from b)
union
select * from a where a.liehao in (select b.lie from b)
union
select * from a where a.liehao in (select b.lie+1 from b)
union
select * from a where a.liehao in (select b.lie+2 from b)

但这样效率不高,union 的每个select 语句都要扫描一下外部表 alert_log

PLAN_TABLE_OUTPUT       
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
| Id  | Operation                        | Name                        | Rows  |
--------------------------------------------------------------------------------
|   0 | SELECT STATEMENT                 |           www.hkdeng.com                  |  3335K|
|   1 |  TEMP TABLE TRANSFORMATION       |                             |       |
|   2 |   LOAD AS SELECT                 |                             |       |
|   3 |    VIEW                          |                             |  8168 |
|   4 |     COUNT                        |                             |       |
|   5 |      EXTERNAL TABLE ACCESS FULL  | ALERT_LOG                   |  8168 |
|   6 |   SORT UNIQUE                    |                             |  3335K|
|   7 |    UNION-ALL                     |                             |       |
|   8 |     HASH JOIN                    |                             |   667K|
|   9 |      VIEW                        |                             |  8168 |
|  10 |       TABLE ACCESS FULL          | SYS_TEMP_0FD9D6799_DDEA6950 |  8168 |
|  11 |      VIEW                        |                             |  8168 |
|  12 |       COUNT                      |                             |       |
|  13 |        EXTERNAL TABLE ACCESS FULL| ALERT_LOG                   |  8168 |
|  14 |     HASH JOIN                    |                             |   667K|
|  15 |      VIEW                        |                             |  8168 |
|  16 |       TABLE ACCESS FULL          | SYS_TEMP_0FD9D6799_DDEA6950 |  8168 |
|  17 |      VIEW                        |                             |  8168 |
|  18 |       COUNT                      |                             |       |
|  19 |        EXTERNAL TABLE ACCESS FULL| ALERT_LOG                   |  8168 |
|  20 |     HASH JOIN                    |                             |   667K|
|  21 |      VIEW                        |                             |  8168 |
|  22 |       TABLE ACCESS FULL          | SYS_TEMP_0FD9D6799_DDEA6950 |  8168 |
|  23 |      VIEW                        |                             |  8168 |
|  24 |       COUNT                      |                             |       |
|  25 |        EXTERNAL TABLE ACCESS FULL| ALERT_LOG                   |  8168 |
|  26 |     HASH JOIN                    |                             |   667K|
|  27 |      VIEW                        |                             |  8168 |
|  28 |       TABLE ACCESS FULL          | SYS_TEMP_0FD9D6799_DDEA6950 |  8168 |
|  29 |      VIEW                        |                             |  8168 |
|  30 |       COUNT                      |                             |       |
|  31 |        EXTERNAL TABLE ACCESS FULL| ALERT_LOG                   |  8168 |
|  32 |     HASH JOIN                    |                             |   667K|
|  33 |      VIEW                        |                             |  8168 |
|  34 |       TABLE ACCESS FULL          | SYS_TEMP_0FD9D6799_DDEA6950 |  8168 |
|  35 |      VIEW                        |                             |  8168 |
|  36 |       COUNT                      |                             |       |
|  37 |        EXTERNAL TABLE ACCESS FULL| ALERT_LOG                   |  8168 |
--------------------------------------------------------------------------------



各位高手请指教,查出列号后,如何让这个列号的上下2条也加入到这个结果集,而不用多次查询?
Qt中的QTableView控件是用于显示数据的表格视图,但是默认情况下它不支持带有控件的上下移动一行。如果我们需要实现这个功能,可以通过自定义代理类来实现。 首先,我们需要创建一个继承自QItemDelegate的代理类,该代理类负责显示带有控件的单元格。在代理类的createEditor()函数中,我们可以创建一个自定义的控件,并将其返回。在setModelData()函数中,我们可以更新模型中的数据。 其次,在主窗口中,我们需要为表格视图设置代理类。我们可以使用setCellWidget()函数将控件添加到指定的单元格中。然后,使用selectionModel()函数获取当前选中的号,然后通过移动数据模型中的来实现上下移动一行的效果。 下面是一个简单的示例代码,用于演示如何实现带有控件的上下移一行: ```cpp #include <QtWidgets> class CustomDelegate : public QItemDelegate { public: QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override { if(index.column() == 0) // 只在第一列添加控件 { QLineEdit *editor = new QLineEdit(parent); return editor; } return QItemDelegate::createEditor(parent, option, index); } void setEditorData(QWidget *editor, const QModelIndex &index) const override { if(index.column() == 0) // 只设置第一列控件的数据 { QString text = index.model()->data(index, Qt::EditRole).toString(); QLineEdit *lineEdit = static_cast<QLineEdit*>(editor); lineEdit->setText(text); return; } QItemDelegate::setEditorData(editor, index); } void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override { if(index.column() == 0) // 只更新第一列控件的数据 { QLineEdit *lineEdit = static_cast<QLineEdit*>(editor); QString text = lineEdit->text(); model->setData(index, text, Qt::EditRole); return; } QItemDelegate::setModelData(editor, model, index); } }; int main(int argc, char *argv[]) { QApplication a(argc, argv); QStandardItemModel model(4, 2); QTableView tableView; tableView.setModel(&model); CustomDelegate delegate; for (int row = 0; row < 4; ++row) { for (int column = 0; column < 2; ++column) { QModelIndex index = model.index(row, column, QModelIndex()); tableView.setIndexWidget(index, delegate.createEditor(&tableView, {}, index)); } } QItemSelectionModel *selectionModel = tableView.selectionModel(); QObject::connect(selectionModel, &QItemSelectionModel::currentRowChanged, [&](const QModelIndex &current, const QModelIndex &previous) { if(current.row() > previous.row()) { model.insertRow(current.row() + 1); model.setData(model.index(current.row() + 1, 0), "New Row"); model.removeRow(previous.row()); tableView.scrollTo(current); } else if(current.row() < previous.row()) { model.insertRow(current.row()); model.setData(model.index(current.row(), 0), "New Row"); model.removeRow(previous.row() + 1); tableView.scrollTo(current); } }); tableView.show(); return a.exec(); } ``` 以上代码创建了一个带有42列的表格视图,并为每个单元格设置了一个QLineEdit控件。当选中一行并上下移动时,模型中的数据会相应地更新,并自动滚动到新的选中。 希望以上内容能对你有所帮助,如有其他问题,请多多指教。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值