我的观察表是这样的:
position price orderid buylog otherlog
1 15 100 08:00 08:01
2 15 100 08:00 08:02
2 15 100 08:00 08:05
2 15 100 08:00 08:02
2 15 101 08:10 08:15
2 15 101 08:10 08:12
2 15 102 08:20 08:25
2 15 103 08:30 08:31
2 15 103 08:30 08:32
2 15 103 08:30 08:33
预期结果:
position price orderid buylog otherlog
1 15 100 08:00 08:01
2 15 100 08:00 08:05
2 15 101 08:10 08:15
2 15 102 08:20 08:25
2 15 103 08:30 08:33
这只是迈出的一步,我想,我真正想要的是:
position price
1 15
2 60
所以我需要概述每个职位的总付款。
但是现在,我需要的是:对于每个位置和orderid,我只想要具有最高其他日志条目的行。
现在有其他日志时间低于buylog-times,但我只是通过buylog < otherlog
过滤掉了它们。
但是现在我不知道如何只显示每个orderid组中最高的其他日志。我尝试使用max(otherlog)
,但它仍会输出第一个表格。
这是将三个表连接在一起后的视图,我希望在同一个查询中得到预期的结果。
查询是这样的:
select position,price,orderid,buylog,otherlog
from table1 inner join table2 on t1.userid=t2.userid
inner join table3 on t2.id=t2.id
where (some conditions to narrow down the results)
我正在使用ms sql server 2012。
//修改
查询:
Use [dbname]
go
with cte
as ( select olt.position,
ot.price,
ot.orderid,
ot.buylog = min(ot.buylog) over (partition by olt.position,ot.orderid),
olt.otherlog = max(olt.otherlog) over (partition by olt.position,ot.orderid),
rn=row_number() over(partition by olt.position, order by olt.position)
from ordertable as ot inner join anothertable as at
on ordertable.userid=anothertable.userid
inner join otherlogtable as olt on anothertable.id=otherlogtable.sessionlogid
)
select
olt.position,
ot.price,
ot.orderid,
ot.buylog,
olt.otherlog
from
cte
where
rn=1
答案 0 :(得分:0)
这应该可行(虽然不确定列的来源):
WITH cte
AS (SELECT position,
price,
orderid,
buylog = Min(buylog)
OVER(
partition BY position,orderid),
otherlog = Max(otherlog)
OVER(
partition BY position,orderid),
rn = Row_number()
OVER(
partition BY position,orderid
ORDER BY position)
FROM T1)
SELECT position,
price,
orderid,
buylog,
otherlog
FROM cte
WHERE rn = 1
这里是DEMO
修改以下是您加入的完整查询,将您编辑过的问题考虑在内:
WITH cte
AS (SELECT olt.position,
ot.price,
ot.orderid,
ot.buylog = Min(ot.buylog)
OVER (
partition BY olt.position, ot.orderid),
olt.otherlog = Max(olt.otherlog)
OVER (
partition BY olt.position, ot.orderid),
rn=Row_number()
OVER(
partition BY olt.position
ORDER BY olt.position)
FROM ordertable AS ot
INNER JOIN anothertable AS at
ON ordertable.userid = anothertable.userid
INNER JOIN otherlogtable AS olt
ON anothertable.id = otherlogtable.sessionlogid)
SELECT position,
price,
orderid,
buylog,
otherlog
FROM cte
WHERE rn = 1
问题是:
CTE
上的外部选择中移除表名称(例如SELECT position
而不是SELECT olt.position
)PARTITION BY
部分ROW_NUMBER
和ORDER BY
之间的逗号:rn=Row_number()
OVER(
partition BY olt.position
ORDER BY olt.position)
答案 1 :(得分:0)
试试这个:
with cte
as ( select olt.position,
ot.price,
ot.orderid,
log1 = min(ot.buylog) over (partition by olt.position,ot.orderid),
log2 = max(olt.otherlog) over (partition by olt.position,ot.orderid),
rn=row_number() over(partition by olt.position order by olt.position)
from
ordertable as ot
inner join anothertable as at on ordertable.userid=anothertable.userid
inner join otherlogtable as olt on anothertable.id=otherlogtable.sessionlogid
)
select position, price, orderid,
log1 ALIASNAME1, log2 ALIASNAME2
from cte where rn=1