Using an if-else statement in big-query with a IFNULL statement - sql

I have a delicate dilemma with follwing SQL statement in Bigquery:
ROUND(SUM(r.DeliveredQuantity * IFNULL(a.DirectCost,a.PurchasePrice))) AS TotalCost,
I need to change it to check if some articles are present or not and if they are, I have to do one expressiona and if not another expression.This is my check
IF(a.ArticleNumber NOT IN ("204","204.2","204.3","204.4","204.5","204.6","204.7")
If those articles are not present then I have to run following query:
ROUND(SUM(r.DeliveredQuantity * IFNULL(a.DirectCost,a.PurchasePrice)))
Otherwise ( and here is where the unfortunate part comes in I guess ) I have to run something like this:
ROUND(SUM(r.DeliveredQuantity * IFNULL(a.DirectCost,a.PurchasePrice) / (a.SalesPrice * i.CurrencyRate)))
So the whole code snippet I have so far is:
IF(a.ArticleNumber NOT IN ("204","204.2","204.3","204.4","204.5","204.6","204.7"),
ROUND(SUM(r.DeliveredQuantity * IFNULL(a.DirectCost,a.PurchasePrice))),
ROUND(SUM(r.DeliveredQuantity * IFNULL(a.DirectCost,a.PurchasePrice) / (a.SalesPrice * i.CurrencyRate)))
) AS TG,
The result is a error saying:
division by zero: 0 / 0
The IFNULL I have to run unfortunately since my users have not be consistent in their data input. But even when only using one of either choices inside the IFNULL, I get the same response saying division by zero: 0/0
Endresult is that IF certaina rticles are present, then I need to get those including the i.CurrencyRate, otherwise without i.CurrencyRate. Any ideas?
PS! I followed this tutorial

IF(a.ArticleNumber NOT IN ("204","204.2","204.3","204.4","204.5","204.6","204.7"),
ROUND(SUM(r.DeliveredQuantity * IFNULL(a.DirectCost,a.PurchasePrice))),
ROUND(SUM(SAFE_DIVIDE((r.DeliveredQuantity * IFNULL(a.DirectCost,a.PurchasePrice)) , (a.SalesPrice * i.CurrencyRate))))
) AS TG,

Related

MS Access query produces invalid procedure call

My query in access works fine if i only use the below query with a select statement. As soon as it becomes an append query it produces an "invalid procedure call" error.
I have narrowed down the offending columns as being "Publ" and "PublLong". Both are long text strings. If I remove these two columns the query updates without an error.
Here is a sample data point found in the [Bezeichung] Field:
publications.bank.com/publ-dl-ch/pdf/WhatsUp_20181113_en.pdf
I checked the table that it is being inserted to and the data types are the same nor did i see any other setting that would block the insertion.
How can i get it to work?
INSERT INTO tbl_MatomoRaw ( DownloadDate, IntExt, Publ, PublLong,
PublDate, [Language], Download_Visits, PublMonth )
SELECT
Date() AS DownloadDate,
Left([Bezeichnung],InStr([Bezeichnung],".")-1) AS IntExt,
Nz(Mid([Bezeichnung],InStrRev([Bezeichnung],"/")+1,InStr([Bezeichnung],"_")-
InStrRev([Bezeichnung],"/")-1),"") AS Publ,
Mid([Bezeichnung],InStrRev([Bezeichnung],"/")+1,InStrRev([Bezeichnung],"_")-
InStrRev([Bezeichnung],"/")-1) AS PublLong,
Mid([Bezeichnung],InStr([Bezeichnung],"_")+1,8) AS PublDate,
Mid([Bezeichnung],Len([Bezeichnung])-5,2) AS [Language],
xlsx_Output.[Eindeutige Downloads] AS Download_Visits,
Mid([Bezeichnung],InStr([Bezeichnung],"_")+1,6) AS PublMonth
FROM xlsx_Output
WHERE
(((Nz(Mid([Bezeichnung],InStrRev([Bezeichnung],"/")+1,InStr([Bezeichnung],"_")-
InStrRev([Bezeichnung],"/")-1),"")) Not Like "#Func!"));
#Func! indicates one of your functions is causing an error.
Your query uses multiple functions that run into trouble when your input doesn't meet that format, pre-filter instead of filtering on an error, since you can't filter on an error when appending:
INSERT INTO tbl_MatomoRaw ( DownloadDate, IntExt, Publ, PublLong,
PublDate, [Language], Download_Visits, PublMonth )
SELECT
Date() AS DownloadDate,
Left([Bezeichnung],InStr([Bezeichnung],".")-1) AS IntExt,
Nz(Mid([Bezeichnung],InStrRev([Bezeichnung],"/")+1,InStr([Bezeichnung],"_")-
InStrRev([Bezeichnung],"/")-1),"") AS Publ,
Mid([Bezeichnung],InStrRev([Bezeichnung],"/")+1,InStrRev([Bezeichnung],"_")-
InStrRev([Bezeichnung],"/")-1) AS PublLong,
Mid([Bezeichnung],InStr([Bezeichnung],"_")+1,8) AS PublDate,
Mid([Bezeichnung],Len([Bezeichnung])-5,2) AS [Language],
[Eindeutige Downloads] AS Download_Visits,
Mid([Bezeichnung],InStr([Bezeichnung],"_")+1,6) AS PublMonth
FROM (SELECT * FROM xlsx_Output WHERE Len(Bezeichnung) > 5 AND Bezeichnung LIKE "*?.?*" AND Bezeichnung LIKE "*_????????*" AND Bezeichnung LIKE "*?\?*")
WHERE
(((Nz(Mid([Bezeichnung],InStrRev([Bezeichnung],"/")+1,InStr([Bezeichnung],"_")-
InStrRev([Bezeichnung],"/")-1),"")) Not Like "#Func!"));
Since I don't know exactly where the errors occur, I can't write up a proper filter to identify them, but judging by your query they should include a slash and a symbol after that slash, an underscore and at least 8 symbols after that underscore, and a dot with at least one symbol before and after the dot.

If statement in SQL issue

I'm using Access to run queries on a database called dbo_item. It contains a column called itm_class that contains numbers 1-100. It also contains columns called itm_len, itm_wid, itm_hgt, pal_len, pal_wid, pal_hgt. Depending on the item class number, I need it to calculate the volume either by item dimensions or by pallet dimensions. I have tried a few different things, but am not really sure how to get the If statement to work. Any help would be appreciated.
I.E. If itm_class equals 1,5,22,45,67,89,97,98,99,100 then volume is (itm_len * itm_wid * itm_hgt) and for all other itm_class numbers, volume is (pal_len * pal_wid * pal_hgt). Is there any way to do this?
You can use a iif condition for this.
IIF(itm_class in (1,5,22,45,67,89,97,98,99,100), itm_len * itm_wid * itm_hgt, pal_len * pal_wid * pal_hgt)
It works like , If condition(1st argument) is True, select the first value (2nd argument) or else select the other (3rd argument)
It depends on if you are doing this statement in visual-basic or if you are doing it at a database level (function/stored procedure) or if you are doing it with the query. The simplest way would be with a case when statement in your query such as:
select case when itm_class in (1,2,3,4) then (itm_len * itm_wid * itm_hgt)
when itm_class in (5,6,7,8) then (itm_len * itm_wid)
else (itm_len * itm_wid) end volume_calculation
from some_table

"Data type mismatch in criteria expression" when doing SQL subquery in Access 2010

Here is my simplified code (little more than some basic elements to cause the SQL not to execute):
select *
from (
select replace(mytxtfield, "llama", "") as badones
from XYZ
)
where badones is not null;
The outer query runs fine when the WHERE cause is:
badones like "ZZZ-[0-9][0-9][0-9]"
but it breaks when the WHERE cause includes more than one LIKE (of any digit matching pattern) such as:
badones like "ZZZ-[0-9][0-9]" OR
badones like "ZZZ-[0-9][0-9][0-9]"
More info:
mytxtfield is of type Text
It doesn't matter if there is a WHERE cause in the inner query to check eliminate null / empty strings.
64-bit office
Since your sub-query returns the alias "badnews", you must use it in place of "badones" in your
outer query. The following produces no errors:
SELECT *
FROM (select replace(mytxtfield, "llama", "") as badnews
from XYZ
)
WHERE (((badnews) Like "ZZZ-[0-9][0-9]" Or (badnews) Like "ZZZ-[0-9][0-9][0-9]"));

Receiving the following error with my query when calculating distance

I'm receiving the following error "An invalid floating point operation occurred." when I run this query:
SELECT PolID, LocID, Address, City, StateCode, OrigGeoLat, OrigGeoLong, NewGeoLat, NewGeoLong,
acos(sin(radians(OrigGeoLat)) * sin(radians(NewGeoLat)) +
cos(radians(OrigGeoLat)) * cos(radians(NewGeoLat)) *
cos(radians(OrigGeoLong - NewGeoLong))) * 6372.8 as Distance
FROM zzGeoDataTMP
All of the *geoLat and *geoLong data is defined as numeric(18,10). When I run the query I start getting data back and then I get errors on specific rows of data. For example the following row throws the above mentioned exception only once the ACOS function is called:
OrigGeoLat|OrigGeoLong|NewGeoLat|NewGeoLong
---------------------------------------------
32.9364620|-80.0411000|32.9364620|-80.0411000
Thanks so much in advance for any insight you may be able to help with!
You're calculation is accumulating some small errors, which mean that the calculated value (passed to ACOS) is slightly higher than 1. Try wrapping it with a ROUND call:
acos(ROUND(sin(radians(OrigGeoLat)) * sin(radians(NewGeoLat)) +
cos(radians(OrigGeoLat)) * cos(radians(NewGeoLat)) *
cos(radians(OrigGeoLong - NewGeoLong)),15)
)
Where we're only keeping 15 decimal places of accuracy.

Visual Studio error on valid SQL

I'm getting an annoying error in visual studio for SQL that executes fine.
SELECT InvoiceLines.LineID,
InvoiceLines.InvoiceID,
InvoiceLines.Text,
InvoiceLines.Rate,
InvoiceLines.Count,
InvoiceLines.Rate * InvoiceLines.Count AS LineTotal,
((InvoiceLines.Rate * InvoiceLines.Count) * (1 + Invoices.VatRate / 100)) * (1 - CAST(Invoices.Discount AS money) * InvoiceLines.ApplyDiscount / 100) AS LineTotalIncVat, InvoiceLines.ApplyDiscount
FROM InvoiceLines
LEFT JOIN Invoices ON Invoices.InvoiceID = InvoiceLines.InvoiceID
What LineTotalIncVat is trying to do is compute the total for the invoice item while adding the vat and subtracting the discount, and yes, probably better to do this in code (would if I could)
The error visual studio gives is:
There was an error parsing the query [token line number =1, token line offset =14, token in error = InvoiceLines]
Even though it will validate and execute without a problem in the query builder...
Try bracketing the table and column names, it may be having trouble parsing InvoiceLines.Count because Count is a reserved word. Try [InvoiceLines].[Count].
Solved
Deleted the table from the dataset and added it again with exactly the same SQL.
How strange... although not the first time I've had to do this.