SQL Query从每列获取数据

时间:2012-02-26 21:00:58

标签: sql database sqlite

我在表中有多列

一列可能包含州名(US),另一列包含专业

以下是一些示例数据(纽约可能有多个C#,也可能存在于其他州)

C# NY
C# NJ
C# WA
C# CA
C# NY
C# NY
Java NY
Java NJ
Java IL

基本上我想得到一个

的输出
C# 100 (sum of all states that have C#)
Java 85 (sum of all states that have Java)

我想得到

State  Total Speciality
NY     150   C# 
NY     100   C++ 

我使用SQLite3作为我的数据库

1 个答案:

答案 0 :(得分:1)

Sqlite应该有GROUP BY和COUNT。你可以尝试:

SELECT Speciality, COUNT(State) AS Total
FROM YourTable 
GROUP BY Speciality 

SELECT State, Speciality, COUNT(*)  AS Total
FROM YourTable 
GROUP BY State, Speciality