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;
Related
I have a column where I have 0 or 1. I like to do the following set up:
If 0 than put / use the Region_table (here I have regions like EMEA, AP,LA with finished goods only) and when it 1 then put / use the Plant_table (here I have plants with non-finished goods) data's.
I tried to write it in 2 different statements but it is not good:
,Case
when [FG_NFG_Selektion] = '0' Then 'AC_region'
End as 'AC_region'
,Case
when [FG_NFG_Selektion] = '1' Then 'AC_plant'
End as 'AC_plant'
I'm not 100% clear on what you're looking for, but if you want to get data from different tables based on the value in the [FG_NFG_Selektion] field, you can do something like this:
SELECT
CASE
WHEN [FG_NFG_Selektion] = '0' THEN r.some_col -- If 0, use value from "region" table
WHEN [FG_NFG_Selektion] = '1' THEN p.some_col -- If 1, use value from "plant" table
END AS new_field
FROM MyTable t
LEFT JOIN AC_region r ON t.pk_col = r.pk_col -- get data from "AC_region" table
LEFT JOIN AC_plant p ON t.pk_col = p.pk_col -- get data from "AC_plant" table
;
If [FG_NFG_Selektion] is a numeric field, then you should remove the single quotes: [FG_NFG_Selektion] = 0.
I would strongly recommend putting the conditions in the ON clauses:
SELECT COALESCE(r.some_col, p.some_col) as som_col
FROM t LEFT JOIN
AC_region r
ON t.pk_col = r.pk_col AND
t.FG_NFG_Selektion = '0' LEFT JOIN
AC_plant p
ON t.pk_col = p.pk_col AND
t.FG_NFG_Selektion = '1';
Why do I recommend this? First, this works correctly if there are multiple matches in either table. That is probably not an issue in this case, but it could be in others. You don't want to figure out where extra rows come from.
Second, putting the conditions in the ON clause allows the optimizer/execution engine to take advantage of them. For instance, it is more likely to use FG_NFG_Selektion in an index.
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.
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);
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
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.