How to sort the records correctly using DISTINCT

0

In my table I have the column DESCRICAO and the DATA_DE_CADASTRO of items. In my Android application I would like to display the last 10 records that were recorded.

There are a number of attributes that must be reported when writing a new item, so this list is for you to choose an item from which you can copy some attributes to the other item that is currently being recorded since both items share the same information.

In this way I would like to have the last 10 records distinctly because it does not make sense in this case to present you with a list of 10 repeating items.

I make select by ordering it from the DTCAD column in descending order.

SELECT DSBEM FROM TAB ORDER BY DTCAD DESC

And it brings me the following data:

Andthat'sright.TELEFONEDIGITALCOMVISORwasthelastitemtobewritten,howeverwehaveseveralrepeateditems,butifyouselect

SELECTDISTINCTDSBEMFROMTABORDERBYDTCADDESC

Nowyou'rewrong.Where'sthedigitalphone?

Ihavealreadycreatedtheindexesforthedescriptionfordtcad,fordescriptionanddtcad.

Thecommand

SELECTDISTINCTDSBEMFROM(SELECTDSBEMFROMTABORDERBYDTCADdesc)LIMIT10

alsodoesnotwork.

Whatcouldbedone?

Onepossiblesolutionwouldbethisquery.

SELECTDISTINCTDTCAD,DSBEMFROMTABorderbydatetime(DTCAD)desc

Butthereturnisthis:

    
asked by anonymous 07.09.2016 / 15:47

1 answer

0

Change to look like this:

SELECT DISTINCT DTCAD, DSBEM FROM TAB ORDER BY DTCAD DESC 

What is happening is that when you put the distinct by denomination the phone that is repeated adds up because you also do not consider the registration date as a differentiating factor. This setting will be considered as the uniqueness of the denomination AND date, thus making the phone registration reappear.

    
07.09.2016 / 17:13