Help Whit Query

  • Hi,

    i have the followin query in a store procedure:

    select d.Dependencia, count(*) as TitulosAdquisicion

    from Dependencias D, EjemplaresSolicitados ES, Titulos T

    where ES.IDTitulo = T.IDTitulo

    and T.IDDependencia = D.IDDependencia

    and D.Dependencia = @Dependencia

    and ES.FechaRecepcion between @FechaIni and @FechaFin

    group by d.Dependencia

    having count(*)>=0

    is in a store procedure and recibes the parameters @Dependencia, @FechaIni , @FechaFin

    I need to know if i need to show all the "Dependencias" and not filter whit this parameter but i cant modify the query, which value can i put in the parameter @Dependencia for this purpose?

    thank you very much any help and Merry christmas for every body!

  • I need to know if i need to show all the "Dependencias" and not filter whit this parameter but i cant modify the query, which value can i put in the parameter @Dependencia for this purpose?

    just remove the line and D.Dependencia = @Dependencia

    and u're done..

    you should also remove the Dependencia parameter from the SP(as it will no longer be required after modification).



    Pradeep Singh

  • You need to have a key value of @Dependencia which means 'All', then use this:

    select d.Dependencia, count(*) as TitulosAdquisicion

    from Dependencias D, EjemplaresSolicitados ES, Titulos T

    where ES.IDTitulo = T.IDTitulo

    and T.IDDependencia = D.IDDependencia

    and D.Dependencia = CASE @Dependencia WHEN 'All' THEN D.Dependencia ELSE @Dependencia END

    and ES.FechaRecepcion between @FechaIni and @FechaFin

    group by d.Dependencia

    having count(*)>=0

    You could use and D.Dependencia = ISNULL(@Dependencia, D.Dependencia)if @Dependencia is set to NULL for all.

    Proper join syntax is recommended:

    select d.Dependencia, count(*) as TitulosAdquisicion

    from Dependencias D

    INNER JOIN Titulos T ON T.IDDependencia = D.IDDependencia

    INNER JOIN EjemplaresSolicitados ES ON ES.IDTitulo = T.IDTitulo

    where D.Dependencia = CASE @Dependencia WHEN 'All' THEN D.Dependencia ELSE @Dependencia END

    and ES.FechaRecepcion between @FechaIni and @FechaFin

    group by d.Dependencia

    --having count(*)>=0

    HAVING is a filter of the output from the aggregate: in this case, having count(*)>=0 doesn't do anything.

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • thank you all, it works perfectly!!

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply