-1

テーブル内の使用可能なデータの列をこのクエリに追加するにはどうすればよいですか? evnt_dat列と列を追加したいevnt_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 に答える