Hi
I have below code and i want if Location has 0 value in all six columns then it should not be displayed.
Select T0."Location,
(Select Sum(A1."LineTotal") from OPCH A0 inner join PCH1 A1 on A0."DocEntry" = A1."DocEntry"
inner join Oitm A2 on A1."ItemCode" = A2."ItemCode" and A0."CANCELED" not in ('Y','C') and A2."U_A_M" = 'EVE_EXP' and A0."BPLId" = T0."BPLId" and A0."DocDate" >= FromDate and A0."DocDate"<= ToDate) As "Accounts",
(Select Sum(A1."LineTotal") from OPCH A0 inner join PCH1 A1 on A0."DocEntry" = A1."DocEntry"
inner join Oitm A2 on A1."ItemCode" = A2."ItemCode" and A0."CANCELED" not in ('Y','C') and A2."U_A_M" = 'PR_MKT' and A0."BPLId" = T0."BPLId" and A0."DocDate" >= FromDate and A0."DocDate"<= ToDate) As "Sale",
from OBPL T0
Thanks
Tuhin PaulPosted Mar 8, 2025, 6:33 AM
COALESCEFunction :NULL, it is treated as0. This prevents errors when summing up the values.WHEREClause :0. If the total is0, the row is excluded from the result.Subqueries :
WHEREclause for filtering.Tuhin PaulPosted Mar 8, 2025, 6:31 AM
To ensure that locations with 0 values in all six columns are not displayed, you need to filter out such rows. This can be achieved by adding a
WHEREclause that checks if the sum of all six columns is greater than 0.Eliana BlakePosted Mar 8, 2025, 5:19 AM
Hi! I understand that you are looking to refine your SQL query to exclude displaying records where the Location has zero values in all six columns. To achieve this in SQL, you can use a HAVING clause after performing the necessary calculations. The HAVING clause can conditionally filter records based on aggregated values, which in this case would be checking if all six columns have a sum greater than zero.
Here's an example of how you could modify your SQL query to account for this condition:
In this revised query:
- The GROUP BY clause groups the results by Location.
- The HAVING clause ensures that only records with non-zero sums in both "Accounts" and "Sale" columns are returned.
By incorporating this modification, your SQL query will display only those records where the Location has non-zero values in at least one of the specified columns. This refinement should help achieve your goal of excluding records with zero values in all six columns. Let me know if you need further assistance or if you have any other questions!