1

我有一个t1有 4 列的表:

f1
f2
quant
rate

我需要创建一个包含所有记录的视图,t1但列出每个和组quant对应的最小值和最大值。ratef1f2

我在下面有一个解决方案,但这似乎是一个很长的解决方法。有没有更简单的方法可以在 SQL Server 2014 中实现这些结果?

--Create min
    Select 
        a.f1
        ,a.f2
        ,b.min_quant
        ,a.rate As min_rate
    Into t1_min
    From t1 As a
    Inner Join (
        Select 
            f1
            ,f2
            ,MIN(quant) As min_quant
        From t1
        Group By f1, f2
    ) As b
        On a.f1 = b.f1
        And a.f2 = b.f2
        And a.quant = b.min_quant

--Create Max
    Select 
        a.f1
        ,a.f2
        ,b.max_quant
        ,a.rate As max_rate
    Into t1_max
    From t1 As a
    Inner Join (
        Select 
            f1
            ,f2
            ,MAX(quant) As max_quant
        From t1
        Group By f1, f2
    ) As b
        On a.f1 = b.f1
        And a.f2 = b.f2
        And a.quant = b.max_quant

--Create Final
    Select
    a.f1
    ,a.f2
    ,a.quant
    ,b.min_quant
    ,c.max_quant
    ,a.rate
    ,b.min_rate
    ,c.max_rate
    From t1 As a
    Left Join t1_min As b
        On a.f1 = b.f1
        And a.f2 = b.f2
    Left Join t1_max As c
        On a.f1 = c.f1
        And a.f2 = c.f2
4

4 回答 4

1

您需要删除那些 t1_min 和 t1_max 并每次重新创建。

恕我直言,这是一种更简单的方法(但是您的要求很模糊,例如,当有多个 min\max 值时会发生什么。无论如何,这至少与您的结果相匹配):

SELECT f1, f2, quant, 
  min_quant, min_rate,
  max_quant, max_rate
FROM dbo.t1 t
CROSS APPLY (SELECT MIN(quant), MAX(quant) 
    FROM t1 tt WHERE t.f1=tt.f1 AND t.f2=tt.f2) tm(min_quant, max_quant)
CROSS APPLY (SELECT rate FROM t1 tt 
    WHERE t.f1=tt.f1 AND t.f2=tt.f2 AND tt.quant = tm.min_quant) tminr(min_rate)
CROSS APPLY (SELECT rate FROM t1 tt 
    WHERE t.f1=tt.f1 AND t.f2=tt.f2 AND tt.quant = tm.max_quant) tmaxr(max_rate);
于 2018-08-24T17:22:36.657 回答
1

您可以在两个方向(升序和降序)上创建一个编号,并在将编号记录连接到 t1 时从两个编号中选择数字 1:

WITH
  minmax (f1, f2, quant, rate, rn_min, rn_max) AS (
    SELECT f1, f2, quant, rate,
      ROW_NUMBER() OVER (PARTITION BY f1, f2 ORDER BY quant ASC),
      ROW_NUMBER() OVER (PARTITION BY f1, f2 ORDER BY quant DESC)
    FROM t1
  )
SELECT t1.f1, t1.f2, t1.quant, t1.rate,
  mi.quant AS min_quant, mi.rate AS min_rate,
  ma.quant AS max_quant, ma.rate AS max_rate
FROM t1
  INNER JOIN minmax mi ON t1.f1 = mi.f1 AND t1.f2 = mi.f2 AND mi.rn_min = 1
  INNER JOIN minmax ma ON t1.f1 = ma.f1 AND t1.f2 = ma.f2 AND ma.rn_max = 1
于 2018-08-24T17:29:06.443 回答
1

我猜窗口函数会做:

select
    f1, f2,
    quant,
    min(quant) over(partition by f1, f2) as min_quant,
    max(quant) over(partition by f1, f2) as max_quant,
    rate,
    min(rate) over(partition by f1, f2) as min_rate,
    max(rate) over(partition by f1, f2) as max_rate,
  from t1;
于 2018-08-24T17:09:34.303 回答
0

鉴于其他答案的良好线索,我发现了使用 FIRST_VALUE() 和 LAST_VALUE() 创建所需输出的分析函数。

Select
    f1
    ,f2
    ,quant
    ,MIN(quant) Over(Partition By f1, f2) As min_quant
    ,MAX(quant) Over(Partition By f1, f2) As max_quant
    ,rate
    ,FIRST_VALUE(rate) Over(Partition By f1, f2 Order By quant) As min_rate
    ,LAST_VALUE(rate) Over(Partition By f1, f2 Order By quant RANGE Between CURRENT ROW And UNBOUNDED FOLLOWING) As max_rate
From t1
Order By f1, f2, quant
于 2018-08-24T18:19:19.690 回答