我有一个包含三个表的数据库items,parameters并且measurements在两个服务器中都想查询该measuerment表。但是在 PostgeSQL (9.4) 和 SQL Server (2012) 中查询要慢得多。
measurements:
column | type | attributes
---------------+----------------------+-----------------------------------------------------------
id | int/serial | (identity) primary key
measuretime | datetime/timestamp | not null
parameter_id | int | not null (foreign key) references parameters(id)
item_id | int | not null (foreign key) references items(id)
value | float | not null
Run Code Online (Sandbox Code Playgroud)
和两个nonclustered index上measuretime和parameter_id
我插入了 2.609.280 行items(半年,每行间隔 5 秒)和 31.311.360 行measurements …
我有一个查询,其中链接了两列。为什么别名不能在工作中的GROUP BY条款,但ORDER BY条款,它的工作?如何正确编写选择?
SELECT
KOS_VER_ID AS "Vertrag"
, WHR_ISO_3_CODE AS "Waehrung"
, KOS_KOA_ST_KZN || ' - ' || ST_LANGBEZ_EN as "Kostenart"
, SUM (KOS_BETRAG) AS "Summe pro KOA"
FROM
KOSTEN
, WAEHRUNG
, SCHLUESSELTABELLE
WHERE
KOSTEN.KOS_VERHI_WHR_ID = WAEHRUNG.WHR_ISO_ID
AND KOSTEN.KOS_KOA_ST_KZN = SCHLUESSELTABELLE.ST_ID
AND KOS_VER_ID in (2509, 2510, 2511)
GROUP BY
KOS_VER_ID
, WHR_ISO_3_CODE
, KOS_KOA_ST_KZN || ' - ' || ST_LANGBEZ_EN
ORDER BY
"Vertrag"
, "Kostenart"
;
Run Code Online (Sandbox Code Playgroud) 我需要从我们的 MySQL-DB 中获取一些统计信息,并想知道是否有更快的方法来做到这一点。
基本上,在按属性进行分隔之前,我需要有关任何给定月份的用户注册信息。
例如:
SELECT count(*) AS c
FROM `users`
WHERE `date_created` LIKE '2015-10%'
AND `country` = 'DE'
Run Code Online (Sandbox Code Playgroud)
这将返回 2015 年 10 月的数字。如何更改此语句以在一个语句中返回所有月份的结果?
我也有兴趣添加类似AND confirmed = 1单独的结果。
仅使用一个 MySQL 语句就可以做到这一点吗?
此问题与以下 2 个 SQL 错误有关(我添加的换行符)
MySql.Data.MySqlClient.MySqlException (0x80004005):
Expression #2 of ORDER BY clause is not in SELECT list, references column 'auitool2014.a.prog' which is not in SELECT list;
this is incompatible with DISTINCT
MySql.Data.MySqlClient.MySqlException (0x80004005):
Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'H.C51' which is not functionally dependent on columns in GROUP BY clause;
this is incompatible with sql_mode=only_full_group_by
Run Code Online (Sandbox Code Playgroud)
我的问题很容易描述,即使没有发布原始 SQL。我有一个5.7.11-log在 Windows 上运行的 Mysql服务器,我可以SELECT A,B GROUP BY A并且SELECT …
我们都知道一个简单的语句例如:
SELECT * FROM stuff;
Run Code Online (Sandbox Code Playgroud)
应该不会产生有序的结果。然而,当我试图证明这一点时,它总是以主键顺序出现。
此外还有一个声明,例如:
SELECT thing,whatever FROM stuff
GROUP BY thing,whatever;
Run Code Online (Sandbox Code Playgroud)
似乎总是按GROUP BY子句中的最后一个字段对事物进行排序,这根本没有帮助。
问题是,在什么情况下 SQL SERVER 会在未询问的情况下对结果进行排序,我该怎么做才能阻止这种情况?
我试图向我的学生证明,除非指定,否则顺序是不确定的,但这对我的情况没有帮助。
我承认我正在处理一小组样本数据。
谢谢
我有一张ROAD桌子:
+----+------------+
| ID | ROAD_CLASS |
+----+------------+
| 1 | ARTERIAL A |
| 2 | ARTERIAL B |
| 3 | ARTERIAL B |
| 4 | ARTERIAL C |
| 5 | ARTERIAL C |
| 6 | ARTERIAL C |
| 7 | COLLECTOR |
| 8 | COLLECTOR |
| 9 | LOCAL |
| 10 | LOCAL |
+----+------------+
Run Code Online (Sandbox Code Playgroud)
该ROAD_CLASS字段的数据类型是NVARCHAR2.
我想创建一个视图,将所有主干道路分组到一个ARTERIAL类别中,但将其他道路类保留原样:
+------------+
| ROAD_CLASS |
+------------+ …Run Code Online (Sandbox Code Playgroud) 我需要建立一个梦幻足球游戏用户排行榜。游戏的简化数据库如下:
users必须squad_players在squads每一个matches(由相关transfer_period)players有match_points每个matchessquad_players有位置,优先。该优先级是如果换人的顺序squad_players不会出现在matches squads具有formations确定从优先级排序的每个位置中选择的最大玩家数量数据库为MySQL 5.6,最大数量users为10K。
我能够内部加入(按顺序)squad_players, squads, matches,match_points以获得每个squad_players玩过的人(players没有玩过的人没有match_points)的观点。
我奋力SUM的分X squad_players每squads其中X是formations通过确定位置的的squad_players。
我试图通过具有相关子查询的group by修改 …
mysql database-design group-by mysql-5.6 greatest-n-per-group
我添加了一个不使用窗口函数的解决方案和一个基准测试,其中包含一个低于 Martin's Answer 的大数据集
这是GROUP BY 使用不在 SELECT 列表中的列的后续线程- 这什么时候实用、优雅或强大?
在我对这一挑战的解决方案中,我使用了一个查询,该查询按不属于选择列表的表达式进行分组。当逻辑分组元素涉及来自其他行的数据时,这经常与窗口函数一起使用。
也许这是一个矫枉过正的例子,但我认为你可能会发现挑战本身很有趣。我会等待发布我的解决方案,也许你们中的一些人可以提出更好的解决方案。
我们有一个定期记录读数值的传感器表。无法保证采样时间处于单调间隔。
您需要编写一个查询来报告“异常”,这意味着传感器报告的读数超出阈值的次数,无论是低还是高。传感器报告超过或低于阈值的每个时间段都被视为“例外”。一旦读数恢复正常,异常结束。
该脚本采用 T-SQL 格式,是我的培训材料的一部分。
------------------------------------------
-- Sensor Thresholds - 1 - Setup Example --
------------------------------------------
CREATE TABLE [Sensors]
(
[Sensor] NVARCHAR(10) NOT NULL,
[Lower Threshold] DECIMAL(7,2) NOT NULL,
[Upper Threshold] DECIMAL(7,2) NOT NULL,
CONSTRAINT [PK Sensors]
PRIMARY KEY CLUSTERED ([Sensor]),
CONSTRAINT [CK Value Range]
CHECK ([Upper Threshold] > [Lower Threshold])
);
GO
INSERT INTO [Sensors]
(
[Sensor] , …Run Code Online (Sandbox Code Playgroud) 我有这个数据:
CREATE TABLE tickets(user_id int NOT NULL);
INSERT INTO tickets VALUES (1);
INSERT INTO tickets VALUES (2);
INSERT INTO tickets VALUES (3); -- 3 times
INSERT INTO tickets VALUES (4); -- 10 times
Run Code Online (Sandbox Code Playgroud)
现在我想显示每个用户的票数百分比。
我试过这个:
WITH number_of_tickets AS (
SELECT user_id, COUNT(user_id) AS number_of_tickets_per_user
FROM tickets
GROUP BY user_id
)
SELECT
number_of_tickets_per_user,
ROUND((COUNT(user_id) * 100.0) / (SELECT COUNT(DISTINCT(user_id)) FROM tickets), 3) -- No no no no
FROM number_of_tickets
GROUP BY number_of_tickets_per_user
ORDER BY number_of_tickets_per_user;
Run Code Online (Sandbox Code Playgroud)
但我可能不能很好地处理百分比计算。结果总是显示每个用户每个票数的 25%。
谢谢
仅当分区中存在负值时,我才需要将值相加。如果分区中没有负值,它应该只输出行。
这就是我现在所拥有的。初始数据作为 CTE 提供。
数据管理语言
;with ledger as (
select accountId, type, amount
from (
values
(1, 'R', -10)
,(1, 'V', 10)
,(1, 'R', 30)
,(2, 'R', 20)
,(2, 'R', -5)
,(2, 'V', 5)
,(3, 'R', 20)
,(3, 'R', 30)
) x (accountId, type, amount)
)
,b as ( --identifies accountid, type with negatives
select
accountid
,type
from ledger
group by accountid, type
having min(amount) < 0
)
,onlyPositives as (
select
l.accountid
,l.type
,l.amount
from ledger l
left join …Run Code Online (Sandbox Code Playgroud) sql-server aggregate window-functions group-by sql-server-2016
group-by ×10
mysql ×3
oracle ×2
postgresql ×2
sql-server ×2
aggregate ×1
alias ×1
case ×1
distinct ×1
like ×1
mysql-5.6 ×1
mysql-5.7 ×1
oracle-12c ×1
order-by ×1
performance ×1
select ×1
syntax ×1
t-sql ×1