由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Database版 - 这个query对么?
相关主题
请教一个query in mysqlLate afternoon 腦不好使
问个问题请教怎么来log duration of a MYSQL procedure?
A sql question求助:sql server 2000, 这句话怎么写?
如何在数据库中进行复杂查询, 但不把中间结果放到程序内存[转载] what's wrong with this PL/SQL
请教一个小白Query 问题sql question
请教oracle sql query questionask for help
[转载] Can anyone interpret this simple SQL?求助:如何ColumName 作为变量放入query
急,SQL2005, 怎么查过去一小时里run过的所有query?oracle和XML
相关话题的讨论汇总
话题: select话题: date话题: weather话题: join话题: tem
进入Database版参与讨论
1 (共1页)
m******u
发帖数: 12400
1
table Weather(id,date, temperature)
question:find the day whose temperature is higher than its previous day (
yesterday).
Select w2.id, w2.date
from weather w1
left outer join weather w2
on w2.id = w1.id + 1
where w2.temp > w1.temp
看上去很别扭,这不像是left outer join,select clause 中没有一个是从left
table里来的。而且 join之后再select这个where condition到底怎么说?在join之后
的虚拟表中它们是在一行内的(如右图)
id date tem id date tem id date tem
1 1/1 2 1 1/1 2 2 1/2
3
2 1/2 3 2 1/2 3 3 1/3
2
3 1/3 2 3 1/3 2 4 1/4
3
4 1/4 3 4 1/4 3 5 1/5
2
5 1/5 2 5 1/5 2 6 1/6
3
6 1/6 3 6 1/6 3 null null
null
预期答案应该是: id date
2 1/2
4 1/4
6 1/6
m******u
发帖数: 12400
2
I can run the query. But I just don't like the query.
m*********a
发帖数: 3299
3
try this (date function may be wrong in your sql system):
Select w2.id, w2.date
from weather w1 join weather w2
on w2.id = w1.id
where w2.temp > w1.temp and w2.date = w1.date +1

【在 m******u 的大作中提到】
: I can run the query. But I just don't like the query.
m******u
发帖数: 12400
4
你说的对,date作为column head要加中括号。你的query更好一些。我的实在是太难看
了。
m*********a
发帖数: 3299
5
我的SQL没有正式学过,就是工作中有很多的server side 的SQL
所以我不是dba,就是一个用到简单SQL 的吗农
我没有看懂你说的date要加括号是啥意思
这个query在你的系统,我猜是microsoft sql server中可以run不?

【在 m******u 的大作中提到】
: 你说的对,date作为column head要加中括号。你的query更好一些。我的实在是太难看
: 了。

m******u
发帖数: 12400
6
date在SQL Server里是一种数据类型,所以是保留字,不能用于变量名(这里也即列名
)要用作列名要加个中括号。你的query在sql server中不work。
发信人: mdrosophila (ranger), 信区: Database
标 题: Re: 这个query对么?
发信站: BBS 未名空间站 (Tue Jun 23 22:15:03 2015, 美东)
我的SQL没有正式学过,就是工作中有很多的server side 的SQL
所以我不是dba,就是一个用到简单SQL 的吗农
我没有看懂你说的date要加括号是啥意思
这个query在你的系统,我猜是microsoft sql server中可以run不?
m*********a
发帖数: 3299
7
我不知道为啥不行,你可以用下面三种方法escape reserved word in sql.
w1.date已经是escape了
你能说说,咋样写是对的?
date is a reserved keyword and hence cannot be used like
SELECT date from some table
there can be multiple solutions for the problem
The date column needs to be enclosed within the brackets like
SELECT [date] FROM tableName
Enclose the reserved keyword in backticks
SELECT 'date' from tableName
Use alias
SELECT tableName.date from tableName

【在 m******u 的大作中提到】
: date在SQL Server里是一种数据类型,所以是保留字,不能用于变量名(这里也即列名
: )要用作列名要加个中括号。你的query在sql server中不work。
: 发信人: mdrosophila (ranger), 信区: Database
: 标 题: Re: 这个query对么?
: 发信站: BBS 未名空间站 (Tue Jun 23 22:15:03 2015, 美东)
: 我的SQL没有正式学过,就是工作中有很多的server side 的SQL
: 所以我不是dba,就是一个用到简单SQL 的吗农
: 我没有看懂你说的date要加括号是啥意思
: 这个query在你的系统,我猜是microsoft sql server中可以run不?

m******u
发帖数: 12400
8
已经做了相应改动:将date加中括号,还把最后一行换成了id相减(date类型不能相减
),但仍不能run。我的那个query能run,并能给出正确结果。
m*********a
发帖数: 3299
9
我看了你的数据,我发现错在哪个地方,原来我以为id是一个area code
比如chicago, new york, LA啥的,所以要找到area code 一样的
你这个表只有一个 area, id只不过是serial number。
但是你不知道id+1是id的后一天,这个假设不一定对,也不需要要left join
用下面的join就行了
Select w2.id, w2.date
from weather w1 join weather w2
where w2.temp > w1.temp and w2.date = w1.date +1

【在 m******u 的大作中提到】
: 已经做了相应改动:将date加中括号,还把最后一行换成了id相减(date类型不能相减
: ),但仍不能run。我的那个query能run,并能给出正确结果。

m******u
发帖数: 12400
10
Finally I got this query. It works and is not ugly.
;with wCTE (ysd_id,ysd_temp,td_id, td_temp)
as(
select w1.id, w1.temp, w2.id, w2.temp
from weather w1
full join weather w2
on w1.id = w2.id -1
)
select weather.date
from weather
inner join wCTE
on wCTE.td_id = weather.id
where wCTE.td_temp > wCTE.ysd_tem
m******u
发帖数: 12400
11
其实是一样的。改改,就不是那么难看了。
select w2.[date] as [day with temperature higher than yesterday]
from weather w1
full join weather w2
on w1.id = w2.id - 1
where w2.temp > w1.temp
n***l
发帖数: 143
12
using lag() over() to access data from previous row:
WITH LagDate as (
SELECT ID, [date], temp,
LAG (temp, 1, 0) over (order by [Date] ASC) as LagTemp
FROM Weather
)
SELECT id, [date] AS [Dates with temp higher than previous day], temp
FROM LagDate
WHERE temp > LagTemp
m******u
发帖数: 12400
13
thanks. I like your query.
发信人: ntycl (你太有才了), 信区: Database
标 题: Re: 这个query对么?
发信站: BBS 未名空间站 (Mon Jul 6 23:43:31 2015, 美东)
using lag() over() to access data from previous row:
WITH LagDate as (
SELECT ID, [date], temp,
LAG (temp, 1, 0) over (order by [Date] ASC) as LagTemp
FROM Weather
)
SELECT id, [date] AS [Dates with temp higher than previous day], temp
FROM LagDate
WHERE temp > LagTemp
1 (共1页)
进入Database版参与讨论
相关主题
oracle和XML请教一个小白Query 问题
How to query a tree请教oracle sql query question
a complex sql query, high hand help!!![转载] Can anyone interpret this simple SQL?
java sql help!急,SQL2005, 怎么查过去一小时里run过的所有query?
请教一个query in mysqlLate afternoon 腦不好使
问个问题请教怎么来log duration of a MYSQL procedure?
A sql question求助:sql server 2000, 这句话怎么写?
如何在数据库中进行复杂查询, 但不把中间结果放到程序内存[转载] what's wrong with this PL/SQL
相关话题的讨论汇总
话题: select话题: date话题: weather话题: join话题: tem