Hi
I have below Data and i want if same DocNum repeats multiple times then RoundDif,InvoiceTotal should be displayed only once.
I want in Sql Query
| Invoice No | Quantity | Basic Value | Tax Amount | Round Diff | Invoice Total |
| 500273 | 513 | 18,940.00 | 2,272.80 | -0.27 | 23,529.00 |
| 500273 | 309 | 1,963.11 | 353.36 | -0.27 | 23,529.00 |
| 500286 | 965 | 3,840.55 | 460.87 | -0.42 | 4,301.00 |
Thanks

Eliana BlakeRank #1,595 · 993 reputationPosted Feb 28, 2025, 6:41 AM EST
To achieve the desired outcome where the Round Diff and Invoice Total are displayed only once for each DocNum, you can leverage SQL to aggregate the data using GROUP BY. Here's an example SQL query based on your provided table structure:
SELECT [Invoice No], SUM(Quantity) AS TotalQuantity, SUM([Basic Value]) AS TotalBasicValue, SUM([Tax Amount]) AS TotalTaxAmount, MAX([Round Diff]) AS RoundDiff, -- Displaying the Round Diff only once MAX([Invoice Total]) AS InvoiceTotal -- Displaying the Invoice Total only once FROM YourTableName -- Replace YourTableName with the actual table name GROUP BY [Invoice No];In this query:
- We are grouping the data by [Invoice No].
- The SUM function is used to calculate the total Quantity, Basic Value, and Tax Amount for each unique [Invoice No].
- The MAX function is used to display the Round Diff and Invoice Total only once for each [Invoice No].
By running this SQL query on your dataset, you should be able to achieve the desired result by consolidating the Round Diff and Invoice Total values for each unique Invoice No.
If you encounter any issues or need further clarification, feel free to ask!
Tuhin PaulRank #7 · 71.9K reputationPosted Feb 28, 2025, 7:23 AM EST
To achieve the desired output where
Round DiffandInvoice Totalare displayed only once for repeatedDocNumvalues, you can use a ROW_NUMBER() function in SQL to identify the first occurrence of eachDocNum. Then, you can conditionally display theRound DiffandInvoice Totalonly for the first row of each group.Tuhin PaulRank #7 · 71.9K reputationPosted Feb 28, 2025, 7:23 AM EST
WITH RankedData AS ( SELECT [Invoice No], Quantity, [Basic Value], [Tax Amount], [Round Diff], [Invoice Total], ROW_NUMBER() OVER (PARTITION BY [Invoice No] ORDER BY (SELECT NULL)) AS RowNum FROM YourTable ) SELECT [Invoice No], Quantity, [Basic Value], [Tax Amount], CASE WHEN RowNum = 1 THEN [Round Diff] ELSE NULL END AS [Round Diff], CASE WHEN RowNum = 1 THEN [Invoice Total] ELSE NULL END AS [Invoice Total] FROM RankedData ORDER BY [Invoice No], RowNum;ROW_NUMBER()Function :ROW_NUMBER()function assigns a unique number to each row within a group of rows (PARTITION BY [Invoice No]).ORDER BY (SELECT NULL)ensures that the rows are numbered arbitrarily within each group. You can replace(SELECT NULL)with a specific column (e.g.,Quantity) if you want to control the ordering.CASEStatement :CASEstatement checks if theRowNumis1(i.e., the first occurrence of theInvoice No).RowNum = 1, it displays theRound DiffandInvoice Total. Otherwise, it sets these columns toNULL.WITHClause (Common Table Expression) :WITH RankedDataclause creates a temporary result set that includes theRowNumcolumn for each row.SELECTstatement.Final Output :
SELECTstatement retrieves all columns but conditionally displaysRound DiffandInvoice Totalonly for the first row of eachInvoice No.Tuhin PaulRank #7 · 71.9K reputationPosted Feb 28, 2025, 7:24 AM EST
Input Data
Invoice No
Quantity
Basic Value
Tax Amount
Round Diff
Invoice Total
500273
513
18,940.00
2,272.80
-0.27
23,529.00
500273
309
1,963.11
353.36
-0.27
23,529.00
500286
965
3,840.55
460.87
-0.42
4,301.00
Output
Invoice No
Quantity
Basic Value
Tax Amount
Round Diff
Invoice Total
500273
513
18,940.00
2,272.80
-0.27
23,529.00
500273
309
1,963.11
353.36
NULL
NULL
500286
965
3,840.55
460.87
-0.42
4,301.00
Tuhin PaulRank #7 · 71.9K reputationPosted Feb 28, 2025, 7:24 AM EST
If you don’t need to retain all rows and only want one row per
Invoice No, you can use aGROUP BYquery:SELECT [Invoice No], SUM(Quantity) AS TotalQuantity, SUM([Basic Value]) AS TotalBasicValue, SUM([Tax Amount]) AS TotalTaxAmount, MAX([Round Diff]) AS [Round Diff], MAX([Invoice Total]) AS [Invoice Total] FROM YourTable GROUP BY [Invoice No];This approach aggregates the data, displaying only one row per
Invoice No.