mysql / case / when / then - mysql

Can you help me please.
Where I have error in the following mysql query?
SELECT *,
CASE
WHEN vat = '1' THEN '1.10'
WHEN vat = '2' THEN '1.21'
END AS _vat,
CASE
WHEN sleva_kc != '' AND sleva_procent = '' THEN ((cena - sleva_kc) * _vat)
WHEN sleva_kc = '' AND sleva_procent != '' THEN ((cena * (1 - (sleva_procent / 100))) * _vat)
WHEN sleva_kc = '' AND sleva_procent = '' THEN (cena * _vat) END AS _cena
FROM kws_produkty WHERE _cena >= '100' AND _cena <= '500'
I use mysql connector:
#mysql_connect(HOST, USER, PASS)
explanation:
vat = VAT
cena = price
sleva_kc = Amount of discount in cash
sleva_procent = Amount of discount in percent
I need get output:
_cena = price with DISCOUNT and VAT (DISCOUNT can be a percentage or amount, VAT can be higher or smaller)
Thank you.

Your SQL is COMPLETELY mangled. You have your case statements in a place where they cannot be used and you are using elseif but do have have an if statement going.
SELECT
CASE .... END CASE,
IF ... THEN ... ELSEIF ... END IF
FROM yourtable
WHERE ....
I suggest you start reading the syntax documentation: https://dev.mysql.com/doc/refman/5.0/en/case.html

Your CASE WHEN ... END expressions should be part of the field list - before your FROM-clause.
The use of a column alias name in the WHERE-clause is illegal, see manual:
Standard SQL disallows references to column aliases in a WHERE clause. This restriction is imposed because when the WHERE clause is evaluated, the column value may not yet have been determined.

Related

mysql date range query, when the field is not sent anything for the 'between' statement

I have this query, which returns me 7 records
Now if I add this line 'AND date_format (ds.date_emission,'% Y-% m-% d ') BETWEEN' 'AND' '', nothing returns me, I want that when the date range is empty I need that I also returned the 7 records
Please refer answer below.
I have added two variable replace inStartDate variable with a range start date and inEndDate with range end date.
SELECT ds.id
FROM documentos_salida ds
WHERE ds.terceros_id = 329
AND ds.estado =0
AND ds.tipo_documento ='Factura'
AND ds.saldo_pendiente_factura >0
AND (ds.fecha_vencimiento > now() or
ds.fetcha_vencimiento = NOW())
AND ds.numero_documento LIKE '%%'
AND ( CASE WHEN (inStartDate = '' AND inEndDate = '')
THEN TRUE
ELSE (date_format(ds.fetcha_emision, '%Y-%m-%d') BETWEEN inStartDate AND inStartDate)
END)
ORDER BY ds.fecha_emision DESC;

How to assign column value to a variable within mySQL script ?

I have the following script but it returns null all the time.
SELECT
#PRICE_LARGE_PRICE = PRICE_LARGE_PRICE,
#PRICE_SMALL_PRICE = PRICE_SMALL_PRICE
FROM
prices
WHERE
PRICE_LISTING_ID = 60;
SET #ITEM_PRICE = (CASE Size WHEN GivenLargeSizeName THEN #PRICE_LARGE_PRICE
WHEN GivenSmallSizeName THEN #PRICE_SMALL_PRICE
ELSE null
END);
The issue here is
#PRICE_LARGE_PRICE = PRICE_LARGE_PRICE,
#PRICE_SMALL_PRICE = PRICE_SMALL_PRICE
table returns PRICE_LARGE_PRICE & PRICE_SMALL_PRICE correctly but the assignment does not work. Hence CASE fails.
Any help is appreciated.
You need to use SELECT ... INTO:
SELECT PRICE_LARGE_PRICE, PRICE_SMALL_PRICE
INTO #PRICE_LARGE_PRICE, #PRICE_SMALL_PRICE
FROM prices
WHERE PRICE_LISTING_ID = 60;
Note that you need to ensure that the query only returns one row of data, using LIMIT 1 if necessary.
SELECT
#PRICE_LARGE_PRICE:=PRICE_LARGE_PRICE,
#PRICE_SMALL_PRICE:=PRICE_SMALL_PRICE
FROM
prices
WHERE
PRICE_LISTING_ID = 60;
just add colon before equal sign in mysql

IF Then Else Mysql

Working on a view that pulls from two table however in one table I need to select either one field or another depending on a third..it's the if else that has me stubbed.
Create view as
select
pens.PartNo,
pens.Title,
ranges.weight
if(pens.SpeacialOffer = 1 then pens.offer as Price else pens.Price)
from
pens, ranges
where
pens.penrange = ranges.id;
If the specialoffer is falged the the view needs to pull in the offer else it needs to pull in the Price.
What you need is a CASE operator:
CASE
WHEN condition
THEN value_a
ELSE value_b
END
So in your case:
CASE
WHEN pens.SpeacialOffer = 1
THEN pens.offer
ELSE pens.price
END
This replaces the entire column definition in your SELECT statement, so the whole view becomes:
Create View as
Select
pens.PartNo,
pens.Title,
ranges.weight,
Case
When pens.SpeacialOffer = 1
Then pens.offer
Else pens.price
End as Price
From
pens, ranges
Where
pens.penrange = ranges.id;
Use CASE, also converted the query to explicit join instead of implicit join
select pens.PartNo,
pens.Title,
ranges.weight,
(Case when
pens.SpeacialOffer = 1 then
pens.offer else pens.Price
end ) as Price
FROM pens,
JOIn ranges
ON pens.penrange = ranges.id;
Here's one way:
Create view as select
pens.PartNo,
pens.Title,
ranges.weight,
(pens.SpeacialOffer * pens.offer + (1 - pens.SpeacialOffer) * pens.price) as Price
from
pens,
ranges
where
pens.penrange = ranges.id;

Conditional where in SQL based on Value?

I am doing a report whereby I can't construct the SQL programatically. I have two values that are fed into the report. The values that can be one of the following three options:
redeem
purchase
redeem AND purchase
The query needs to have a WHERE clause. If "redeem" is fed in, it must have:
... WHERE balance < 0
If "purchase" is fed in, it must have:
... WHERE balance >= 0
if both are fed in, this condition can be left out completely, or it can be said:
... WHERE balance >= 0 OR balance < 0 --> but this is redundant
Is there a way to apply this kind of logic in SQL? Is something like this possible in SQL:
SELECT * FROM account WHERE (if param1 = 'redeem' then 'balance <= 0) ... etc
?
Yep. You're almost there.
WHERE (param='redeem' and balance <=0) or (param='purchase' and balance>=0) or (param='redeem AND purchase ')
Use CASE statements in your WHERE clause. You can find examples here.
I tried the following code in MySQL and it works:
SET #param = 'purchase';
SELECT * FROM TEST.ACCOUNT
WHERE CASE
WHEN #param = 'redeem' THEN BALANCE < 0
WHEN #param = 'purchase' THEN BALANCE >= 0
ELSE TRUE
END;
Would this do the trick?
WHERE
(purchase is not null && balance >= 0)
OR
(redeem is not null && balance < 0)
CASE WHEN param1 = 'redeem' THEN balance <= 0

grouping by non-database field

How can I group a query result by a field that is not saved in the database.
For example I want to group the result by duration which is came from subtraction of start time and end time.
here is how i find out the duration
date1= $row_TicketRS['CloseDate'];
$date2 = $row_TicketRS['OpenDate'];
$diff = abs(strtotime($date2) - strtotime($date1));
$days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));
if ( $days > 0)
{
$time1 = $row_TicketRS['OpenTime'];
$time2= $row_TicketRS['CloseTime'];
$t1=($time1);
$t2=($time2);
$end=('14:30');
$start=('07:30');
$n = $end- $t1;
$n2 = $t2- $start;
$Hours2 = floor(($n+$n2)+(($days-1)*7));
echo $Hours2.' Hours';
but know i do not know how to add it to the query
here is my query
$strQuery = "SELECT count(`ticket`.TicketID) as TotOutput, department.`DeptName` FROM `ticket`, `user`, department where ticket.OwnerID = user.EmpNo and user.`DepartmentID` = department.`DepartmentID` and OpenDate between'".$DateFrom."' And '".$DateTo."'"
It'd be better to have details, but a derived table/inline view would allow you to group by a computed value:
SELECT x.duration,
COUNT(*)
FROM (SELECT t.col,
t.end_time - t.start_time AS duration
FROM YOUR_TABLE t) x
GROUP BY x.duration
How about adding that computed value to the query with an alias like this:
SELECT some_fields, end - start AS duration FROM table ORDER BY duration
dont put alias for hidden column , use directly
exmaple:
SELECT id, FLOOR(value/100)
FROM tbl_name
GROUP BY id, FLOOR(value/100);
Reference
MySQL permits expressions in GROUP BY
clauses, so the alias is unnecessary: