如何在SQL中打印唯一元素并计数频率?

时间:2019-01-10 04:45:15

标签: mysql sql database

我有一个如下表,

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
        self.menubar.setObjectName("menubar")
        self.menuMaintenance = QtWidgets.QMenu(self.menubar)
        self.menuMaintenance.setObjectName("menuMaintenance")
        self.menuStock_Items = QtWidgets.QMenu(self.menuMaintenance)
        self.menuStock_Items.setObjectName("menuStock_Items")
        self.menuStock_Modify = QtWidgets.QMenu(self.menuMaintenance)
        self.menuStock_Modify.setObjectName("menuStock_Modify")
        self.menuInput = QtWidgets.QMenu(self.menubar)
        self.menuInput.setObjectName("menuInput")
        self.menuReports = QtWidgets.QMenu(self.menubar)
        self.menuReports.setObjectName("menuReports")
        self.menuStock_Statements = QtWidgets.QMenu(self.menuReports)
        self.menuStock_Statements.setObjectName("menuStock_Statements")
        self.menuGroupwise_Statements = QtWidgets.QMenu(self.menuReports)
        self.menuGroupwise_Statements.setObjectName("menuGroupwise_Statements")
        self.menuPuritywise_Statements = QtWidgets.QMenu(self.menuReports)
        self.menuPuritywise_Statements.setObjectName("menuPuritywise_Statements")
        self.menuStaff_Statements = QtWidgets.QMenu(self.menuReports)
        self.menuStaff_Statements.setObjectName("menuStaff_Statements")
        self.menuExit = QtWidgets.QMenu(self.menubar)
        self.menuExit.setObjectName("menuExit")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)
        self.actionGold = QtWidgets.QAction(MainWindow)
        self.actionGold.setObjectName("actionGold")
        self.actionDiamond = QtWidgets.QAction(MainWindow)
        self.actionDiamond.setObjectName("actionDiamond")
        self.actionPurity_Master = QtWidgets.QAction(MainWindow)
        self.actionPurity_Master.setObjectName("actionPurity_Master")
        self.actionGold_2 = QtWidgets.QAction(MainWindow)
        self.actionGold_2.setObjectName("actionGold_2")
        self.actionDiamond_2 = QtWidgets.QAction(MainWindow)
        self.actionDiamond_2.setObjectName("actionDiamond_2")
        self.actionArrivals = QtWidgets.QAction(MainWindow)
        self.actionArrivals.setObjectName("actionArrivals")
        self.actionIssues = QtWidgets.QAction(MainWindow)
        self.actionIssues.setObjectName("actionIssues")
        self.actionGold_3 = QtWidgets.QAction(MainWindow)
        self.actionGold_3.setObjectName("actionGold_3")
        self.actionDiamond_3 = QtWidgets.QAction(MainWindow)
        self.actionDiamond_3.setObjectName("actionDiamond_3")
        self.actionGold_4 = QtWidgets.QAction(MainWindow)
        self.actionGold_4.setObjectName("actionGold_4")
        self.actionDiamond_4 = QtWidgets.QAction(MainWindow)
        self.actionDiamond_4.setObjectName("actionDiamond_4")
        self.actionGold_5 = QtWidgets.QAction(MainWindow)
        self.actionGold_5.setObjectName("actionGold_5")
        self.actionDiamond_5 = QtWidgets.QAction(MainWindow)
        self.actionDiamond_5.setObjectName("actionDiamond_5")
        self.actionCombined = QtWidgets.QAction(MainWindow)
        self.actionCombined.setObjectName("actionCombined")
        self.actionGold_6 = QtWidgets.QAction(MainWindow)
        self.actionGold_6.setObjectName("actionGold_6")
        self.actionDiamond_6 = QtWidgets.QAction(MainWindow)
        self.actionDiamond_6.setObjectName("actionDiamond_6")
        self.actionDiamond_7 = QtWidgets.QAction(MainWindow)
        self.actionDiamond_7.setObjectName("actionDiamond_7")
        self.actionGold_7 = QtWidgets.QAction(MainWindow)
        self.actionGold_7.setObjectName("actionGold_7")
        self.actionDaily_Arrivals_2 = QtWidgets.QAction(MainWindow)
        self.actionDaily_Arrivals_2.setObjectName("actionDaily_Arrivals_2")
        self.actionDaily_Issues = QtWidgets.QAction(MainWindow)
        self.actionDaily_Issues.setObjectName("actionDaily_Issues")
        self.menuStock_Items.addAction(self.actionGold)
        self.menuStock_Items.addAction(self.actionDiamond)
        self.menuStock_Modify.addAction(self.actionGold_2)
        self.menuStock_Modify.addAction(self.actionDiamond_2)
        self.menuMaintenance.addAction(self.menuStock_Items.menuAction())
        self.menuMaintenance.addAction(self.actionPurity_Master)
        self.menuMaintenance.addSeparator()
        self.menuMaintenance.addAction(self.menuStock_Modify.menuAction())
        self.menuInput.addAction(self.actionArrivals)
        self.menuInput.addAction(self.actionIssues)
        self.menuStock_Statements.addAction(self.actionGold_3)
        self.menuStock_Statements.addAction(self.actionDiamond_3)
        self.menuGroupwise_Statements.addAction(self.actionGold_4)
        self.menuGroupwise_Statements.addAction(self.actionDiamond_4)
        self.menuPuritywise_Statements.addAction(self.actionGold_5)
        self.menuPuritywise_Statements.addAction(self.actionDiamond_5)
        self.menuPuritywise_Statements.addSeparator()
        self.menuPuritywise_Statements.addAction(self.actionCombined)
        self.menuStaff_Statements.addAction(self.actionGold_6)
        self.menuStaff_Statements.addAction(self.actionDiamond_6)
        self.menuStaff_Statements.addSeparator()
        self.menuStaff_Statements.addAction(self.actionDiamond_7)
        self.menuReports.addAction(self.menuStock_Statements.menuAction())
        self.menuReports.addAction(self.menuGroupwise_Statements.menuAction())
        self.menuReports.addAction(self.menuPuritywise_Statements.menuAction())
        self.menuReports.addAction(self.menuStaff_Statements.menuAction())
        self.menuReports.addSeparator()
        self.menuReports.addAction(self.actionDaily_Arrivals_2)
        self.menuReports.addAction(self.actionDaily_Issues)
        self.menuReports.addSeparator()
        self.menubar.addAction(self.menuMaintenance.menuAction())
        self.menubar.addAction(self.menuInput.menuAction())
        self.menubar.addAction(self.menuReports.menuAction())
        self.menubar.addAction(self.menuExit.menuAction())

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.menuMaintenance.setTitle(_translate("MainWindow", "Maintenance"))
        self.menuStock_Items.setTitle(_translate("MainWindow", "Stock Items"))
        self.menuStock_Modify.setTitle(_translate("MainWindow", "Stock Modify"))
        self.menuInput.setTitle(_translate("MainWindow", "Input"))
        self.menuReports.setTitle(_translate("MainWindow", "Reports"))
        self.menuStock_Statements.setTitle(_translate("MainWindow", "Stock Statements"))
        self.menuGroupwise_Statements.setTitle(_translate("MainWindow", "Groupwise Statements"))
        self.menuPuritywise_Statements.setTitle(_translate("MainWindow", "Puritywise Statements"))
        self.menuStaff_Statements.setTitle(_translate("MainWindow", "Staff Statements"))
        self.menuExit.setTitle(_translate("MainWindow", "Exit"))
        self.actionGold.setText(_translate("MainWindow", "Gold"))
        self.actionDiamond.setText(_translate("MainWindow", "Diamond"))
        self.actionPurity_Master.setText(_translate("MainWindow", "Purity Master"))
        self.actionGold_2.setText(_translate("MainWindow", "Gold"))
        self.actionDiamond_2.setText(_translate("MainWindow", "Diamond"))
        self.actionArrivals.setText(_translate("MainWindow", "Arrivals"))
        self.actionIssues.setText(_translate("MainWindow", "Issues"))
        self.actionGold_3.setText(_translate("MainWindow", "Gold"))
        self.actionDiamond_3.setText(_translate("MainWindow", "Diamond"))
        self.actionGold_4.setText(_translate("MainWindow", "Gold"))
        self.actionDiamond_4.setText(_translate("MainWindow", "Diamond"))
        self.actionGold_5.setText(_translate("MainWindow", "Gold"))
        self.actionDiamond_5.setText(_translate("MainWindow", "Diamond"))
        self.actionCombined.setText(_translate("MainWindow", "Combined"))
        self.actionGold_6.setText(_translate("MainWindow", "Gold"))
        self.actionDiamond_6.setText(_translate("MainWindow", "Diamond"))
        self.actionDiamond_7.setText(_translate("MainWindow", "Combined"))
        self.actionGold_7.setText(_translate("MainWindow", "Gold"))
        self.actionDaily_Arrivals_2.setText(_translate("MainWindow", "Daily Arrivals"))
        self.actionDaily_Issues.setText(_translate("MainWindow", "Daily Issues"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

我想编写一个SQL,该SQL可以找到唯一的名称并计算出现的次数。预期的输出如下:

Name  ActionCategory
Ana    1
Joe    2
Bob    1
Tom    3
Joe    4
Ana    5
...

我可以使用以下sql打印唯一名称,有人可以告诉我如何计算外观吗?

Name     Appearance
Ana      2
Joe      2
Bob      1
Tom      1

谢谢

5 个答案:

答案 0 :(得分:2)

您可以在下面使用聚合函数count()尝试并按分组应用

SELECT 
   Name,count(Name) as UniqueNames
FROM
   tableName
group by Name

答案 1 :(得分:1)

您可以使用group by

   SELECT Name , count(Name) as Appearance 
      FROM yourtable
    GROUP BY NAME

答案 2 :(得分:1)

您可以使用此:

SELECT 
   Name, count(Name) as TotalAppearance
FROM
   tableName
group by Name
having count(Name) > 0
order by TotalAppearance desc;

您也可以使用order by。

答案 3 :(得分:1)

您可以通过简单地运行以下查询来实现

SELECT 
      Name as UniqueNames, count(Name) as occurrences
FROM
      tableName
HAVING occurrences > 0 --you may ignore this condition
GROUP BY Name
ORDER BY UniqueNames

答案 4 :(得分:0)

您将需要使用distinct和group by来获取出现次数的计数,如下所示:

SELECT distinct(Name), count(Name) as 'Appearance' from test_table group by Name;