0

Good morning, I have the following table with the following records

Mov Descip  Correlativo Total
25558   AAAAAAAA    1   540
25558   AAAAAAAA    2   540
25559   BBBBBBBBB   3   40
25560   CCCCCCCCC   4   50
25561   DDDDDDDD    5   120
25561   DDDDDDDD    6   120
25561   DDDDDDDD    7   120

Do not know how to do a query to show me but without repeating records, I have tried with DISTINCT does not work.

4
  • Do show us your query and what you want to achieve?? Sep 19, 2013 at 15:32
  • 4
    You do not have any repeating records, they are all different because the Correlativo column is different in every row. Sep 19, 2013 at 15:32
  • You most likely want to GROUP BY whatever columns you consider relevant to a record's uniqueness.
    – TehShrike
    Sep 19, 2013 at 15:36
  • Ok Celso, sigo sin entender ... en el ejemplo de arriba exactamente QUE seria lo que necesitas que el query te devuelva ?
    – Rostol
    Oct 1, 2013 at 16:15

2 Answers 2

3

Depends what you want to get out of this table:

Mov Descip Correlativo Total 
25558 AAAAAAAA 1 540 
25558 AAAAAAAA 2 540 
25559 BBBBBBBBB 3 40 
25560 CCCCCCCCC 4 50 
25561 DDDDDDDD 5 120 
25561 DDDDDDDD 6 120 
25561 DDDDDDDD 7 120

These rows are all distinct, because Correlativo is unique, so the DISTINCT keyword will return all seven rows:

SELECT DISTINCT Mov, Descip, Correlativo, Total
FROM YourTableName

If you remove Correlativo from the select list, then the DISTINCT should pare it down to four rows:

SELECT DISTINCT Mov, Descip, Total
FROM YourTableName

You can also use GROUP BY, but then you have to use an aggregate function to tell SQL Server what to do with the multiple values of Correlativo. For example, you could use MIN(), MAX(), or SUM():

SELECT Mov, Descip, SUM(Correlativo), Total
FROM YourTableName
GROUP BY Mov, Descip, Total
2
  • Total value should not add, the value Correlative is AutoNumber, the value is just a value Movement Mov repeated on several occasions if you find references, is a logic that is used here, so my only care a record of the two movements but with all fields, I've been Biendo the CROSS APPLY, but no use. Sep 19, 2013 at 17:42
  • If I understand you correctly, you want to exclude the Correlativo Identity column from the select list and use DISTINCT. Sep 19, 2013 at 18:36
0

Your question is a little light on details, as far as i can see:

  • Correlativo (as the name in spanish implies): Is ID (autonumber?) Obviously different on each row.
  • Mov abreviación de Movimiento?): Is the movment id? or mover id?
  • Descrip: is just a text generic description
  • Total: is the total you want to add

If those are correct, and building on top of critialfix's solution, adding totals per Mov would be:

SELECT Mov, Sum (Total)
FROM YourTableName
GROUP BY Mov

Or if Descrip is needed:

SELECT Mov, Descip, Sum(Total)
FROM YourTableName
GROUP BY Mov, Descip
1
  • Total value should not add, the value Correlative is AutoNumber, the value is just a value Movement Mov repeated on several occasions if you find references, is a logic that is used here, so my only care a record of the two movements but with all fields, I've been Biendo the CROSS APPLY, but no use. Sep 19, 2013 at 17:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.