SQL Server - 选择两列的不同,其中所选的不同列具有基于另外两列的最大值

时间:2016-03-03 11:56:42

标签: sql sql-server distinct multiple-columns

我有2个表 - TC和T,下面指定了列。 TC映射到T_ID列上的T。

TC
----
T_ID,
TC_ID

T
-----
T_ID,
V_ID, 
Datetime,
Count

我当前的结果集是:

V_ID TC_ID  Datetime     Count   
----|-----|------------|--------|
2   | 1   | 2013-09-26 | 450600 |
2   | 1   | 2013-12-09 | 14700  |  
2   | 1   | 2014-01-22 | 15000  |
2   | 1   | 2014-01-22 | 15000  |
2   | 1   | 2014-01-22 | 7500   |
4   | 1   | 2014-01-22 | 1000   |
4   | 1   | 2013-12-05 | 0      |
4   | 2   | 2013-12-05 | 0      |

使用以下查询:

select T.V_ID, 
    TC.TC_ID, 
    T.Datetime,
    T.Count 
    from T 
    inner join TC 
    on TC.T_ID = T.T_ID

我想要的结果集:

V_ID TC_ID  Datetime     Count   
----|-----|------------|--------|  
2   | 1   | 2014-01-22 | 15000  |
4   | 1   | 2014-01-22 | 1000   |
4   | 2   | 2013-12-05 | 0      |

我想编写一个查询来选择每个不同的V_ID + TC_ID组合,但仅限于最大日期时间,并且该日期时间是最大计数。例如。对于V_ID = 2TC_ID = 1的独特组合,'2014-01-22'是最大日期时间,对于该日期时间,15000是最大计数,因此请为新表选择此记录。有任何想法吗?我不知道这对于查询是否过于雄心勃勃而我应该只在代码中处理结果集。

3 个答案:

答案 0 :(得分:2)

一种方法使用You can customize your image view add all functionality in the class like this i create a image view as a circle imageview. import android.content.Context; import android.graphics.Canvas; import android.graphics.Path; import android.graphics.RectF; import android.util.AttributeSet; import android.widget.ImageView; public class RoundedImageView extends ImageView { public RoundedImageView(Context context) { super(context); } public RoundedImageView(Context context, AttributeSet attrs) { super(context, attrs); } public RoundedImageView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override protected void onDraw(Canvas canvas) { float radius = 90.0f; // angle of round corners Path clipPath = new Path(); RectF rect = new RectF(0, 0, this.getWidth(), this.getHeight()); clipPath.addRoundRect(rect, radius, radius, Path.Direction.CW); canvas.clipPath(clipPath); super.onDraw(canvas); } }

   private Diagnostics backupCopy;
   private bool inEdit;

   public void BeginEdit()
   {
       if (inEdit) return;
       inEdit = true;
       backupCopy = this.MemberwiseClone() as Diagnostics;
   }

   public void CancelEdit()
   {
       if (!inEdit) return;
       inEdit = false;
       this.Name= backupCopy.Name;
   }

   public void EndEdit()
   {
       if (!inEdit) return;
       inEdit = false;
       backupCopy = null;
   }

唯一的问题是某些行具有相同的最大row_number()值。 SQL表表示无序集,因此无法确定哪个是最大值 - 除非select v_id, tc_id, datetime, count from (select T.V_ID, TC.TC_ID, T.Datetime, T.Count, row_number() over (partition by t.V_ID, tc.tc_id order by datetime desc, count desc ) as seqnum from t join tc on tc.t_id = t._id ) tt where seqnum = 1; 确实有时间组件或另一列指定一天内的排序。< / p>

答案 1 :(得分:1)

可以使用CTE解决这个问题。首先,从查询中提取数据。第二,获得最大化。第三,获得每个maxdate的最高计数。:

;WITH Dataset AS 
(
    select T.V_ID, 
    TC.TC_ID, 
    T.[Datetime],
    T.[Count] 
    from T 
    inner join TC 
    on TC.T_ID = T._ID
),
MaxDates AS 
(
    SELECT V_ID, TC_ID, MAX(t.[Datetime]) AS MaxDate
    FROM Dataset t
    GROUP BY t.V_ID, t.TC_ID
)
SELECT t.V_ID, t.TC_ID, t.[Datetime], MAX(t.[Count]) AS [Count]
FROM Dataset t
INNER JOIN MaxDates m ON t.V_ID = m.V_ID AND t.TC_ID = m.TC_ID AND m.MaxDate = t.[Datetime]
GROUP BY t.V_ID, t.TC_ID, t.[Datetime]

答案 2 :(得分:0)

只是为了保持简单: 您需要按T.V_ID,TC.TC_ID进行分组, 选择最大日期然后获得最大计数,您必须使用如下的子查询,

select T.V_ID, 
    TC.TC_ID, 
    max(T.Datetime) as Date_Time,
    (select max(Count) from T as tb where v_ID = T.v_ID and DateTime = max(T.DateTime)) as Count
    from T 
    inner join TC 
    on TC.T_ID = T._ID
group by T.V_ID,TC.TC_ID,