select distinct

  • Hello

    i´ve this query

    SELECT distinct BO.NMDOS,bo.ETOTALDEB, bo.tpdesc ,bo.dataobra,BO.NOME ,BO.OBRANO,ft.nmdoc,FT.FNO,CASE

    WHEN FT.NDOC<> 1

    THEN 0

    ELSE FT.ETTILIQ

    END as etiliquido

    FROM BO left JOIN BI ON bi.bostamp=bo.bostamp left JOIN FI ON fi.bistamp=bi.bistamp

    left JOIN FT ON FT.FTSTAMP=FI.FTSTAMP WHERE BO.ndos='18' and bo.fechada=0

    GROUP BY BO.NMDOS,bo.DATAOBRA,BO.NOME,BO.OBRANO,BI.OBRANO,FT.FNO,FT.ETTILIQ,bo.tpdesc ,ft.nmdoc,bo.ETOTALDEB,ft.ndoc

    order by bo.DATAOBRA

    --- that returns this something like this--

    Dossier 1|1000,00|10|20130210|client|999|Invoice|1|150,00

    Dossier 1|1000,00|10|20130210|client|999|Invoice|4|250,00

    Dossier 1|1000,00|10|20130210|client|999|Invoice|6|250,00

    .... and goes on...

    my question ? can i remove the duplicate row value 1000,00, to return only the first, to something like this

    Dossier 1|1000,00|10|20130210|client|999|Invoice|1|150,00

    Dossier 1|Null|10|20130210|client|999|Invoice|4|250,00

    Dossier 1|Null|10|20130210|client|999|Invoice|6|250,00

    thanks in advance

  • Without a lot of information, I guess you could do something like this.

    SELECT distinct BO.NMDOS,

    CASE WHEN FT.FNO = MIN(FT.FNO) OVER(PARTITION BY BO.NMDOS) THEN bo.ETOTALDEB END AS ETOTALDEB ,

    bo.tpdesc ,

    bo.dataobra,

    BO.NOME ,

    BO.OBRANO,

    ft.nmdoc,

    FT.FNO,

    CASE WHEN FT.NDOC<> 1

    THEN 0

    ELSE FT.ETTILIQ

    END as etiliquido

    FROM BO left JOIN BI ON bi.bostamp=bo.bostamp left JOIN FI ON fi.bistamp=bi.bistamp

    left JOIN FT ON FT.FTSTAMP=FI.FTSTAMP WHERE BO.ndos='18' and bo.fechada=0

    GROUP BY BO.NMDOS,bo.DATAOBRA,BO.NOME,BO.OBRANO,BI.OBRANO,FT.FNO,FT.ETTILIQ,bo.tpdesc ,ft.nmdoc,bo.ETOTALDEB,ft.ndoc

    order by bo.DATAOBRA

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • First of all, many thanks for your response

    it returned

    Dossier 1|Null|10|20130210|client|999|Invoice|1|150,00

    Dossier 1|Null|10|20130210|client|999|Invoice|4|250,00

    Dossier 1|Null|10|20130210|client|999|Invoice|6|250,00

    and i need that first value 1000,00

    Dossier 1|1000,00|10|20130210|client|999|Invoice|1|150,00

    Dossier 1|Null|10|20130210|client|999|Invoice|4|250,00

    Dossier 1|Null|10|20130210|client|999|Invoice|6|250,00

  • Maybe this?

    select NMDOS, case RowNum when 1 then ETOTALDEB else NULL end as ETOTALDEB, tpdesc, dataobra, NOME, OBRANO, nmdoc, FNO, etiliquido

    from

    (

    SELECT distinct BO.NMDOS,bo.ETOTALDEB, bo.tpdesc ,bo.dataobra,BO.NOME ,BO.OBRANO,ft.nmdoc,FT.FNO,CASE

    WHEN FT.NDOC<> 1

    THEN 0

    ELSE FT.ETTILIQ

    END as etiliquido, ROW_NUMBER() over (Partition by bo.ETOTALDEB order by bo.DATAOBRA) as RowNum

    FROM BO left JOIN BI ON bi.bostamp=bo.bostamp left JOIN FI ON fi.bistamp=bi.bistamp

    left JOIN FT ON FT.FTSTAMP=FI.FTSTAMP WHERE BO.ndos='18' and bo.fechada=0

    GROUP BY BO.NMDOS,bo.DATAOBRA,BO.NOME,BO.OBRANO,BI.OBRANO,FT.FNO,FT.ETTILIQ,bo.tpdesc ,ft.nmdoc,bo.ETOTALDEB,ft.ndoc

    ) x

    order by x.DATAOBRA

    The problem here is that we are shooting in the dark. We can't see your tables, have no concept of your data or what you are trying to do. We can't test the code we post and we have no real idea if it will work.

    If this doesn't do it then please take a few minutes and read the first article in my signature for best practices when posting questions.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • it worked as i intended.

    Next time i post something, i definitly will put more information ( tables, data, fields...), to help you help me!

    Thank you very much for your time and patience.

  • Here's another way. Just uncomment your code and comment out the sample data.

    WITH CTE (NMDOS, ETOTALDEB, tpdesc, dataobra, NOME, OBRANO, nmdoc, FNO, etiliquido)

    AS (

    --SELECT distinct BO.NMDOS,bo.ETOTALDEB, bo.tpdesc ,bo.dataobra,BO.NOME ,BO.OBRANO,ft.nmdoc,FT.FNO,CASE

    --WHEN FT.NDOC<> 1

    --THEN 0

    --ELSE FT.ETTILIQ

    --END as etiliquido

    --FROM BO left JOIN BI ON bi.bostamp=bo.bostamp left JOIN FI ON fi.bistamp=bi.bistamp

    --left JOIN FT ON FT.FTSTAMP=FI.FTSTAMP WHERE BO.ndos='18' and bo.fechada=0

    --GROUP BY BO.NMDOS,bo.DATAOBRA,BO.NOME,BO.OBRANO,BI.OBRANO,FT.FNO,FT.ETTILIQ,bo.tpdesc ,ft.nmdoc,bo.ETOTALDEB,ft.ndoc

    SELECT 'Dossier 1',1000.00,10,20130210,'client',999,'Invoice',1.150,00

    UNION ALL SELECT 'Dossier 1',1000.00,10,20130210,'client',999,'Invoice',4.250,00

    UNION ALL SELECT 'Dossier 1',1000.00,10,20130210,'client',999,'Invoice',6.250,00

    )

    SELECT NMDOS, CASE rn WHEN 1 THEN ETOTALDEB ELSE NULL END

    ,tpdesc, dataobra, NOME, OBRANO, nmdoc, FNO, etiliquido

    FROM (

    SELECT *, rn=ROW_NUMBER() OVER (PARTITION BY NMDOS ORDER BY dataobra)

    FROM CTE) a

    ORDER BY DATAOBRA


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

Viewing 6 posts - 1 through 5 (of 5 total)

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