Reputation: 1
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.
Upvotes: 0
Views: 76
Reputation: 209
Your question is a little light on details, as far as i can see:
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
Upvotes: 0
Reputation: 2870
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
Upvotes: 3