I've the following code
select * from weeks
where case
when wet>1000 then wenumber=1
when wet>500 then wenumber=2
when wet>100 then wenumber=3
else wenumber= 22
end
it gives me the result of both ( when > 1000 and else )
through my search I understood that it is search case, but
I need to make it a simple case
simply if the first condition were true stop evaluating "else" statement
any help
Better way of doing it
select *
from weeks
where ( wet>1000 and wenumber = 1 ) or wenumber = 22
Try this:
SELECT *
FROM weeks
WHERE wenumber = CASE WHEN wet > 1000 THEN 1
WHEN wet BETWEEN 500 AND 1000 THEN 2
WHEN wet BETWEEN 100 AND 500 THEN 3
ELSE 22
END;
You need to specify which column should be compared with result of CASE like:
SELECT *
FROM weeks
WHERE wenumber = (CASE
WHEN wet > 1000 THEN 1
ELSE 22
END)
Related
I've got a case function within my select query. Within this case I want to convert "SongLength" which is in seconds into and mm:ss format with the SEC_TO TIME function. However I get a syntax error upon putting this function in there.
select SongTitle,Artist,SongLength
case
when SongLength < 600 then
sec_to_time(SongLength)
else
sec_to_time(SongLength)
end
from Songs, Artists where Songs.ArtistId = Artists.Id
order by SongTitle;
Try SongLength to make an alias as :
select SongTitle,Artist,
(case
when SongLength < 600 then
sec_to_time(SongLength)
else
sec_to_time(SongLength)
end) as SongLength
from Songs join Artists
on ( Songs.ArtistId = Artists.Id )
order by SongTitle;
see below example works fine
select case
when 400 < 600 then
sec_to_time(400)
else
sec_to_time(300)
end as col
it returns below output
col
00:06:40
So if your datatype of column SongLength is numeric then it should work
http://www.sqlfiddle.com/#!9/835f61/150
I need to make a select like,
Select * from table WHERE column = x if column != -1
but i have no idea for now.
Anyone know or made in past something like that?
Thanks.
You should also write like this,
Select * from table
WHERE
1 = case when column != -1 then
case when column = x then 1 else 0 end
else 1 end
You can utilize case when in where clause.
Similarly you can add more conditional criteria like,
Select * from table
WHERE
1 = case when column != -1 then
case when column = x then 1 else 0 end
else 1 end
AND
1 = case when column1 [conditional operator] value then
case when column1 = xx then 1 else 0 end
else 1 end
This is just an example how you can integrate more conditional criteria together, even though you can add more case when in else part even.
I've got a rather large query that is trying to get a list of carriers and compare the amount of insurance they have on record to identify carriers that do not meet a minimum threshold. If I run the select query it works just fine with no errors. But when I try to use it for an insert into a table it returns this error message
[Err] 1366 - Incorrect decimal value: '' for column '' at row -1
I have to use the cast as decimal at the bottom of this query because the value that is being stored in the database is a varchar and I cannot change that.
Anyone have any ideas?
set #cw_days = 15;
INSERT INTO carrier_dnl (carrier_id, dnl_reason_id, status_id)
SELECT work_cw_carrier_status_update.carrier_id, company_dnl_schema.dnl_reason_id,
CASE
WHEN work_cw_carrier_status_update.comparison_date > #cw_days THEN 1
ELSE 4
END as status
FROM work_cw_carrier_status_update
JOIN company_dnl_schema
ON company_dnl_schema.dnl_reason_id = 51
LEFT OUTER JOIN carrier_insurance
ON carrier_insurance.carrier_id = work_cw_carrier_status_update.carrier_id
WHERE ifnull(carrier_insurance.insurance_type_id,4) = 4
AND date(now()) BETWEEN IFNULL(carrier_insurance.insurance_effective_date,DATE_SUB(now(),INTERVAL 1 day)) AND IFNULL(carrier_insurance.insurance_expiration_date,DATE_ADD(now(),INTERVAL 1 day))
AND CASE WHEN NULLIF(carrier_insurance.insurance_bipdto_amount,'') is null THEN 0 < company_dnl_schema.value
ELSE
ifnull(cast(replace(carrier_insurance.insurance_bipdto_amount, '*','') as decimal),0) < company_dnl_schema.value
END
AND ( work_cw_carrier_status_update.b_bulk = 0 OR work_cw_carrier_status_update.b_bulk = 1 )
AND ( work_cw_carrier_status_update.b_otr = 1 OR work_cw_carrier_status_update.b_ltl = 1
OR work_cw_carrier_status_update.b_dray = 1 OR work_cw_carrier_status_update.b_rail = 1
OR work_cw_carrier_status_update.b_intermodal = 1 OR work_cw_carrier_status_update.b_forwarder = 1
OR work_cw_carrier_status_update.b_broker = 1 )
group by work_cw_carrier_status_update.carrier_id;`
If the select seems to work, then there are two possible problems. The first is that the select doesn't really work and the problem appears further down in the data. Returning one or a handful of rows is not always the same as "working".
The second is an incompatibility with the types for the insert. You can try to use silent conversion to convert the values in the select to numbers:
SELECT work_cw_carrier_status_update.carrier_id + 0, company_dnl_schema.dnl_reason_id + 0,
(CASE WHEN work_cw_carrier_status_update.comparison_date > #cw_days THEN 1
ELSE 4
END) as status
This may look ugly, but it is not nearly as ugly as storing ids as strings in one table and as numbers in another.
I'm about to build some sort of function or query where I can check if a certain record already exists in the database. The following rules apply:
The table has 6 columns
My yet-to-build-query has access to a complete row-object (all 6 values)
This query should find each row with at least 4 out of 6 corresponding values from the object I passed
Using MySQL
Is it even possible to build a query like this? My goal is to have a function which can return true if it's likely that a row like the passed object is already existing in the database.
Is my only option to make a query with multiple where-statements (where I try for each combination 4 different values)?
pseudo:
function getSimilarRow(Row_Object $row)
{
//select *
//from table_x
//where 4 out of 6 properties from object $row apply
}
You could use a case statement in the where clause for each property you are trying to match. If it meets the criteria then give the case statement a value of 1; if it doesn't then give it 0. The sum of the cases should then be >= 4.
I'm not that familiar with MySQL but the following will work (I knocked up a quick SQL Fiddle to show it working):
select * from SomeTable where
(case when propertyOne = 'value1' then 1 else 0 end) +
(case when propertyTwo = 'value2' then 1 else 0 end) +
(case when propertyThree = 'value3' then 1 else 0 end) +
(case when propertyFour = 'value4' then 1 else 0 end) +
(case when propertyFive = 'value5' then 1 else 0 end) +
(case when propertySix = 'value6' then 1 else 0 end) >= 4
Obviously you could change your logic in each clause if you'd prefer them to be likes or anything. You could even apply a weighting to each column by using something other than just 1 if you needed to get really creative.
I'm looking for a string function that will display data from my table where 2 values end in 1.
For example
int_id int2_id
101 302
201 301
Here the data I would want to show is the 201/301 due to the fact that they both end in 1.
Any help would be greatly appreciated.
SELECT * FROM `myTable` WHERE `int_id` LIKE '%1' and `int2_id` LIKE '%1';
The % character is a wildcard character that matches anything.
SELECT * FROM myTable WHERE RIGHT(int_id, 1) = 1 AND RIGHT(int2_id, 1) = 1
Use modular arithmetic:
SELECT * FROM `table` WHERE int_id % 10 = 1 AND int2_id % 10 = 1