在sql中分组两个条件

时间:2016-09-02 15:49:47

标签: sql sql-server group-by

我有这张桌子

Option Explicit

Private Sub cmdSendWorksheet_Click()

Dim xFile As String
Dim xFormat As Long
Dim Wb As Workbook
Dim Wb2 As Workbook
Dim FilePath As String
Dim FileName As String
Dim OutlookApp As Object
Dim OutlookMail As Object

'On Error Resume Next

Application.ScreenUpdating = False

Set Wb = Application.ActiveWorkbook
ActiveSheet.Copy

Set Wb2 = Application.ActiveWorkbook

Select Case Wb.FileFormat
    Case xlOpenXMLWorkbook:
    xFile = ".xlsx"
    xFormat = xlOpenXMLWorkbook
    Case xlOpenXMLWorkbookMacroEnabled:
    If Wb2.HasVBProject Then
    xFile = ".xlsm"
    xFormat = xlOpenXMLWorkbookMacroEnabled
Else
    xFile = ".xlsm"
    xFormat = xlOpenXMLWorkbook
    End If
End Select

FilePath = Environ$("temp") & "\"
FileName = Wb.Name & Format(Now, "dd-mmm-yy h-mm-ss")
Set OutlookApp = CreateObject("Outlook.Application")
Set OutlookMail = OutlookApp.CreateItem(0)
Wb2.SaveAs FilePath & FileName & xFile, FileFormat:=xFormat

With OutlookMail
.To = "email address"
.CC = ""
.BCC = ""
.Subject = "Worksheet Attached"
.Body = "Please see attached worksheet"
.cmdSendWorksheet.Enabled = True
.Attachments.Add Wb2.FullName
.Send
End With

Wb2.Close
Kill FilePath & FileName & xFile
Set OutlookMail = Nothing
Set OutlookApp = Nothing
Application.ScreenUpdating = True

End Sub

enter image description here

如何按此查询进行分组以计算SELECT [WeldStatus] ,PackageId FROM [SPMS2].[dbo].[JointHistory] total testpackage以及total accept

例如,对于packageid = 6030,我需要这个:

total remain

6 个答案:

答案 0 :(得分:1)

使用CASEGroup By

SELECT 
      PackageId,
      count(*) as total,
      sum(case [WeldStatus] when 'Accept' then 1 end) as accept,
      sum(case [WeldStatus] when 'accept' then 0 else 1 end) as remain
  FROM [SPMS2].[dbo].[JointHistory]
group by PackageId;

答案 1 :(得分:1)

SELECT
packageid
,count(*) as 'total'
,sum(case when weldstatus = 'Accept' then 1 else 0 end) as 'accept'
,sum(case when weldstatus is null then 1 else 0 end) as 'remain'
FROM [SPMS2].[dbo].[JointHistory]
group by packageid

"总和(情况......"已经拯救了我很多次。当你拯救你时,NULL可能会很痛苦。

答案 2 :(得分:1)

您可以在聚合中使用CASE表达式来实现所需的输出。

SELECT
    PackageId,
    SUM (CASE WHEN WeldStatus = 'Accept' THEN 1 ELSE 0 END) AS AcceptCount, 
    SUM (CASE WHEN WeldStatus IS NULL THEN 1 ELSE 0 END) AS RemainCount, 
    COUNT (*) AS Total
FROM [SPMS2].[dbo].[JointHistory]
GROUP BY PackageId

答案 3 :(得分:1)

select packageid, SUM(TOTAL),  SUM(ACCEPT),  SUM(REMAIN) from (
select packageid, 1 as TOTAL, 
case 
when WeldStatus='Accept' then 1 else 0
END as ACCEPT,
case 
when WeldStatus  IS NULL then 1 else 0
END as REMAIN
from JointHistory
) group by packageid

答案 4 :(得分:1)

如果WeldStatus仅采用'Accept'NULL的值,则一个简单的方法是:

SELECT PackageId, count(*) as total,
       count(WeldStatus) as accept,
       count(*) - count(WeldStatus) as remain
FROM [SPMS2].[dbo].[JointHistory] jh
GROUP BY PackageId;

我是第一个承认这依赖于单个值或NULL的假设。

答案 5 :(得分:0)

SELECT PackageId,       COUNT(*)AS总计,       SUM(当[WeldStatus] ='接受'那么1结束时的情况)AS接受,       SUM(ISNULL时的情况([WeldStatus],'')=''那么1节0结束)AS仍然存在   FROM [SPMS2]。[dbo]。[JointHistory] group by PackageId;