AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / dba / 问题

问题[query](dba)

Martin Hope
Pantea
Asked: 2024-12-13 16:45:27 +0800 CST

更好地“透视”表格数据

  • 5

我有一张具有以下结构的表格:

create table test_table
(queuename    number,
 duration_sum number,
 rating_sum   number,
 rating_avg   number,
 rating_cnt   number
 )

以下是示例数据:

insert into test_table (queuename, duration_sum, rating_sum, rating_avg,rating_cnt)
values (1000,50,40,60,70);
insert into test_table (queuename, duration_sum, rating_sum, rating_avg,rating_cnt)
values (1010,12,40,25,34);
insert into test_table (queuename, duration_sum, rating_sum, rating_avg,rating_cnt)
values (2000,50,34,60,23);
insert into test_table (queuename, duration_sum, rating_sum, rating_avg,rating_cnt)
values (3000,90,40,60,67);
commit;

我需要的是以下结果:

 queuename         1000     1010     2000     3000     
 duration_sum       50       12       50       90 
 rating_sum         40       40       34       40 
 rating_avg         60       25       60       60  
 rating_cnt         70       34       23       67 

我正在尝试使用pivot它,这就是我迄今为止所写的:

select *
from (
    select 
        queuename,
        'duration_sum' as measure, duration_sum as value from test_table
    union all
    select 
        queuename,
        'rating_sum' as measure, rating_sum as value from test_table
    union all
    select 
        queuename,
        'rating_avg' as measure, rating_avg as value from test_table
    union all
    select 
        queuename,
        'rating_cnt' as measure, rating_cnt as value from test_table
         )
pivot (
    max(value)
    for queuename in (1000 as "1000", 1010 as "1010", 2000 as "2000", 3000 as "3000")
       )
order by measure;

这是一种更好的结果方法吗?我猜使用语句可能比使用pivoting有更好的方法。我应该说“queuename”是唯一的。pivotunion

提前致谢。

query
  • 1 个回答
  • 29 Views
Martin Hope
James Madison
Asked: 2024-02-23 01:04:29 +0800 CST

Snowflake 不允许重命名分析函数

  • 7
这个问题是从超级用户 迁移的,因为它可以在数据库管理员堆栈交换上得到回答。16 小时前迁移 。

在下面的代码中,我可以毫无问题地执行 LAG 和 PARTITION BY 作为查询。但我需要它作为一张桌子。如果我执行 CTAS,我需要在列上使用别名。但是当我尝试为其添加别名时,它失败了。我认为这是有效的 SQL。但我正在使用Snowflake,所以我向超级用户询问,因为我担心这是供应商问题。

如何使用该查询的输出创建一个表?

drop table a;

create table a (
    a1 varchar,
    a2 varchar
);

insert into a values ( 'a', 1 );
insert into a values ( 'a', 3 );
insert into a values ( 'a', 5 );
insert into a values ( 'a', 9 );
insert into a values ( 'b', 1 );
insert into a values ( 'b', 3 );
insert into a values ( 'b', 4 );
insert into a values ( 'c', 3 );
insert into a values ( 'c', 4 );
insert into a values ( 'c', 5 );

-- This works fine.
select a1
     , a2
     , lag(a2)
  over (partition by a1 order by a2)
  from a
;

-- This fails.
select a1
     , a2
     , lag(a2) new_col_name
  over (partition by a1 order by a2)
  from a
;

-- But if I can't name the column, I can't CTAS.
create table b as
select a1
     , a2
     , lag(a2)
  over (partition by a1 order by a2)
  from a
;

输出是:

status
A successfully dropped.
status
Table A successfully created.
number of rows inserted
                      1
number of rows inserted
                      1
number of rows inserted
                      1
number of rows inserted
                      1
number of rows inserted
                      1
number of rows inserted
                      1
number of rows inserted
                      1
number of rows inserted
                      1
number of rows inserted
                      1
number of rows inserted
                      1
A1  A2  LiteralRedacted0
b   1   NULL
b   3   1
b   4   3
c   3   NULL
c   4   3
c   5   4
a   1   NULL
a   3   1
a   5   3
a   9   5
001003 (42000): SQL compilation error:
syntax error line 4 at position 2 unexpected 'over'.
002022 (42601): SQL compilation error:
Missing column specification
query
  • 2 个回答
  • 141 Views
Martin Hope
tkmagnet
Asked: 2024-02-22 05:42:25 +0800 CST

查询满足多个条件和值的单个返回

  • 7

我试图找出一种方法,当一列中满足多个条件时对 ID 进行一次计数,同时在另一列中显示相同条件的总数。对于同一 ID,第一列的条件可以多次使用,但只需计数一次。有 2 个不同的 subGrp 值(1 和 2),但仅关注其中一个。这是我正在使用的两个表的示例数据:

create table vendor (
    subGrp char(1),
    subID varchar(10),
    subStatus char(1),
    subCat char(1) );

insert into vendor (
    subGrp, subID, subStatus, subCat)
values ('1','101','A','E'),
    ('1','105','A','F'),
    ('1','127','A','F'),
    ('1','132','A','F'),
    ('1','133','D','F'),
    ('1','154','A','F'),
    ('1','162','A','F'),
    ('1','167','W','E'),
    ('1','178','A','F'),
    ('1','196','A','F'),
    ('1','200','A','E'),
    ('1','205','A','F'),
    ('1','230','A','F'),
    ('1','231','A','F'),
    ('1','237','A','F'),
    ('1','259','A','F'),
    ('1','260','A','F'),
    ('1','271','A','F');

create table customer (
    cusID varchar(10),
    prodType char(1) );

insert into customer (
    cusID, prodType
values ('101','Y'),
    ('105','H'),
    ('105','Y'),
    ('127','H'),
    ('132','H'),
    ('132','Y'),
    ('132','Y'),
    ('132','F'),
    ('132','Y'),
    ('133','H'),
    ('133','S'),
    ('154','H'),
    ('154','Y'),
    ('154','Y'),
    ('154','Y'),
    ('154','Y'),
    ('162','H'),
    ('162','Y'),
    ('162','Y'),
    ('167','J'),
    ('178','H'),
    ('196','H'),
    ('200','Y'),
    ('205','H'),
    ('205','Y'),
    ('230','H'),
    ('230','Y'),
    ('230','Y'),
    ('230','Y'),
    ('230','Y'),
    ('231','H'),
    ('237','H'),
    ('259','H'),
    ('259','Y'),
    ('260','H'),
    ('271','H');

subGrp 将位于第一列中。在下一列中,我需要计算有多少 cusID 具有 Y、F 或 J prodType,同时具有 A 的 subStatus 和 F 的 subCat。唯一多次使用的值可在 prodType 中找到。

我想要以下结果:

亚组 产品类型总计 子组总计
1 7 14

因此,在 subGrp 1 中,如果 ID 包含 Y、F 或 J 的任意组合,则有 7 个 ID 仅计数一次,并且总共 14 个 ID 具有 A subStatus 和 F subCat。

它是第三方应用程序和数据库,因此我能做的事情有点有限(例如:使用不同时抛出错误:未找到标量函数)。他们告诉我这是 MS SQL。

另外,我不确定数据类型,因为我对此并不知情,但这就是我的假设。希望这是有道理的。提前致谢。

query
  • 1 个回答
  • 35 Views
Martin Hope
Ashley
Asked: 2024-02-21 11:52:14 +0800 CST

选择随机记录,直到超过累计总数

  • 5

我有一个带有id,skill_level和duration列的视频表,我正在尝试编写一个查询,该查询将返回总持续时间超过 的不同视频的播放列表X。

select _id, duration, running_total
from (select *, sum(duration) over (order by RANDOM()) as running_total
      from videos
      where skill_level in ('beginner', 'superbeginner') AND watched IS NULL
     )
where running_total <= 7200;

这有效并返回以下内容:

5e4d11fbaac87f3820954c45|305|305
64e51ad279223a9ad03525c2|930|1235
5e4d1204aac87f3820954c64|627|1862
5e4d1212aac87f3820954c95|367|2229
641e05c2d7c1512d278dc51a|361|2590
5e4d1200aac87f3820954c57|444|3034
649aa7089776ba12d8de28d5|329|3363
5e4d123eaac87f3820954d2c|573|3936
5e4d1216aac87f3820954ca1|399|4335
5e4d120daac87f3820954c84|315|4650
5e4d11dfaac87f3820954be1|510|5160
64f7036d3d578af923ba2afc|326|5486
5e4d11fcaac87f3820954c47|437|5923
5fa51afc27a912152cc51c33|1062|6985

但理想情况下,我想返回足够多的记录,X但只能超过一条。6375274c1cff4a7e31b04a0d下面让我们结束7200。

5e4d11fbaac87f3820954c45|305|305
64e51ad279223a9ad03525c2|930|1235
5e4d1204aac87f3820954c64|627|1862
5e4d1212aac87f3820954c95|367|2229
641e05c2d7c1512d278dc51a|361|2590
5e4d1200aac87f3820954c57|444|3034
649aa7089776ba12d8de28d5|329|3363
5e4d123eaac87f3820954d2c|573|3936
5e4d1216aac87f3820954ca1|399|4335
5e4d120daac87f3820954c84|315|4650
5e4d11dfaac87f3820954be1|510|5160
64f7036d3d578af923ba2afc|326|5486
5e4d11fcaac87f3820954c47|437|5923
5fa51afc27a912152cc51c33|1062|6985
6375274c1cff4a7e31b04a0d|1138|8123

是否可以在 sqlite 中编写查询来实现此目的?

query
  • 1 个回答
  • 13 Views
Martin Hope
lifeisajourney
Asked: 2023-11-14 07:44:11 +0800 CST

使用 SQL 替换单个值中的多个单词

  • 5

我有两个表,#customer 和 #abbr。#customer 表由两列组成:“id”和“name”,而 #abbr 表包含缩写词及其相应的完整形式的条目。我的目标是将客户名称中的所有缩写替换为其各自的完整形式。例如,如果客户名称是“Object ME CT”,我希望结果是“Object Medical Control”。但是,根据我当前的查询,它会生成两行:“对象 ME 控制”和“对象医疗 CT”。我的目标是每个“id”占一行,其中所有缩写都替换为 #abbr 表中的完整形式。我怎样才能实现这个目标?谢谢你!

create table #customer
(
id int identity(1,1)
,cname varchar(100)
)

create table #abbr
(
abbr varchar(100),
fname varchar(100)
)

insert into #customer values('Assign ME CT'),('Assign ME'),('Assign CT')
insert into #abbr values ('ME','Medical'),('CT','Control'),('MMT','Metro')

select *, REPLACE(c.cname, a.abbr,a.fname)
  from #customer c
  join #abbr a
  on c.cname like '%'+a.abbr+'%'
 where id = 1
query
  • 2 个回答
  • 46 Views
Martin Hope
tkmagnet
Asked: 2023-09-08 04:46:06 +0800 CST

根据每个 ID 的值查找百分比

  • 7

我的 table1 有 2 个 ID,每个 ID 有 2 个值(Y,N)。我可以通过以下查询来计算值:

select id
 ,count(*) as "total"
 ,choice
from table1
where id in (1,8)
group by id, choice

我得到以下结果:

ID 全部的 选择
1 55 氮
1 17 号 是
8 第319章 氮
8 123 是

有没有办法编写一个查询,为我提供每个 id 的每个值 (Y,N) 的百分比?
(id1:55/55+17(N)、17/55+17(Y)等)

期望的结果:

ID 全部的 选择 百分
1 55 氮 0.236
1 17 号 是 0.764
8 第319章 氮 0.7222
8 123 是 0.278

我还需要在查询中使用 count(*) 吗?

query
  • 2 个回答
  • 24 Views
Martin Hope
Ali Günay
Asked: 2023-08-12 22:14:46 +0800 CST

给出查询的输出

  • 2
WITH NumberSequence AS
(
SELECT
1 AS Number1
, 2 AS Number2
UNION ALL
SELECT
Number2 * 2 + 1
, Number1 * 5
FROM NumberSequence
WHERE Number1 < 200
)
SELECT
*
FROM NumberSequence

Number1 Number2
   1       2
   5       5
   11      25
   51      55
   111     255
   511     555

我不明白为什么它会给出这个输出。为什么我们不将 Number1 与 5 相乘。为什么我们在数字之间交换过程。有人可以向我解释一下吗?

query
  • 1 个回答
  • 25 Views
Martin Hope
BHASKAR LAHARI
Asked: 2023-05-15 19:20:52 +0800 CST

我有列名作为日期的数据,我需要将这些列取消透视到日期列和值中。我怎样才能在 oracle SQL 开发人员中得到这个

  • 6

所以我的输入表是

在此处输入图像描述

这是将日期列转换为输出中的日期和值

在此处输入图像描述

我需要编写什么 oracle SQL 开发人员代码来实现这个输出表。请帮助我并提前致谢。

query
  • 1 个回答
  • 21 Views
Martin Hope
PaulMcF87
Asked: 2022-10-10 08:55:01 +0800 CST

用 Union 连接表然后添加人工列

  • 0

我使用union以下方法将 2 个表连接在一起

SELECT DateTime AS Date, Pupil, DateTime AS Time
FROM tableA
Where Status = 'Pending'
Union
SELECT DateTime AS Date, Pupil, DateTime AS Time
FROM tableB 
Where Status = 'Pending'
Order by DateTime

我想知道是否可以添加另一列来显示从哪个表中提取信息

IE

日期 瞳孔 时间 桌子
xx xxxxx xxxx 表A
xx xxxxx xxxx 表B
query mariadb
  • 1 个回答
  • 23 Views
Martin Hope
PaulMcF87
Asked: 2022-10-09 07:36:42 +0800 CST

选择具有今天和明天日期的条目

  • 0

我正在使用 MariaBD 并尝试过滤具有今天日期和明天日期的条目。

我已经设法使用过滤今天的日期

SELECT *, 
       DateTime AS Time 
FROM table
where status = 'Pending' 
and DateTime like concat(CURDATE(),'%') 
order by DateTime

我也在努力添加明天日期的条目。可能很容易,但注意到我正在尝试工作

我正在使用 MariaDB

query mariadb
  • 1 个回答
  • 26 Views

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目

    • 12 个回答
  • Marko Smith

    如何让sqlplus的输出出现在一行中?

    • 3 个回答
  • Marko Smith

    选择具有最大日期或最晚日期的日期

    • 3 个回答
  • Marko Smith

    如何列出 PostgreSQL 中的所有模式?

    • 4 个回答
  • Marko Smith

    列出指定表的所有列

    • 5 个回答
  • Marko Smith

    如何在不修改我自己的 tnsnames.ora 的情况下使用 sqlplus 连接到位于另一台主机上的 Oracle 数据库

    • 4 个回答
  • Marko Smith

    你如何mysqldump特定的表?

    • 4 个回答
  • Marko Smith

    使用 psql 列出数据库权限

    • 10 个回答
  • Marko Smith

    如何从 PostgreSQL 中的选择查询中将值插入表中?

    • 4 个回答
  • Marko Smith

    如何使用 psql 列出所有数据库和表?

    • 7 个回答
  • Martin Hope
    Jin 连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目 2014-12-02 02:54:58 +0800 CST
  • Martin Hope
    Stéphane 如何列出 PostgreSQL 中的所有模式? 2013-04-16 11:19:16 +0800 CST
  • Martin Hope
    Mike Walsh 为什么事务日志不断增长或空间不足? 2012-12-05 18:11:22 +0800 CST
  • Martin Hope
    Stephane Rolland 列出指定表的所有列 2012-08-14 04:44:44 +0800 CST
  • Martin Hope
    haxney MySQL 能否合理地对数十亿行执行查询? 2012-07-03 11:36:13 +0800 CST
  • Martin Hope
    qazwsx 如何监控大型 .sql 文件的导入进度? 2012-05-03 08:54:41 +0800 CST
  • Martin Hope
    markdorison 你如何mysqldump特定的表? 2011-12-17 12:39:37 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 对 SQL 查询进行计时? 2011-06-04 02:22:54 +0800 CST
  • Martin Hope
    Jonas 如何从 PostgreSQL 中的选择查询中将值插入表中? 2011-05-28 00:33:05 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 列出所有数据库和表? 2011-02-18 00:45:49 +0800 CST

热门标签

sql-server mysql postgresql sql-server-2014 sql-server-2016 oracle sql-server-2008 database-design query-performance sql-server-2017

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve