SQL Server:如何计算从前50%查询返回的记录

时间:2018-08-19 11:45:31

标签: sql-server tsql

如何获取前50%选择查询返回的记录数。

    function throwIfMissing() {
       throw new Error('Missing parameter');
    }

    class Student{
        constructor(mustBeProvided = throwIfMissing()) {
             return mustBeProvided;
         }
    }
    var student = new Student(100); //works
    console.log(student);
    var err = new Student(); // throws Uncaught Error: Missing parameter

我尝试使用以下查询

select top 50 percent * 
from Customers

但它不准确,因为当表中的记录总数为91时,它将返回45条记录,而TOP 50%的查询将返回46条。

我需要一个查询,它可以告诉select count(*)/2 from Customers 返回的记录数

2 个答案:

答案 0 :(得分:4)

您可以使用子查询将其包装:

SELECT COUNT(*) FROM (select TOP 50 percent * from Customers) s

或使用窗口计数:

select TOP 50 percent *, COUNT(*) OVER() AS cnt from Customers

答案 1 :(得分:0)

您可以使用COUNT并使用小数点获得更准确的结果:

 override func viewDidLoad() {
    super.viewDidLoad()

    self.wishlistBackgroundView.hero.isEnabled = true
    self.wishlistBackgroundView.heroID = "wishlistView"

    self.wishlistBackgroundView.hero.modifiers = [.fade, .translate(CGPoint(x: 0, y: 800), z: 20)]

    // adding panGestureRecognizer
    panGR = UIPanGestureRecognizer(target: self,
              action: #selector(handlePan(gestureRecognizer:)))
    view.addGestureRecognizer(panGR)

    self.wishlistLabel.text = wishList.name
    self.wishlistImage.image = wishList.image
    self.theTableView.wishList = wishList.wishData
    self.theTableView.tableView.reloadData()


    view.addSubview(wishlistBackgroundView)
    view.addSubview(dismissWishlistViewButton)
    view.addSubview(menueButton)
    wishlistBackgroundView.addSubview(wishlistView)
    wishlistBackgroundView.addSubview(wishlistLabel)
    wishlistBackgroundView.addSubview(wishlistImage)
    wishlistView.addSubview(theTableView.tableView)
    wishlistView.addSubview(addWishButton)

    NSLayoutConstraint.activate([


        // constrain wishlistView
        wishlistBackgroundView.topAnchor.constraint(equalTo: view.topAnchor),
        wishlistBackgroundView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
        wishlistBackgroundView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
        wishlistBackgroundView.trailingAnchor.constraint(equalTo: view.trailingAnchor),


        wishlistView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 160.0),
        wishlistView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0),
        wishlistView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 0),
        wishlistView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: 0),

        // constrain wishTableView
        theTableView.view.topAnchor.constraint(equalTo: wishlistView.topAnchor, constant: 60.0),
        theTableView.view.bottomAnchor.constraint(equalTo: wishlistView.bottomAnchor, constant: 0),
        theTableView.view.leadingAnchor.constraint(equalTo: wishlistView.safeAreaLayoutGuide.leadingAnchor, constant: 30.0),
        theTableView.view.trailingAnchor.constraint(equalTo: wishlistView.safeAreaLayoutGuide.trailingAnchor, constant: -30.0),

        // constrain dismissButton
        dismissWishlistViewButton.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 20),
        dismissWishlistViewButton.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 23.0),

        // constrain menueButton
        menueButton.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 20),
        menueButton.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: -25.0),

        // constrain wishlistImage
        wishlistImage.topAnchor.constraint(equalTo: wishlistView.topAnchor, constant: -70),
        wishlistImage.leadingAnchor.constraint(equalTo: wishlistView.leadingAnchor, constant: 30),
        wishlistImage.widthAnchor.constraint(equalToConstant: 90),
        wishlistImage.heightAnchor.constraint(equalToConstant: 90),

        //constrain wishlistlabel
        wishlistLabel.topAnchor.constraint(equalTo: wishlistView.topAnchor, constant: -47),
        wishlistLabel.leadingAnchor.constraint(equalTo: wishlistImage.leadingAnchor, constant: 100),

        addWishButton.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: -20),
        addWishButton.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: -40),

    ])

    // set DeleteWishDelegate protocol for the table
    theTableView.deleteWishDelegate = self


}

// define a small helper function to add two CGPoints
func addCGPoints (left: CGPoint, right: CGPoint) -> CGPoint {
  return CGPoint(x: left.x + right.x, y: left.y + right.y)
}

// handle swqipe down gesture
@objc private func handlePan(gestureRecognizer:UIPanGestureRecognizer) {

    // calculate the progress based on how far the user moved
    let translation = panGR.translation(in: nil)
    let progress = translation.y / 2 / view.bounds.height

  switch panGR.state {
  case .began:
    // begin the transition as normal
    dismiss(animated: true, completion: nil)
  case .changed:

    Hero.shared.update(progress)

    // update views' position based on the translation
    let viewPosition = CGPoint(x: wishlistBackgroundView.center.x, y: translation.y + wishlistBackgroundView.center.y)

    Hero.shared.apply(modifiers: [.position(viewPosition)], to: self.wishlistBackgroundView)


  default:
    // finish or cancel the transition based on the progress and user's touch velocity
       if progress + panGR.velocity(in: nil).y / view.bounds.height > 0.3 {
         Hero.shared.finish()
       } else {
         Hero.shared.cancel()
       }
  }
}