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
Tuhin PaulPosted Feb 28, 2025, 12:24 PM
If you don’t need to retain all rows and only want one row per
Invoice No, you can use aGROUP BYquery:This approach aggregates the data, displaying only one row per
Invoice No.Tuhin PaulPosted Feb 28, 2025, 12:24 PM
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 PaulPosted Feb 28, 2025, 12:23 PM
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 PaulPosted Feb 28, 2025, 12:23 PM
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.Eliana BlakePosted Feb 28, 2025, 11:41 AM
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:
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!