Coalesce(), Nz() alternative in Access SQL 2010 - ms-access

I am trying to fetch null values but coalesce() and Nz() are not working.
select Nz(delay_head,0) from sumb where dep_reg = 'er'

Try
SELECT IIf(IsNull(delay_head),0,delay_head) AS [delay_head_result]
FROM sumb
WHERE dep_reg='er';

Related

In SQL, how can I write a query to determine the percentage of a certain value in a column after I have queried its COUNT?

-- Here's my code:
SELECT
COUNT(Action_taken)
FROM
Friend_requests
WHERE action_taken = “Accepted”
-- How can I write a second query to determine the percentage of values that equal "accepted"?
The simplest method is conditional aggregation. In MySQL, you can express this as:
select avg(action = 'accepted')
from friend_requests;
This works because MySQL treats boolean values as 1 for true and 0 for false. So the average is the ratio of true values over all the values.
Note: This will ignore NULL values for action. That can trivially be handled using the NULL-safe comparison operator:
select avg(action <=> 'accepted')
from friend_requests;
select
(select count(1) from friend_requests where action = "accepted")
/
(select count(1) from friend_requests)
As #Gordon Linoff's answer and #Strawberry's comment, The ful query for me kinda like:
select round(avg(action = 'accepted') * 100, x)
from friend_requests
where action_taken is not null
x is how many numbers after decimal

why does this if statement in mysql query always return null?

Why does the following code, always return NULL ?
Query
SELECT if(sum(amount)=NULL,0,sum(amount)) as amount
FROM item_details
WHERE order_id = 1390;
Your statement should only return NULL when the value is NULL. The condition sum(amount) = NULL will always return "unknown", which is treated as "false". Hence, only the else part is returned and the if is not really doing anything.
You probably intend sum(amount) is null.
As mentioned in another answer, coalesce() is a better solution. The correct form is:
select coalesce(sum(amount), 0)
from item_details
where order_id = 1390;
If you do this:
select sum(coalesce(amount, 0))
from item_details
where order_id = 1390;
You will still get NULL when there is no match.
Comparing with NULL in mysql gives NULL. Please use sum(amount) IS NULL or sum(amount) IS NOT NULL instead.
http://dev.mysql.com/doc/refman/5.0/en/working-with-null.html
try changing to
sum( coalesce( amount, 0 )) as amount
Try something like this:
SELECT if((sum(amount))=NULL,0,sum(amount)) as amount
FROM item_details
WHERE order_id = 1390;
SELECT if(sum(amount)=NULL,0,sum( coalesce( amount, 0 ))
FROM item_details
WHERE order_id = 1390;
Is that the query you're looking for?
SELECT IFNULL(SUM(I.amount),0) as amount
FROM item_details I
WHERE I.order_id = 1390
GROUP BY I.order_id
I use IFNULL and a GROUP BY clause in order to get the expected result.
Hope this will help you
NULL is not equals to any value and not equals to NULL.
NULL means: the value is not set -> unknown value. You can not compare NULL to anything with normal comparsion operators (=, <, >, etc), it has no value.
SUM(Amount) = NULL is always UNKNOWN. UNKONWN is stated as FALSE in comparsions.
You can handle NULL values via its related functions and comparsion operators such as COALESCE(expr1, expr2, ..., exprN), IS NULL, etc.
See MySQL documentation - Working with NULL Values for more info.
You should change the if(sum(amount)=NULL,0,sum(amount)) expression to COALESCE(SUM(amount), 0) to achive your goal.
COALESCE() returns the first not-null expression.

NULL to zero with ISNULL

I have the following SQL statement:
SELECT name, SUM(growth) AS sum_buy_price, SUM(recovery) AS sum_msrp, SUM(growth)+SUM(recovery) AS total
FROM orders
WHERE id = ?
GROUP BY name
My data is coming from a CSV file that I have no control over and either 'growth' or 'recovery' can be NULL in the data, but not at the same time. I need to use ISNULL to convert the possible NULL values to zero in order for the SUM to work correctly, but I'm unsure of how/where to add the ISNULL since the SELECT is indexing another record (name).
ISNULL returns whether the argument passed is null (i.e., it is analogous to true or false). I suppose, what you need is IFNULL:
SELECT
name,
SUM(IFNULL(growth, 0)) AS sum_buy_price,
SUM(IFNULL(recovery, 0)) AS sum_msrp,
SUM(IFNULL(growth, 0))+SUM(IFNULL(recovery,0)) AS total
FROM
orders
WHERE
id = ?
GROUP BY
name
The SUM() function ignores NULL values, so you don't need to change a NULL to a 0 in order for it to work properly.
If however, all values that you're aggregating are NULL and you want to return a 0 instead of NULL you can use IFNULL() or the more common COALESCE() to show 0 as the sum instead of NULL:
SELECT COALESCE(SUM(growth),0)
ISNULL() is a valid SQL Server function, IFNULL() is the equivalent in MySQL, but all major databases make use of COALESCE() which returns the first non-NULL value in a set, ie: COALESCE(NULL,NULL,5) would return 5.
This should work for you:
SELECT name, SUM(ISNULL(growth, 0)) AS sum_buy_price,
SUM(ISNULL(recovery, 0)) AS sum_msrp,
SUM(ISNULL(growth, 0))+SUM(ISNULL(recovery,0)) AS total
FROM orders
WHERE id = ?
GROUP BY name

how to select and set value as 0 even if there is no value or data stored in database

Is there any select query that set value as 0 even if it is empty or no record stored in database?
Because I am trying to subtract values from two different tables. But the problem is that I cant subtract the the tables if one of them is no data stored.
Here is my code. This code can subtract if both tables have value.
SELECT category,(SELECT SUM(s.total)-SUM(r.total)
FROM rsales AS s WHERE r.pcode=s.pcode
) as total,
r.pcode
FROM rreturn AS r
GROUP BY r.pcode;
Use IFNULL or COALESCE:
SELECT IFNULL(SUM(s.total), 0)
SELECT COALESCE(SUM(s.total), 0)
If expr1 is not NULL, IFNULL() returns expr1; otherwise it returns expr2.
IFNULL() returns a numeric or string value, depending on the context in which it is used.
something like this should work
SUM(IF(s.total, s.total, 0))
OR
SUM(IFNULL(s.total), 0))
The syntax you're using is both confusing and error prone. I would go for something simpler like this:
SELECT category, SUM(s.total) - SUM(r.total) total
FROM rsales s
LEFT JOIN rreturn r USING (pcode)
GROUP BY s.pcode;
This assumes rreturn may not have records for each pcode.

Set default value in select statement(not use UNION statement)

I would like to have a SELECT statement that will return specified default values if no rows are returned from the database.
We can use UNION to get the desired result like this question: "How to set a default row for a query that returns no rows?", but this gives an extra result row.
example:
SELECT a
from TBL_TEST
UNION
SELECT 0
FROM DUAL
Is there a better way, or a standard SQL way to do this that will be portable across multiple database engines?
SELECT ifnull(a,20) FROM TBL_TEST
Selects 20 if a is null otherwise selects a (in mysql, not sure about others)
For a portable solution, how about:
select coalesce(a, 0)
from TBL_TEST
right outer join DUAL on null is null
The COALESCE function is used here because it is more portable than NVL() or IFNULL().
You would have a DUAL table created in database systems that use a different name, such as SQL Server or DB2.
MySQL has the DEFAULT function, but I'm not sure how standard or widely supported it is.
MySQL IFNULL is like oracle's NVL function
MySQL IFNULL() takes two expressions and if the first expression is not NULL, it returns the first expression. Otherwise it returns the second expression.
Syntax
IFNULL(expression1, expression2);
SELECT IFNULL(a,<default value>) from TBL_TEST
In Oracle:
select nvl(a, 0)
from DUAL left join TBL_TEST on null is null
use the COALESCE() to convert the null value column with its default value such as
select coalesce(a,0) from TBL_TEST
In, SQL SERVER 2008 R2 : When Value IS NULL
SELECT ISNULL(a,<Default Value>) from TBL_TEST
e.g. SELECT ISNULL(a,0) from TBL_TEST
In, SQL SERVER 2008 R2 : When Empty String
SELECT ISNULL(NULLIF(a,<Empty String>)<Default Value>) from TBL_TEST
e.g. SELECT ISNULL(NULLIF(a,'')0) from TBL_TEST
This is working fine...