-1

如何将表中更多可用的数据列添加到此查询中?我想添加evnt_datevnt_desrp列。

SELECT DISTINCT 
        ROW_NUMBER() OVER (ORDER BY 
                                panel_descrp   
                            ,   cardno
                            ,   lname
                           ) rid 
    ,   panel_descrp   
    ,   cardno   
    ,   lname

FROM ev_log

where evnt_descrp = 'local grant' 
      and datepart(yyyy,EVNT_DAT) = datepart(yyyy,GETDATE()) 
      and DATEPART(mm,evnt_dat)   = DATEPART(mm,getdate()) -1
      and DATEPART(dd,evnt_dat)  >= 1 and DATEPART(dd,evnt_dat)<=2

    GROUP BY  
        panel_descrp   
    ,   cardno 
    ,   lname
4

1 回答 1

0

如果你想要它们,你必须将它们包含在GROUP BY子句中:

SELECT DISTINCT 
        ROW_NUMBER() OVER (ORDER BY 
                                panel_descrp   
                            ,   cardno
                            ,   lname
                           ) rid 
    ,   panel_descrp   
    ,   cardno   
    ,   lname
    ,   evnt_dat
    ,   evnt_desrp

FROM ev_log

where evnt_descrp = 'local grant' 
      and datepart(yyyy,EVNT_DAT) = datepart(yyyy,GETDATE()) 
      and DATEPART(mm,evnt_dat)   = DATEPART(mm,getdate()) -1
      and DATEPART(dd,evnt_dat)  >= 1 and DATEPART(dd,evnt_dat)<=2

    GROUP BY  
        panel_descrp   
    ,   cardno 
    ,   lname
    ,   evnt_dat
    ,   evnt_desrp
;

或者,对它们使用一些聚合函数,例如MAX(那么您不必将它们放在GROUP BY子句中,但查询的含义可能不是您所期望的):

SELECT DISTINCT 
        ROW_NUMBER() OVER (ORDER BY 
                                panel_descrp   
                            ,   cardno
                            ,   lname
                           ) rid 
    ,   panel_descrp   
    ,   cardno   
    ,   lname
    ,   MAX(evnt_dat)
    ,   MAX(evnt_desrp)
FROM ev_log

where evnt_descrp = 'local grant' 
      and datepart(yyyy,EVNT_DAT) = datepart(yyyy,GETDATE()) 
      and DATEPART(mm,evnt_dat)   = DATEPART(mm,getdate()) -1
      and DATEPART(dd,evnt_dat)  >= 1 and DATEPART(dd,evnt_dat)<=2

    GROUP BY  
        panel_descrp   
    ,   cardno 
    ,   lname
;
于 2013-11-08T00:13:08.017 回答