我需要识别具有多个相同IncidentCode的所有StudentID,但仅当IncidentID也是>我该怎么办?
数据集:
期望的结果:
示例数据集代码:
CREATE TABLE #Duplicates(
IncidentID varchar(10),
StudentID varchar(10),
IncidentCode varchar(10),
Levels varchar(10)
)
INSERT into #Duplicates (IncidentID, StudentID, IncidentCode, Levels)
VALUES
('1', '4444444','06','1'),
('1', '4444444','06','2'),
('23', '5555555','06','1'),
('22', '3333333','06','2'),
('64', '3333333','06','2'),
('64', '3333333','06','3')
SELECT *
FROM #Duplicates
谢谢!
答案 0 :(得分:2)
SELECT Distinct *
FROM #Duplicates
Where IncidentID in (Select IncidentID from #Duplicates Group By IncidentID having count(*)>1)
返回
IncidentID StudentID IncidentCode Levels
1 4444444 06 1
1 4444444 06 2
64 3333333 06 2
64 3333333 06 3
答案 1 :(得分:1)
您也可以使用连接:如果您处理大量数据,它将帮助您提高性能
SELECT *
FROM #Duplicates a
INNER JOIN (SELECT incidentID FROM #Duplicates b
GROUP BY incidentID HAVING COUNT(incidentCode)>1) b ON a.IncidentID = b.IncidentID
答案 2 :(得分:1)
您可以使用此类查询
;WITH cte
AS (SELECT
*,
ROW_NUMBER() OVER (PARTITION BY studentid ORDER BY incidentcode) AS rn
FROM #Duplicates
WHERE IncidentID > 1)
SELECT DISTINCT
StudentId
FROM cte
答案 3 :(得分:0)
let refer = FIRDatabase.database().reference().child("UserDevices")
var globalEmail : String!
var globalPhone : String!
var globalImageUrl: String!
override func viewWillAppear(_ animated : Bool){
super.viewWillAppear(animated)
retrieveUserData{(email,phone,ImageUrl) in
self.globalEmail = email
self.globalPhone = phone
self.globalImageUrl = ImageUrl
}
}
func retrieveUserData(_ completionBlock : @escaping ((_ email : String?, _ phone : String?, _ ImageUrl: String?)->Void)){
refer.child(byAppendingPath: self.strUserid as String).observe(.value , with: {snapshot in
if let userDict = snapshot.value as? [String:AnyObject] {
completionBlock(userDict["email"] as! String, userDict["phone"] as! String, userDict["ImageUrl"] as! String)
}
})
}
var strUserid : NSString!
override func viewDidLoad() {
super.viewDidLoad()
print(globalEmail)
print(globalImageUrl)
print(globalPhone)
self.navigationController?.navigationBar.tintColor = UIColor.white
print("idis \(self.strUserid)")
let ref = FIRDatabase.database().reference().child("UserDevices")
self.navigationController?.navigationBar.tintColor = UIColor.white
ref.child(byAppendingPath: self.strUserid as String).observe(.value, with: { snapshot in
if let dict = snapshot.value as? NSMutableDictionary{
print("dict is \(dict)")
if let Provider = dict["name"] as? String
{
self.DeviceDetailsProvider.text = Provider
// self.navigationItem.title = Provider
}
if let name = dict["DeviceName"] as? String
{
self.DeviceDetailsName.text = name
self.navigationItem.title = name
}
if let ShortDescription = dict["Description"] as? String
{
self.DeviceDetailsDescription.text = ShortDescription
}
if let City = dict["city"] as? String
{
self.DeviceDetailsCity.text = City
}
}
})
self.DeviceDetailsImageView.downloadedFrom(link: globalImageUrl)
}