在SELECT查询中
SELECT b.id, MIN(IFNULL(a.views,0)) AS counted
FROM table1 a JOIN table2 b ON a.id=b.id GROUP BY id
HAVING counted>0
Run Code Online (Sandbox Code Playgroud)
我怎么能拒绝此查询UPDATE作为
UPDATE b.number = counted
Run Code Online (Sandbox Code Playgroud) 我正在运行 SQL Server 2014
我有一个看起来像这样的表:
ID | Name | AddressType | Address | Features
========================================================
1 | Bob | Home | 123 Nope St | JP
2 | John | Work | 555 Fake St | MNGF
2 | John | Home | 654 Madeup Ln | IMP JP
3 | Kim | Work | 92 Nadda Blvd | MP
Run Code Online (Sandbox Code Playgroud)
我正在尝试编写一个 SQL Server 查询来查找重复的 ID,并始终返回包含“工作”地址的行,但我希望它将两行中的功能连接起来,以便得到如下所示的结果:
ID | Name | AddressType | Address | Features
========================================================
1 | Bob | Home …Run Code Online (Sandbox Code Playgroud) Oracle SQL 中有一项技术可用于简化聚合查询:
聚合特定列,但使用 SELECT 列表中的简单计算列从不同列获取信息。
--Oracle
--For a given country, what city has the highest population? (where the country has more than one city)
--Include the city name as a column.
select
country,
count(*),
max(population),
any_value(city) keep (dense_rank first order by population desc) --<<--
from
cities
group by
country
having
count(*) > 1
Run Code Online (Sandbox Code Playgroud)
如上所示,以下列可以带入城市名称,即使城市名称不在 GROUP BY 中:
any_value(city) keep (dense_rank first order by population desc)
Run Code Online (Sandbox Code Playgroud)
有多种方法可以使用 SQL 来实现此类操作。我正在 PostgreSQL 中寻找一种解决方案,让我可以在计算列中完成此操作 - 所有这些都在单个 SELECT 查询中(没有子查询、联接、WITH 等)。
问题:PostgreSQL 中是否有与 Oracle …
我想计算两列,但需要在一行中获取第一列的结果。
SELECT country, COUNT(*)
FROM table1
GROUP BY country, type
Run Code Online (Sandbox Code Playgroud)
这个查询给了我
country type COUNT(*)
Canada first 22
Canada second 42
Canada third 15
Australia second 23
Australia third 18
Run Code Online (Sandbox Code Playgroud)
但我需要得到
country type_first type_second type_third
Canada 22 42 15
Australia 23 18 0
Run Code Online (Sandbox Code Playgroud)
因为我想用这些值更新另一个表,并且使用这个行结构,我可以根据上述查询逐行更新国家/地区表。
UPDATE country SET first=x, second=x, third=x
Run Code Online (Sandbox Code Playgroud)
注意:type列ENUM具有预定义的值。
我有一个需要很长时间才能执行的查询(1.13 秒)。这两个表user_articles,并articles有大约25,000记录。这是查询:
SELECT `user_articles`.*
FROM `user_articles`
INNER JOIN `articles` ON (`user_articles`.`article_id` = `articles`.`id`)
GROUP BY `articles`.`id`
ORDER BY `user_articles`.`created_at` DESC
Run Code Online (Sandbox Code Playgroud)
我发现通过删除ORDER BY语句可以加快速度(0.003s)。这些是从结果EXPLAIN
+----+-------------+---------------+--------+---------------+------------+---------+-------------+-------+----------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+---------------+--------+---------------+------------+---------+-------------+-------+----------------------------------------------+
| 1 | SIMPLE | articles | index | PRIMARY | PRIMARY | 4 | NULL | 22678 | Using index; Using temporary; Using filesort |
| 1 …Run Code Online (Sandbox Code Playgroud) 在这个查询中:
SELECT col1, col2, col3
FROM table1
GROUP BY col1
Run Code Online (Sandbox Code Playgroud)
每行包括 的第一个值col2和col3的每个唯一值col1。
如何选择MAX和MIN的每个值col1。
就像是:
SELECT col1, minimum of col2, maximum of col2, minimum of col3, maximum of col3
Run Code Online (Sandbox Code Playgroud) 我有一个如下所示的查询:
Select t1.field1, t2.field2, sum(t1.field3)
From t1 inner join t2 on t1.id = t2.id
Group by t1.field1, t2.field2
Run Code Online (Sandbox Code Playgroud)
它被简化了,通常上面有更多的连接和属性。我现在的问题是,将 TOP N 子句添加到查询而不是在 N 行返回到我的 C# 代码后停止查询的影响有多大。
我知道,添加该条款对服务器更好,但我对粗略估计该条款有多大影响很感兴趣。我的猜测是,因为我有一个聚合,服务器无论如何都必须处理所有行,因此影响不是那么大。
编辑:C# Snippet 我们如何停止执行
var cmd = new SqlCommand();
cmd.CommandText = sqlStatement;
cmd.Connection = connectionString;
SqlDataReader dr;
dr = cmd.ExecuteReader();
while (dr.HasRows)
{
while (dr.Read())
{
MyDataRow newRow = new MyDataRowDataRow();
newRow.Parse(dr); //Parse the value from datarow to our format
result.Add(newRow);
if (topN.HasValue)
{
if (result.Count >= topN.Value)
{
break; //stop reading!
}
} …Run Code Online (Sandbox Code Playgroud) 我有一个带有人口值的点网格。每个点都有一个 id 和人口值。我也有一个 state_id,它说明了要点是什么状态。
现在我想计算每个州的百分位数 ntile(100)。
SELECT id, population, state_id,
ntile(100) OVER(ORDER BY car20) as percentile
FROM avi_threshold01
Run Code Online (Sandbox Code Playgroud)
当我使用它时,我认为它会计算所有点和状态的 ntile。
我有 4 个子查询,每个子查询都按“组名”分组。尝试将每个子查询作为一个列,全部按“组名”分组。这是查询:
select
coalesce(co.group_name, requests.group_Name, incidents.group_Name, problems.group_Name) as 'SD Groups'
, isnull(co.co, '') as 'CO'
, isnull(incidents.incidents, '' ) as 'Inc'
, isnull(problems.problems, '') as 'Prob'
, isnull(requests.requests, '') as 'Rqst'
from
(
select
groups.last_name AS Group_Name
,count(chg_ref_num) AS 'CO'
from chg
left join ca_contact groups
on chg.group_id = groups.contact_uuid
left join ca_contact assignee
on chg.assignee = assignee.contact_uuid
left join ca_company cc
on assignee.company_uuid = cc.company_uuid
where
groups.last_name in ('8197 Qlikview Support', '8202 OBIEE-BIP'
, '8205 BI SAS', '8206 …Run Code Online (Sandbox Code Playgroud) 此代码仅返回具有评级的帖子。如果帖子没有评级,我如何获取所有具有空值的帖子?
select
posts.id,
posts.user_id,
posts.name,
posts.created_at,
ratings.post_id,
ratings.avg,
ratings.count
from
posts
join (
select
post_id,
avg(rating) as avg,
count(rating) as count
from
ratings
group by
ratings.post_id
) ratings
on ratings.post_id = posts.id
Run Code Online (Sandbox Code Playgroud) group-by ×10
mysql ×5
join ×3
aggregate ×2
postgresql ×2
select ×2
sql-server ×2
update ×2
limits ×1
mysql-5.5 ×1
null ×1
optimization ×1
oracle ×1
order-by ×1
performance ×1
query ×1