I want to use CASE with three conditions. It is showing error.
My query :
select distinct a.phone, other,starttime,duration,imeinumber,
imsinumber,call_type,a.provider_key, a.celltowerid, siteaddress,
lat, long, azimuth from cdat a
left join cdatdupl.dbo.cdatcelltowerareanew b on
case
when a.provider_key!='9' then a.tower_key=b.tower_key
else a.celltowerid = b.celltowerid
and a.provider_key=b.provider_key
and a.state_key end=b.state_key
You have to close the CASE expression with end keyword
syntax
CASE { simple_case_expression
| searched_case_expression
}
[ else_clause ]
END
Updated Query
select distinct a.phone, other,starttime,duration,imeinumber,
imsinumber,call_type,a.provider_key, a.celltowerid, siteaddress,
lat, long, azimuth from cdat a
left join cdatdupl.dbo.cdatcelltowerareanew b on
case
when a.provider_key!='9' then a.tower_key=b.tower_key
else (a.celltowerid = b.celltowerid
and a.provider_key=b.provider_key
and a.state_key end=b.state_key)
end
Related
SQL Query
sessionFactory.getCurrentSession().createSQLQuery("select claim.encounterId, claim.claimUniqID, patientmaster.FirstName, tbl_insurance.insurance_name, claim.status from rcmdb.claim join rcmdb.encounter on claim.encounterID=encounter.encounterID join rcmdb.insurance_details on encounter.insuranceDetailsID=insurance_details.insuranceDetailsID
join rcmdb.tbl_insurance on insurance_details.insurance=tbl_insurance.insurance_id
join rcmdb.patientmaster onpatientmaster.patientMasterID=encounter.patientMasterID
where createdByDate between'"+from+"' and '"+to+"'").list();
i want to return string values based on claim.status values like if the status is 1 accepted, in output I want the string values how can I write the query?
You can use CASE statement. https://www.w3schools.com/sql/func_mysql_case.asp
SELECT CASE
WHEN status =1 THEN STRING
ELSE NULL
END
Put a table in the database that maps the int to the string and join it:
ClaimStatus
--------------
ID, StatusDescription
1, Accepted
2, Rejected
SELECT c.PolicyNumber, c.ClaimValue, cs.StatusDescription
FROM
claims c
INNER JOIN claimstatus cs ON c.ClaimStatusId = cs.ID
Is it possible to compare the result of a query to a string in MySQL? Something like :
select case when 'Captain' = (select role from roleassociation ra
inner join users urs
on ra.userentityid = urs.invuserid
where invuserid = 007)
then 'true' else 'false' end as result;
The above query is incorrect, but is it possible to implement? Thanks in advance.
I would probably just put the CASE expression in the subquery:
SELECT
CASE WHEN role = 'Captain' THEN 'true' ELSE 'result' END AS result
FROM roleassociation ra
INNER JOIN users urs
ON ra.userentityid = urs.invuserid
WHERE
invuserid = 007;
Assuming you expect the result to be only a single record, you will still have just a single result with this approach.
I would like to use if statement in sql query :
what I want :
if(tractions_delivery.send_date_id !=0 ){
date_send_commodities.id = tractions_delivery.send_date_id
}
my query :
from
tractions_delivery,user_address,province,city,date_send_commodities,users
WHERE
tractions_delivery.tr_id = $tr_id
AND
tractions_delivery.address_id = user_address.id
AND
user_address.province_id = province.id
AND
user_address.city_id = city.id
AND
//not work
(tractions_delivery.send_date_id IS NOT 0 date_send_commodities.id = tractions_delivery.send_date_id)
AND
users.id = user_address.user_id
You could use the CASE-statement
SELECT
*
FROM
tractions_delivery,
user_address,
province,
city,
date_send_commodities,users
WHERE
tractions_delivery.tr_id = $tr_id AND
tractions_delivery.address_id = user_address.id AND
user_address.province_id = province.id AND
user_address.city_id = city.id AND
CASE WHEN tractions_delivery.send_date_id != 0 THEN date_send_commodities.id = tractions_delivery.send_date_id ELSE 1=1 END AND
users.id = user_address.user_id
You can only use if statements in stored procedures or functions. If you just write a sql statement unfortunately you cannot use if statements around the query. But you can use logic in the query itself, e.g.:
SELECT CASE WHEN col1 = col2 THEN'col1 equals col2' else 'col1 doesnt equal col2' ELSE
FROM table1
So around doesnt work, but in the field list you can create CASE WHEN ELSE END logic.
CASE or IF() operators can be of help.
Examples,
SELECT (CASE 1 WHEN 1 THEN 'One' WHEN 2 THEN 'Two' ELSE 'More' END) 'Result';
OR
SELECT IF(1=1, 'One', 'Two') 'Result';
These CASE and IF() operators can be used in the SELECT clause to conditionally interpret column values and return in the resultset.
Note: Do not confuse CASE operator here with 'CASE conditional syntax block' that ends with END CASE.
So here is the issue. I'm trying to write a new fillrate report because the one built in is not good enough... I'm trying to run a single select statement to return both, a count of how many times an item was ordered for a specific month, and then also a count of how many times it was invoiced/shipped in full.
This code is obviously wrong, I also currently have it restricted to only look at AUG of 2015, but that is just to simplify results during testing.
I can't figure out how to do the 2nd count... This is what I was trying (brain stuck on old for each loop logic):
select inv_mast.item_id,
inv_mast.item_desc,
"YEAR" = year(oe_line.required_date),
"MONTH" = month(oe_line.required_date),
"ORDERS" = count(1),
"HITS" = (
select count(1)
from invoice_line
where invoice_line.order_no = oe_line.order_no
and invoice_line.oe_line_number = oe_line.line_no
and invoice_line.qty_shipped = oe_line.qty_ordered
)
from oe_line,
inv_mast,
inv_loc
where inv_mast.inv_mast_uid = oe_line.inv_mast_uid
and inv_mast.delete_flag = 'N'
and inv_mast.inv_mast_uid = inv_loc.inv_mast_uid
and inv_loc.location_id = '101'
and year(oe_line.required_date) = '2015'
and month(oe_line.required_date) = '8'
group by inv_mast.item_id,
inv_mast.item_desc,
year(oe_line.required_date),
month(oe_line.required_date)
order by inv_mast.item_id
To me it would seem like you could rewrite the query to use a left join on the invoice_line table instead. Without any proper test data I can't guarantee it is correct, but I think it should be.
Besides the left join I also changed to explicit joins and moved the aliases as I don't think MySQL supports the alias = column syntax.
select inv_mast.item_id,
inv_mast.item_desc,
year(o.required_date) as "YEAR",
month(o.required_date) as "MONTH",
count(1) as "ORDERS",
count(invoice_line.order_no) as "HITS"
from oe_line o
join inv_mast on inv_mast.inv_mast_uid = o.inv_mast_uid
join inv_loc on inv_mast.inv_mast_uid = inv_loc.inv_mast_uid
left join invoice_line on invoice_line.order_no = o.order_no
and invoice_line.oe_line_number = o.line_no
and invoice_line.qty_shipped = o.qty_ordered
where inv_mast.delete_flag = 'N'
and inv_loc.location_id = '101'
and year(o.required_date) = '2015'
and month(o.required_date) = '8'
group by inv_mast.item_id,
inv_mast.item_desc,
year(o.required_date),
month(o.required_date)
order by inv_mast.item_id;
I am trying to use the following query in SQL Server
SELECT [AL].[Subscriptions].Id,
[AL].[Subscriptions].name,
[AL].[Subscriptions].description,
[AL].[Subscriptions].price,
[AL].[Subscriptions].iconFileName,
IIf(a.expiryDate > Now(), 'TRUE', 'FALSE') AS isSubsByUser
FROM [AL].[Subscriptions]
LEFT JOIN (SELECT *
FROM [AL].[UserSubscriptions]
WHERE userId = 13259) AS a
ON Subscriptions.Id = a.itemid;
but always get the error
Error in list of function arguments: '>' not recognized.
Unable to parse query text.
How do I resolve it?
Like Martin Smith said you need to use a case statement. Also it looks like you are only using a couple of fields in the derived table therefor I would suggest not using *. I put a example below.
SELECT [AL].[Subscriptions].Id,
[AL].[Subscriptions].name,
[AL].[Subscriptions].description,
[AL].[Subscriptions].price,
[AL].[Subscriptions].iconFileName,
case when a.expiryDate > GetDate() then 'TRUE' else 'FALSE' end AS isSubsByUser
FROM [AL].[Subscriptions]
LEFT JOIN (SELECT expiryDate, itemid
FROM [AL].[UserSubscriptions]
WHERE userId = 13259) AS a
ON Subscriptions.Id = a.itemid;