我有一个问题,我需要确定有多少公司参与了给定的项目编号。例如,确定哪些公司恰好参与了2个项目。
给出下面的基数:
Option Explicit
Sub Export_to_TXT_UTF16()
Dim saveas_filename As Variant
saveas_filename = Application.GetSaveAsFilename(FileFilter:="Unicode Text (*.txt), *.txt", Title:="SaveAs")
If saveas_filename = False Then
Exit Sub
End If
With Application
.ScreenUpdating = False
.DisplayAlerts = False
End With
ActiveSheet.Copy
With ActiveSheet.UsedRange
.Value = .Value
End With
Rows(1).Delete 'DELETE ROW #1
Columns("C:G").Delete 'DELETE COLUMNS C-G
ActiveWorkbook.SaveAs Filename:=saveas_filename, FileFormat:=xlUnicodeText
ActiveWorkbook.Close SaveChanges:=False
With Application
.ScreenUpdating = True
.DisplayAlerts = True
End With
MsgBox "Your data has been exported!", vbExclamation, "Sheet Exported"
End Sub
如何使用SQL查询解决此问题?对于上述情况,查询将返回:
id company project
1 company1 project1
2 company2 project1
3 company3 project2
4 company1 project2
5 company2 project2
6 company2 project3
7 company3 project1
8 company1 project1
9 company2 project2
10 company1 project2
答案 0 :(得分:4)
您可以使用company
来聚合,并使用having
子句进行过滤:
select company
from mytable
group by company
having count(project) = 2
如果同一项目对于给定的公司可能发生两次,那么您希望使用count(distinct project)
而不是count(project)
。
答案 1 :(得分:3)
您使用group by
和having
:
select company
from t
group by company
having count(distinct project) = 2;