0

我的视图表是这样的:

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,我只想要具有最高 otherlog 条目的行。

现在还有其他日志时间低于购买日志时间,但我将它们过滤掉了buylog < otherlog

但是现在我不知道如何仅显示每个 orderid-group 中最高的 otherlog。我试过了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

据我所知,您可以通过电话进行。

4

2 回答 2

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 

这是一个演示

编辑所以这里是您的联接的完整查询,以考虑您编辑的问题:

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 

问题是:

  1. 从外部选择中删除表名CTE(例如SELECT position,而不是SELECT olt.position
  2. PARTITION BY删除部分之间的逗号ROW_NUMBERORDER BYrn=Row_number() OVER( partition BY olt.position ORDER BY olt.position)
于 2013-03-06T09:29:48.757 回答
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
于 2013-03-06T11:54:57.113 回答