Member since
03-27-2023
12
Posts
5
Kudos Received
0
Solutions
08-14-2024
12:07 AM
1 Kudo
So I can't seem to sum up correctly to show that there is no difference, even though I've verified in Excel that there is no difference- and that Column A + Column B = Column C in all cases. It appears to be adding or subtracting incorrectly at times- no matter if I use minus or add(even tried switching them around), or using ABS(absolute). Data from table Sales: Currency ColA ColB Total_ColC AED 2,600,0000 {null} 2,600,000 AUD 465,000 (406,000) 59,000 CAD (1,450,000,000) 35,000,000 (1,415,000,000) CHF 490,000 (114,000) (376,000) CNH (216,000) 2,200,000 1,984,000 This is the query that I'm trying to double-check that the sum of column ColA + ColB = column Total_ColC: SELECT currency, CASE WHEN SUM(COALESCE(ColA,0)) + SUM(COALESCE(ColB,0)) = (SUM(COALESCE(Total_ColC,0))) THEN 0 ELSE SUM(COALESCE(ColA,0)) + SUM(COALESCE(ColB,0)) - (SUM(COALESCE(Total_ColC,0))) END AS Total_Check FROM `reports`.sales WHERE DateOfBusiness = '2024-08-10' GROUP BY Currency Here are the incorrect results that I get for some rows- notably, anything that has a negative value. Currency Total_Check AED 0 AUD (871,000) CAD (2,900,000,000) CHF (228,000) CNH (4,400,000) I expect for all rows in Total_Check to be 0... Thanks!
... View more
Labels:
- Labels:
-
Cloudera Hue
08-07-2024
06:33 AM
After adding a ) to both lines(which Hive didn't like not having), it works for some of the currencies- but others are not adding up properly- ie, they'll double the negative values(when there).
... View more
08-06-2024
03:29 PM
1 Kudo
Hey all, So I'm trying to check some columns together to ensure that the sums of A & B equal to row C, for each given currency. Table Sales; Currency ColA ColB ColC AUD 50,000 50,000 100,000 CAD {null} 20,000 20,000 MZN 30,000 {null} 30,000 YER 10,000 10,000 20,000 Query that I can't seem to get working properly: SELECT Currency, CAST((SUM)CASE WHEN((coalesce(ColA,0)) + (coalesce(ColB,0))) = (coalesce(ColC,0)) THEN 0 ELSE SUM((coalesce(ColA,0)) + (coalesce(ColB,0))) - (coalesce(ColC,0) END AS sales_check) The output that I'd like to get: Currency sales_check AUD 0 CAD 0 MZN 0 YER 0 Thanks,
... View more
Labels:
- Labels:
-
Cloudera Hue
07-02-2024
12:02 PM
2 Kudos
So I have 2 values from different tables that i'm trying to join and aggregate. The problem is that i get results where the math doesn't add up at times- and have tried changing the join\inner join, group by, select distinct, cast- it doesn't seem to work. I need coalesce, as there are null values. Dataset: Table Sales: Currency ColA ColB ColC DateField AUD 208,883 {null} {null} '2024-06-30' CAD 215,913 {null} {null} '2024-06-30' MZN 0 0 {null} '2024-06-30' YER 0 {null} 10 '2024-06-30' Table OTHER_Sales: Currency Col1 Col2 Col3 DateField AUD {null} 208,883 20 '2024-06-30' CAD {null} 215,913 0 '2024-06-30' MZN {null} 0 13 '2024-06-30' YER {null} 0 0 '2024-06-30' Query: SELECT a.currency as currency, SUM((coalesce(a.ColA,0)) + (coalesce(b.Col2))) as sales_check FROM db.sales a inner join db.OTHER_sales b ON a.currency = b.currency WHERE a.DateField = '2024-06-30' GROUP BY a.currency The incorrect results that I currently get: currency sales_check AUD -1645761638.790000 CAD 5855379123.770950 MZN 0 YER 0 The correct results that I need to get: currency sales_check AUD 0 CAD 0 MZN 0 YER 0 Thanks!
... View more
Labels:
- Labels:
-
Cloudera Hue
05-10-2024
10:39 AM
1 Kudo
Thanks! because i have null values in my data set as well, i used colaesce, and it worked! Your query was the basis though, so thanks again! Query by @nramanaiah that worked for me, as I have null records in the dataset: select Currency, (coalesce(spend_a,0)) + (colaesce(spend_b,0)) + coalesce(spend_c,0)) + coalesce(spend_d,0)) as total_spend from test
... View more
05-09-2024
12:02 PM
Thanks @nramanaiah. I realized that I gave a bad example before. Apologies. Table CustCurr: Currency Spend_A Spend_B Spend_C Spend_D GBP 50 25 (20) 5 JPY 50 25 (20) 5 AUD 50 25 (20) 5 The Output that I'd like to get(as a check- can't create temp\tables as i only have read access) is: Currency total_spend_check A_and_C B_C_D GBP 80 30 10 JPY 80 30 10 AUD 80 30 10 The query that I'm trying is: SELECT Currency, SUM(Spend_A + Spend_B, + Spend_C + Spend_D) AS total_spend_check FROM CustCurr WHERE DateofAction = '2024-05-01' GROUP BY Currency Here's the incorrect result that I get with above query: currency total_spend_check GBP null JPY null AUD null I've tried coalesce, cast(field as int) with no avail.
... View more
04-25-2024
10:24 AM
Understood- so okay, added Customer ID to Sales table; Customer table: Customer ID Money Spent 0001 500 0002 250 0003 100 Sales table: Customer ID Order ID Amount 0001 5005 500 0002 5006 250 0003 5007 50 Check sales math work in progress query(the query doesn't seem to work, returns a "delta" column with a {null} result: SELECT CAST(SUM(Customer.Money Spent - Sales.Amount) AS INT) AS DELTA The desired output that I'd like: Check table Rule Name Delta Sales Check 0 Sales Check 0 Sales Check 50
... View more
04-23-2024
12:22 PM
Hi all, I'm looking to create a query to check and validate the math on certain fields from different tables. I've found examples where the SUM function was used- but I struggled putting it together, and am just trying to find the best way to do these calcuations. Sample data: Customer table: Customer ID Money Spent 0001 500 0002 250 0003 100 Sales table: Order ID Amount 5005 500 5006 250 5007 50 Check sales math work in progress query(the query doesn't seem to work, returns a "delta" column with a {null} result: SELECT CAST(SUM(Customer.Money Spent - Sales.Amount) AS INT) AS DELTA The desired output that I'd like: Check table Rule Name Delta Sales Check 0 Sales Check 0 Sales Check 50 Thanks in advance,
... View more
Labels:
- Labels:
-
Cloudera Hue
05-03-2023
07:33 AM
Hi all, I have a query that I've found here thanks to @mszurap which I've tweaked and am trying to get working. I have a field called "RunNumber" that identifies if\when data is appended- it can have a value of 1 or 2 usually, but is not limited to 2. I can't use Group By, as you'll see in my query- there are hundreds and thousands of CASE statements. I've tried and cannot seem to get it working in Hive. I get a "..Expression not in GROUP BY key '0'..." Query: SELECT MaxRun = MAX(RunNumber) ,FirstName = SUM(CASE WHEN FirstName IS NULL OR FirstName = 'NULL' OR FirstName = ' ' THEN 1 ELSE 0 END) AS FirstName ,LastName = SUM(CASE WHEN LastName IS NULL OR LastName = 'NULL' OR LastName = ' ' THEN 1 ELSE 0 END) AS LastName ,State = SUM(CASE WHEN State IS NULL OR State = 'NULL' OR State = ' ' THEN 1 ELSE 0 END) AS State FROM Table.Customers WHERE DateOfRecord = '2023-05-01' The output that I'd like to get: MaxRun FirstName LastName State 1 500 750 1000
... View more
Labels:
- Labels:
-
Cloudera Hue
03-29-2023
09:12 AM
Thanks for the info! I am educating myself and certainly appreciate it. Using NVL2, it looks like it either returns a 0 if a null is found, or the specified expression in the argument. Even if I used Impala, NVL2 wouldn't work for me- as I need(and expect) the specific number count of null records across given columns, right? Just wondering. Thanks again!
... View more