在SQL中执行算术运算

时间:2017-08-15 13:14:17

标签: sql

你能帮我解决这个问题吗?我有以下查询,它返回国家名称,每个国家/地区的记录数和国家/地区总数。我怎样才能让它返回每个国家/地区的百分比列数。理想的输出就像是。

  

美国,25,100,25%......英国,28,100,28%......等......

SELECT Country, COUNT(Country) AS number, (SELECT COUNT(Country) 
FROM[Customers]) AS total FROM [Customers] 
GROUP BY Country ORDER BY number DESC

我尝试了number/total AS percent但是没有用。显然我做错了。

我希望能够过滤某些百分比超过20%的国家/地区。

谢谢!

3 个答案:

答案 0 :(得分:1)

使用派生表,它是带别名的子查询。

//
//  DemoTransitionAnimationViewController.swift
//  LoginModule
//
//  Created by Shubham Ojha on 8/15/17.
//  Copyright © 2017 BBI. All rights reserved.
//

import UIKit

class DemoTransitionAnimationViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        print(self.navigationController ?? "Not exist")

        if self.navigationController != nil{
            self.navigationController?.delegate = NavigationControllerDelegate()
       // In the above statement if I am setting the delegate as self instead of 
      //NavigationControllerDelegate() and conforming the methods of navigation 
      //controller delegate protocol. It works perfectly.
        }
        else{
            print("navigation controller does not exist")
        }

    }

}

答案 1 :(得分:0)

你应该手动完成:

SELECT Country, COUNT(Country)*100/ (SELECT COUNT(Country) FROM[Customers]) AS total FROM [Customers] where total >20

答案 2 :(得分:0)

SQL下面会对你有帮助。

SELECT Country, COUNT(Country) AS number, (SELECT COUNT(Country) FROM[Customers]) AS total, ROUND(COUNT(Country) * 100.0 / (SELECT COUNT(Country) FROM[Customers]), 1) FROM [Customers]