选择带有条件的唯一字段值

时间:2018-08-14 16:41:01

标签: sql sql-server

我是SQL新手,我的问题是:

我有一张类似的桌子

panGesture

我需要通过列tableView.panGestureRecognizer.addTarget(self, action: #selector(handlePan(_:)))@objc func handlePan(_ sender: UIPanGestureRecognizer) { let velocity = sender.velocity(in: view) let translation = sender.translation(in: view) switch sender.state { case .began: break case .changed: // shouldMoveSheet can check to see if the sheet should move or scroll view should scroll based on content offset, pan translation, etc. if shouldMoveSheet(for: translation) { bottomConstraint.constant = newConstant // while moving sheet set the content offset to the top so the scroll view does not scroll tableView.setContentOffset(CGPoint(x: 0.0, y: tableView.adjustedContentInset.top), animated: false) sender.setTranslation(.zero, in: view) } case .ended: // Depending on if the sheet has been moved when the gesture ends move the sheet to the up or down position if velocity.y < 0 && tableView.contentOffset.y <= tableView.adjustedContentInset.top { bottomConstraint.constant = constantForSheetUp } else if tableView.contentOffset.y <= tableView.adjustedContentInset.top { bottomConstraint.constant = constantForSheetDown } UIView.animate(duration: 0.3) { view.layoutIfNeeded() } case .failed, .cancelled, .possible: break } } 的最小值选择两个字段( card shop time date 1 1 0000 20171001 2 2 0125 20171002 2 1 0344 20171002 3 3 0342 20171103 4 5 1334 20171104 4 4 1225 20171105 5 4 1452 20171106 card必须是唯一的)和card) (优先级为shop)。

结果应如下所示:

time

提前谢谢!

2 个答案:

答案 0 :(得分:3)

对于SQL Server,您可以使用WITH TIES

select top 1 with ties *
from yourTable
order by row_number() over (partition by card order by date asc, time asc)

答案 1 :(得分:1)

您可以使用子查询和聚合功能

select * from yourtable t 
where t.date in (select min(date) from yourtable  t1
                         where t.card=t1.card )