先按最重复记录排序

时间:2019-03-04 05:53:48

标签: sql-server sql-server-2008-r2

imagePickerManager.pickImage(viewController: collectionViewController)

查询:我正在寻找要在结果集中显示重复记录的顺序。

class ImagePickerManager: NSObject, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    var imagePicker = UIImagePickerController()
    weak var delegate: ImageGetterDelegate?
    var viewController: UIViewController!

    override init() {
        super.init()
        print("ImagePickerManager initialisiert")
    }


    deinit {
        print("imagePickerManager deinitialisiert")
    }
    /// use to pick the Image, make sure to use the root ViewController to pass in to
    func pickImage<T:UIViewController>(viewController: T) {
        self.viewController = viewController
        let alertList = UIAlertController(title: NSLocalizedString("Load Picture", comment: "Picture alert Alertcontroller"), message: nil, preferredStyle: .actionSheet)

        let cameraAction = UIAlertAction(title: "Camera", style: .default) {
            UIAlertAction in self.openCamera()
            alertList.dismiss(animated: true, completion: nil)
        }

        let galleryAction = UIAlertAction(title: "Gallery", style: .default) {
            UIAlertAction in self.openGallery()
            alertList.dismiss(animated: true, completion: nil)
        }

        let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) {
            UIAlertAction in
            alertList.dismiss(animated: true, completion: nil)
        }

        alertList.addAction(cameraAction)
        alertList.addAction(galleryAction)
        alertList.addAction(cancelAction)

        viewController.present(alertList, animated: true, completion: nil)
    }

    private func openCamera() {

        if(UIImagePickerController .isSourceTypeAvailable(.camera)) {
            imagePicker.sourceType = .camera
            imagePicker.delegate = self
            viewController.present(imagePicker, animated: true, completion: nil)
        } else {
            let warningAlert = UIAlertController(title: "Warning", message: "You do not have a camera", preferredStyle: .alert)
            let cancelAction = UIAlertAction(title: "Okay", style: .cancel) {
                UIAlertAction in
                warningAlert.dismiss(animated: true, completion: nil)
            }
            warningAlert.addAction(cancelAction)
            viewController.present(warningAlert, animated: true, completion: nil)
        }

    }

    private func openGallery() {
        imagePicker.sourceType = .photoLibrary
        imagePicker.delegate = self
        viewController.present(imagePicker, animated: true, completion: nil)
    }

    @objc func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        picker.dismiss(animated: true, completion: nil)
        guard let image = info[.originalImage] as? UIImage else {
            print("Expected a dictionary containing an image, but was provided the following: \(info)")
            return
        }
        viewController = nil
        delegate?.returnImage(image: image)

    }

}

获取:

create table tbl_dup
(
    name varchar(100)
);

insert into tbl_dup values('Arsel Rous'),('Oram Rock'),('Oram Rock'),('Brown Twor'),
                          ('John Mak'),('Mak Dee'),('Smith Will'),('Mak Dee'),
                          ('John Mak'),('Oram Rock'),('John Mak'),('Oram Rock');

预期结果:

select * 
from
(
select name,row_number() over(partition by name order by name) rn 
from tbl_dup
) a
order by name,rn;

2 个答案:

答案 0 :(得分:2)

尝试在COUNT子句中将ORDER BY用作分析函数:

SELECT name, ROW_NUMBER() OVER (PARTITION by name ORDER BY name) rn
FROM tbl_dup
ORDER BY COUNT(*) OVER (PARTITION BY name) DESC, rn;

enter image description here

Demo

答案 1 :(得分:1)

您需要按出现次数desc进行第一排序,并按asc进行行编号。

select name,rn 
from
(
    select name,row_number() over(partition by name order by name) rn ,
    count(*) over(partition by name) ct
    from tbl_dup
) a
order by ct desc, rn asc