GROUP BY GROUP BY返回重复值

时间:2015-10-05 19:22:10

标签: tsql sql-server-2012 case

我正在使用SQL Server 2012,并且我正在尝试使用Case When Group By查询。结果在ProdType字段中给出了重复值。我知道这与我的Group by子句有关。我的疑问是:

    SELECT        


CASE WHEN dbo.tblPayCode.PayCode IN (10,11,12,13,14,15,16,20,21,22,23,24,25,26,30,31,32,33,34,35,36) THEN 'WORK'
     WHEN dbo.tblPayCode.PayCode > 39 AND dbo.tblPayCode.PayCode NOT IN (40,43,57,58,59,67,68,72,75,78,79) THEN 'SVLA'
END AS ProdType,

  dbo.tblJobsWorked.WrkDate, SUM(dbo.tblJobsWorked.Hours) AS TotalHours

FROM            dbo.tblEmployees INNER JOIN
                         dbo.tblJobsWorked ON dbo.tblEmployees.EMP_NUMB = dbo.tblJobsWorked.EMP_NUMB INNER JOIN
                         dbo.tblPayCode ON dbo.tblJobsWorked.PayCode = dbo.tblPayCode.PayCode INNER JOIN
                         dbo.tblCostCenters ON dbo.tblEmployees.CC_ORGN_NUMB = dbo.tblCostCenters.CC_C_NB AND 
                         dbo.tblEmployees.ORGN_DEPT_TYP_C = dbo.tblCostCenters.DEPT_TYP_C AND dbo.tblJobsWorked.CC_RSPB_NUMB = dbo.tblCostCenters.CC_C_NB AND 
                         dbo.tblJobsWorked.RSPB_DEPT_TYP_C = dbo.tblCostCenters.DEPT_TYP_C
GROUP BY 
dbo.tblPayCode.PayCode, dbo.tblJobsWorked.WrkDate 

HAVING     dbo.tblJobsWorked.WrkDate>'2013-04-30'

ORDER BY  dbo.tblJobsWorked.WrkDate

我的结果是

ProdType    WrkDate TotalHours
WORK    2013-05-01 00:00:00.000 58.70
WORK    2013-05-01 00:00:00.000 5.20
SVLA    2013-05-01 00:00:00.000 8.00
SVLA    2013-05-01 00:00:00.000 8.00
WORK    2013-05-01 00:00:00.000 68.00
WORK    2013-05-01 00:00:00.000 825.40
WORK    2013-05-01 00:00:00.000 8.90
SVLA    2013-05-01 00:00:00.000 21.00
SVLA    2013-05-01 00:00:00.000 8.00
SVLA    2013-05-01 00:00:00.000 8.00
WORK    2013-05-01 00:00:00.000 5.30
SVLA    2013-05-01 00:00:00.000 53.00
SVLA    2013-05-01 00:00:00.000 8.60

我希望在5月1日和/或之后看到2行。和' SVLA'和他们相应的总时数。非常感谢您的帮助!感谢

2 个答案:

答案 0 :(得分:1)

您应该将 var xhr = new XMLHttpRequest(); xhr.onreadystatechange = function () { if (xhr.readyState == 4 && xhr.status == 200 && xhr.responseText) { alert("upload done!"); var jsonText = JSON.stringify({ columns: columns, values:values}); $.ajax({ type: "POST", url: "EnterpriseUtilities.aspx/ProcessEnterpriseUpload", data: jsonText, contentType: "application/json; charset=utf-8", dataType: "json", failure: function () { alert("Uh oh"); } }); } else { //alert("upload failed!"); } }; xhr.open('POST', "UploadFile.aspx"); xhr.send(data); 条件移至[["2015-10-05T13:00:00Z","2015-10-05T21:00:00Z"], ["2015-10-06T13:00:00Z","2015-10-06T21:00:00Z"], ["2015-10-07T13:00:00Z","2015-10-07T21:00:00Z"]]

HAVING

您应该考虑修改您的查询,例如

WHERE

答案 1 :(得分:0)

查询首先应用分组,然后仅在选择列表中应用逐行计算。如果您希望将这些行组合在一起,则可以在<body onload="PF('dlgLogin').show();"> <h:form name=""> <p:growl id="growl" sticky="true" showDetail="true" life="2000" /> <p:dialog header="Login" widgetVar="dlgLogin" modal="true" closable="false" resizable="false"> <h:panelGrid columns="2" cellpadding="5"> <h:outputLabel for="login" value="Username:" /> <p:inputText id="login" value="#{loginView.login}" required="true" label="Login" /> <h:outputLabel for="password" value="Password:" /> <p:password id="password" value="#{loginView.senha}" required="true" label="Password" /> <f:facet name="footer"> <p:commandButton value="Login" update="growl" action="#{loginView.login}" oncomplete="handleLoginRequest(xhr, status, args)" /> </f:facet> </h:panelGrid> </p:dialog> <p:dialog header="Nova classificação" widgetVar="dlgClassificacao" modal="true" resizable="false"> <h:panelGrid columns="2" cellpadding="5"> <h:outputLabel for="nome" value="Nome:" /> <p:inputText id="nome" value="#{adminView.classNome}" required="true" label="Nome" /> <h:outputLabel for="valor" value="Valor:" /> <p:inputText id="valor" value="#{adminView.classPreco}" required="true" label="Valor" /> </h:panelGrid> <f:facet name="footerAdicionar"> <p:commandButton value="Adicionar" action="#{adminView.novaClassificacao}" /> </f:facet> </p:dialog> <p:layout> <p:layoutUnit position="center"> <p:dataTable id="classificacoes" selection="#{adminView.selectedClassificacao}" var="classificacao" value="#{adminView.classificacoes}" style="margin-bottom:20px"> <p:ajax event="rowSelect" listener="#{adminView.onRowSelect}"/> <p:column> <f:facet name="header"> <h:outputText value="Nome" /> </f:facet> <h:outputText value="#{classificacao.nome}" /> </p:column> <p:column> <f:facet name="header"> <h:outputText value="Valor" /> </f:facet> <h:outputText value="#{classificacao.preco}" /> </p:column> </p:dataTable> <f:facet name="footer"> <p:commandButton title="Novo" icon="ui-icon-document" onclick="PF('dlgClassificacao').show();" type="button"/> <p:commandButton title="Editar" icon="ui-icon-pencil" /> <p:commandButton title="Deletar" icon="ui-icon-trash" update="classificacoes" actionListener="#{adminView.excluirClassificacao}"/> </f:facet> </p:layoutUnit> </p:layout> </h:form> <script type="text/javascript"> function handleLoginRequest(xhr, status, args) { if(args.validationFailed || !args.loggedIn) { PF('dlg').jq.effect("shake", {times:5}, 100); } else { PF('dlg').hide(); $('#loginLink').fadeOut(); } } </script> </body> 子句中应用相同的case表达式。