我有SQL表(有更多列,但并不重要)
CREATE TABLE `test_results` (
`id` int AUTO_INCREMENT,
`date_time` datetime,
`altpn` varchar(60),
`error1` int,
`error2` int,
`error3` int,
PRIMARY KEY (`id`)
);
数据可以是:id,2016-06-16 14:26:02,9513 400,590,0,0。
在此表上运行此SQL查询:
SELECT date(test_results.date_time) AS date, test_results.altpn as PN, COUNT(*) AS count
FROM test_results
WHERE (test_results.error1 = 1000 OR test_results.error1 = 1001 OR test_results.error2 = 1000 OR test_results.error2 = 1001 OR test_results.error3 = 1000 OR test_results.error3 = 1001)
GROUP BY date(test_results.date_time), test_results.altpn
ORDER BY date(test_results.date_time), test_results.altpn
这可行,但我还需要知道COUNT(*)
为零时的日期。我看到关于LEFT JOIN
的几个主题,但我无法修改代码以使用此表。
答案 0 :(得分:0)
使用WHERE时,会过滤出包含其他错误代码的行,并且无法对其进行计数。而是像往常一样选择所有行,并将LEFT JOIN选择到不存在零行的表:
#Region "Merge Combo's"
Private Sub List_Merge_Click(sender As Object, e As EventArgs) Handles List_Merge.Click
Dim ofd = New OpenFileDialog()
ofd.Title = "Import..."
ofd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
ofd.Filter = "Text files|*.txt"
ofd.Multiselect = True
'If the user selects 2 Files;
If (ofd.ShowDialog() = DialogResult.OK And ofd.CheckFileExists = True) Then
'Make sure there are no previously stored Conversions;
ActionList2.Items.Clear()
'Variables;
Dim MergedCombos As Integer = 0
Dim TotalCombos As Integer = 0
Try
For Each filename As String In ofd.FileNames
Using sr As New StreamReader(filename)
Dim result = filename.Union(filename, New MailEqualityComparer)
'Get all Matches found from the Regex Condition;
Dim combos As MatchCollection = New Regex("^([^@]+)@(.*):(.*)$", RegexOptions.Multiline).Matches(sr.ReadToEnd)
'Add each Match to the ActionList except for Duplicates;
For Each combo As Match In combos
'Increment the Total Combo's count;
TotalCombos += 1
'If the ActionList doesn't contain the same Combo;
If Not ActionList2.Items.Contains(combo.Value) Then
'If the email is already in the ActionList;
If IsInListbox(ActionList2, combo.Groups(1).Value + "@" + combo.Groups(2).Value) = True Then
'This combo is presumed to be a Hash Decrypted Combo - Overwrite it with the Encrypted Hash;
ActionList2.Items.Add(combo.Value)
'Remove the Hash Item from ActionList;
ActionList2.Items.RemoveAt(FindListboxIndex(ActionList2, combo.Groups(1).Value + "@" + combo.Groups(2).Value))
Else
'Add the Combo;
ActionList2.Items.Add(combo.Value)
End If
End If
Next
End Using
Next
Catch ex As Exception
Console.WriteLine("Error: " + ex.ToString)
Finally
'If atleast 1 Item is in the ActionList, Enable the Export Button;
If ActionList2.Items.Count > 0 Then
ExportButton.Enabled = True
ExportButton.BackColor = Color.FromArgb(149, 255, 141)
End If
'Update the Merged Combo's count;
StatusBar_LeftText.Text = MergedCombos.ToString
'If MergedCombos are less than TotalCombos, Add a "x Duplicates Removed" message;
If MergedCombos < TotalCombos Then
StatusBar_LeftText.Text += " - " + (TotalCombos - MergedCombos).ToString + " Duplicates Removed"
End If
'Autoscroll;
ActionList2.TopIndex = ActionList2.Items.Count - 1
End Try
End If
End Sub
Private Function FindListboxIndex(lb As ListBox, searchString As String) As Integer
For i As Integer = 0 To lb.Items.Count - 1
If lb.Items(i).ToString().Contains(searchString) Then
Return i
Exit Function
End If
Next
Return -1
End Function
Private Function IsInListbox(lb As ListBox, searchString As String) As Boolean
For i As Integer = 0 To lb.Items.Count - 1
If lb.Items(i).ToString().Contains(searchString) Then
Return True
Exit Function
End If
Next
Return False
End Function
#End Region
如您所见,结果集包含错误为1000/1001的行和其他错误的NULL值。现在,只需将它们分组并计算:
SELECT *
FROM test_results t1
LEFT JOIN test_results t2 ON t2.id=t1.id
AND
(t2.error1 = 1000 OR t2.error1 = 1001 OR t2.error2 = 1000
OR t2.error2 = 1001 OR t2.error3 = 1000 OR t2.error3 = 1001)