Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

select distinct Expand / Collapse
Author
Message
Posted Thursday, July 25, 2013 12:30 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, March 18, 2014 2:48 AM
Points: 44, Visits: 132
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
Post #1477669
Posted Thursday, July 25, 2013 12:42 PM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 6:47 PM
Points: 2,788, Visits: 5,972
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.
I am a great believer in luck, and I find the harder I work the more I have of it. Stephen Leacock

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1477675
Posted Thursday, July 25, 2013 12:58 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, March 18, 2014 2:48 AM
Points: 44, Visits: 132
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
Post #1477688
Posted Thursday, July 25, 2013 1:08 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 3:28 PM
Points: 12,034, Visits: 11,062
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 Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1477693
Posted Thursday, July 25, 2013 3:07 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, March 18, 2014 2:48 AM
Points: 44, Visits: 132
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.
Post #1477755
Posted Thursday, July 25, 2013 7:37 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 5:39 PM
Points: 3,596, Visits: 5,113
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!

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?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
Post #1477824
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse