Why am I geting null when i use max instead of count - sql

I answered the following question question link. But i fount stringe Behaviour.
when i write this
Update product Set [order]= Case when Not Exists (Select * from
product a where a.ProductTypeID =product.ProductTypeID and a.id
<product.ID )
tHEN 1
eLSE
((Select cOUNT([ORDER])+1 from product b where
b.ProductTypeID =product.ProductTypeID and product.ID <product.id)+1)
eND
It works well but when i write ...'
Update product Set [order]= Case when Not Exists (Select * from
product a where a.ProductTypeID =product.ProductTypeID and a.id
<product.ID )
tHEN 1
eLSE
((Select Max([ORDER])+1 from product b where
b.ProductTypeID =product.ProductTypeID and product.ID <product.id)+1)
eND
It's gives null in else situation i dont understand why?Can Anyone Explain this when i missing why its getting null when i use Max.Here is sql fiddle http://sqlfiddle.com/#!3/1e15d/1 where i use count when i use Max it gives null why?

The difference is that count returns zero for an empty result, but max returns null for an empty result.
You have product.ID <product.id in your condition in the subquery, which will always be false as you are comparing a field to itself. That will make the result from the subquery empty.
It should be b.ID <product.id to compare the value in the table in the subquery to a value in the table in the outer query.
So neither query works as intended, but when you use count you don't get a null value from the empty result.

you can try this(for mysql):
select ifnull(max(column), 0) when max() return null, it give you 0.

Related

Return 1 instead of 0 when Count(*) result is Null

My code from SQL Server:
SELECT ESTAGIO.SK_ESTAGIO, ISNULL(count(ESTAGIO.SK_ESTAGIO), 0) as how_many
from ESTAGIO
left join ESTAGIARIO
on ESTAGIARIO.SK_ESTAGIO = ESTAGIO.SK_ESTAGIO
group by
ESTAGIO.SK_ESTAGIO
When "ESTAGIO.SK_ESTAGIO" doesn't exist in the table "ESTAGIARIO" it returns 1 instead of 0, I already tried to use ISNULL(), NULLIF() and COALESCE() and still couldn't find the problem that is making the query above returning 1 when it should be 0.
You are counting the wrong field. Do it like this, taking the field from the outer joined table ESTAGIARIO (not from ESTAGIO):
SELECT ESTAGIO.SK_ESTAGIO, Count(ESTAGIARIO.SK_ESTAGIO) as how_many
from ESTAGIO
left join ESTAGIARIO
on ESTAGIARIO.SK_ESTAGIO = ESTAGIO.SK_ESTAGIO
group by
ESTAGIO.SK_ESTAGIO
BTW, count can never return null.

How to get the result based on the condition in SQL?

Consider Issue Details table has Overall_Issues_ID and Fixed_Issues_ID Column. I need to get the result based on certain condition as below.
Condition: if the Overall_Issues_ID Value exists in any one of the Fixed_Issues_ID Column, then those ID should be consider as Fixed or else it is considered as Not Fixed.
I am using Oracle Version 10 G.
Join the same table twice with different alias names
select i1.overall_issues_id,
case when i2.overall_issues_id is not null
then 'fixed'
else 'not fixed'
end as is_fixed
from issues_details i1
left join issues_details i2 on i2.fixed_issues_id = i1.overall_issues_id
SQLFiddle demo
You can look up the ID in a subquery:
select
overall_issues_id as id,
case when overall_issues_id in (select fixed_issues_id from issue_details fixed)
then 'Fixed' else 'Not Fixed' end as fixed
from issue_details;

Update column within CASE statement with results of a subquery postgres

I need to update a column based on the results of a subquery. If the subquery returns results for that column then the columns must be updated, is the query returns no results for that column then I need to update with 0.
I do not know where to place the subquery and how to combine it with the CASE statement. This is what I thought but the syntax is not correct. Can anybody help please?
(SELECT datazones.ogc_fid, count(*) as total
FROM suppliersnew suppliers, datazone_report_resupply datazones
WHERE St_contains(datazones.geom, suppliers.geometry) AND (suppliers.status = 'Under construction' OR
suppliers.status = 'Unknown' OR suppliers.status = 'Operational') GROUP by datazones.ogc_fid ORDER BY total ASC) sources
UPDATE datazone_report_resupply
SET es_actual =
CASE
WHEN datazone_report_resupply.ogc_fid = sources.ogc_fid THEN sources.total
ELSE 0
END
The query is a little hard to follow, because the aggregation is on the outer column (this is unusual). However, you don't need aggregation or order by. You only seem to care whether a row exists.
I think the logic is:
UPDATE datazone_report_resupply r
SET es_actual =
(CASE WHEN EXISTS (SELECT 1
FROM suppliersnew s
WHERE St_contains(r.geom, s.geometry) AND
s.status IN ('Under construction', 'Unknown', 'Operational')
)
THEN 1 ELSE 0
END);

Using SELECT with a display condition

SELECT DISTINCT Invoice.InvNo, Invoice.OrderNo, Part.PartNo,
orders.orddate AS Order_Date, Invoice.InvDate AS Bill_Date,
MiscChg.Descr, MiscChg.RegFee, Invoice.InvAmt,
Orders.ClaimNo, Firm.FirmName AS Ordering_Firm,
**oppatty.attyid(WHERE oppatty.attyfor = 13)**, Location.Name1 AS Location
The bolded section is the part I'm having trouble with. I know what I have isn't right, but it demonstrates what I would like to accomplish. In the oppatty table, there could be several items listed. I want it to only display "AttyID for the entry that has an ATTYFOR = 13".
Hope this make sense, thanks
Jack
You need to add a CASE WHEN to the select statement.
SELECT DISTINCT
Invoice.InvNo,
Invoice.OrderNo,
Part.PartNo,
orders.orddate AS Order_Date,
Invoice.InvDate AS Bill_Date,
MiscChg.Descr,
MiscChg.RegFee,
Invoice.InvAmt,
Orders.ClaimNo,
Firm.FirmName AS Ordering_Firm,
CASE WHEN oppatty.AttyFor = 13
THEN oppatty.AttyId
ELSE '' END AS attyfor,
Location.Name1 AS Location
FROM
.........
This will display the AttyId field when the row's AttyFor field is equal to 13 and show an empty string when it's not.
Your query has no from or where clause and your question is a bit jumbled, but even so, I think I understand what you want to do. Assuming it's acceptable to fill the "AttyID" values with null where "AttyFor" isn't equal to 13, then you could just use a case statement. Try something like this
select
stuff.things,
case
where oppatty.attyfor <> 13 then null
else oppatty.attyid
end as attyid,
stuff.others
from
oppatty
join stuff on oppatty.ID = stuff.ID
If that's not your desired result, and you'd rather entirely exclude rows where "AttyFor" isnt equal to 13, then just use a where clause.
select
stuff.things,
oppatty.attyid,
stuff.others
from
oppatty
join stuff on oppatty.ID = stuff.ID
where
oppatty.attyfor = 13

PostgreSQL function multiple rows returned from subquery in case statement

So I have written a postgreSQL function that is supposed to do a search on a table based on a huge amount of optional input parameters which i group with lots of AND statements. This one however:
AND
(
(newcheck IS NULL)
OR
(
newcheck IS NOT NULL AND product.id IN(
CASE WHEN newcheck='New'
THEN
(SELECT product.id FROM product WHERE product.anew IS true)
ELSE
(SELECT product.id from product WHERE product.anew IS false)
END)
)
)
gives me a
ERROR: more than one row returned by a subquery used as an expression
This isnt helping much since I do want it to return a lot more than one row.
The values of the newcheck variable will be sent from a dropdown menu in a web form so it can only be 'New' or 'Old'.
Any ideas on what might be causing this problem?
Try something like:
AND ((newcheck IS NULL)
OR (newcheck IS NOT NULL
AND product.id IN (SELECT product.id
FROM product
WHERE product.anew = CASE WHEN newcheck='New'
THEN true
ELSE false
END))