Welcome to Sign in | Help
in Search

select cu afisare totala

Last post 07-25-2009, 8:17 AM by rsocol. 2 replies.
Sort Posts: Previous Next
  •  07-24-2009, 6:03 PM 7422

    select cu afisare totala

    se da tabela:

    Nume | CodMare | CallDate
    N1 C1 7.15.2009
    N2 C2 7.28.2009
    N3 C3 7.16.2009
    N2 C2 7.29.2009


    se doreste selectarea numarului total de apeluri care persoanele N(x), efectuate in perioada
    @firstDayOfPrevMonth -@mydate.

    luand de ex. @mydate=24.07.2008, selectul o sa intoarca persoanele N1 si N3 de forma

    Nume | CodMare | Total
    N1 C1 1
    N2 C2 1

    ce vreau insa, este ca si clientul N2 sa apara in rezultat, cu 0 la total, deci:

    Nume | CodMare | Total
    N1 C1 1
    N2 C2 0
    N3 C3 1

    mai jos e ceea ce am incercat, si-mi intoarce rezultatul fara N2



    DECLARE @mydate DATETIME, @firstDayOfPrevMonth datetime,@firstDayOfCurentMonth datetime
    set @mydate = GETDATE(); --pt exemplu, data de azi, 24 iulie 2009
    set @firstDayOfPrevMonth = CONVERT(VARCHAR(25),DATEADD(dd,-(DAY(DATEADD(mm,1,@mydate))-1),DATEADD(mm,-1,@mydate)),101);
    set @firstDayOfCurentMonth = CONVERT(VARCHAR(25),DATEADD(dd,-(DAY(@mydate)-1),@mydate),101)



    select nume,convert(VARCHAR(25),(case when count(distinct h.codmare) is null then 0 else 1 end)) from Table1 h
    where calldate between @firstDayOfCurentMonth and @mydate
    GROUP BY h.nume
  •  07-24-2009, 9:18 PM 7423 in reply to 7422

    Re: select cu afisare totala

    SQL 2000 sau SQL 2005?
  •  07-25-2009, 8:17 AM 7428 in reply to 7422

    Re: select cu afisare totala

    Încearcă ceva de genul:

    SELECT Nume,CodMare,
    SUM(CASE WHEN calldate BETWEEN @firstDayOfCurentMonth AND @mydate THEN 1 ELSE 0 END) AS Total
    FROM Table1 GROUP BY Nume

    Răzvan
View as RSS news feed in XML
Powered by Community Server (Commercial Edition), by Telligent Systems