This is my code, i have 2 columns - Modem = String, Total = Integer
SELECT `Modem`, `Total`,
CASE `Modem` WHEN 'Yes' THEN
(`Total`+ 50)
ELSE
(`Total`+ 0)
END AS ModemAndTotal
FROM invoices
I need when Modem = "Yes" add 50 to total (Total + 50)
Try something like:
SELECT `Modem`, `Total`, CASE WHEN `Modem` = 'Yes' THEN (`Total`+ 50)
ELSE
`Total`
END AS ModemAndTotal
FROM invoices
Related
I'm doing a select on two tables with this:
SELECT m.torneio, m.deck, m.top, m.lugar, sum( m.quantidade ) AS quantidade, m.formato AS formato, q.quantidade AS qtorneio, t.season AS season, sum( m.top ) AS totaltops, count( m.lugar = '1' ) AS venceu
FROM `metagame` AS m, quantidade AS q, torneios AS t
WHERE m.torneio = t.nome
AND m.torneio = q.nome
GROUP BY m.deck
My problem is that venceu is counting all instances instead of only the ones when lugar = 1. Why is that?
tried with sum() too with no good results too. How can i fix this?
I am surprised that count( m.lugar = '1' ) syntaxs but it does and returns the sames as count(*). You should probably change it to sum(case when lugar = 1 else 0 end) as venceu. You should also look closely at the group by to be sure it works as you expect (i suspect not).
count(x) does not accept an expression.
It's only counting how many times x is returned.
What you should do is check if m.lugar is 1 and yes add one to the counter else do nothing.
Inline checks can be done like so:
case when m.lugar = '1' then 1 else 0 end
Then add all the one you gets :
sum(case when m.lugar = '1' then 1 else 0 end)
Your final query should look like this:
SELECT
m.torneio,
m.deck,
m.top,
m.lugar,
sum( m.quantidade ) AS quantidade,
m.formato AS formato,
q.quantidade AS qtorneio,
t.season AS season,
sum( m.top ) AS totaltops,
sum(case when m.lugar = '1' then 1 else 0 end) AS venceu
FROM
`metagame` AS m,
quantidade AS q,
torneios AS t
WHERE
m.torneio = t.nome
AND m.torneio = q.nome
GROUP BY
m.deck
If I understand your question you can use this:
sum(case when m.lugar = '1' then 1 else 0 end)
or you can try having clause
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
HAVING condition
ORDER BY column_name(s);
I'm creating a rating system. I have two tables hinne (rating) and hinnang (rating multiplier). I need to multiply the rating and then average the rating to know what rating I got out of all ratings by aine(subject).
Example:
All points need to be calculated in 0-100 point system.
So if my first rate is 25 and the rating multiplier is 4 then first rate (25/25)
4*25=100
If the second rate is 30 and multiplier 2 then second rate (30/50)
2*30=60
Now I need to average them like 100+60/2=80.
That should work in my SQL statement, but I got in trouble.
CASE
WHEN aine.nimetus = 'Füüsika I'
THEN hinne.tulemus * hindamine.kaal
ELSE 0
END
,0)
))
So, this is my pivot case statement. hindamine.kaal should be different value for each hinne.tulemus 25*4,50*2 BUT it doesn't work. It just uses multiplier value 4. How can I make this work?
The result of SQL: 150
The expected result: 100
Therefore here is my full SQL:
SELECT
tudeng.m_number,hindamine.kaal, ROUND(AVG(NULLIF(
CASE
WHEN aine.nimetus = 'Füüsika I'
THEN hinne.tulemus * hindamine.kaal
ELSE 0
END
,0)
))
AS FüüsikaI ,ROUND(AVG(NULLIF(
CASE
WHEN aine.nimetus = 'Kõrgem matemaatika I'
THEN hinne.tulemus * hindamine.kaal
ELSE 0
END
,0)
))
AS KõrgemmatemaatikaI ,ROUND(AVG(NULLIF(
CASE
WHEN aine.nimetus = 'Raalprojekteerimine'
THEN hinne.tulemus * hindamine.kaal
ELSE 0
END
,0)
))
AS Raalprojekteerimine ,ROUND(AVG(NULLIF(
CASE
WHEN aine.nimetus = 'Tehniline graafika'
THEN hinne.tulemus * hindamine.kaal
ELSE 0
END
,0)
))
AS Tehnilinegraafika , ROUND(AVG(NULLIF(
CASE
WHEN aine.nimetus = 'Ettevõteluse alused'
THEN hinne.tulemus * hindamine.kaal
ELSE 0
END
,0)
))
AS Ettevõtelusealused
FROM
tudeng
INNER JOIN
aine_tudeng
ON
tudeng.tudeng_id = aine_tudeng.tudeng_id
INNER JOIN
aine
ON
aine.aine_id = aine_tudeng.aine_id
INNER JOIN
hinne
ON
hinne.aine_tudeng_id=aine_tudeng.aine_tudeng_id
INNER JOIN
hindamine
ON
hindamine.hindamine_id=aine_tudeng.aine_id
GROUP BY
tudeng.m_number
I suppose your error is here:
ON hindamine.hindamine_id = aine_tudeng.aine_id
A hindamine (assessment/rating?) is something different from an aine (subject?), so you are mistaken in joining on these IDs.
(I have used Google translator to help me with the meanings.)
When I try to use this:
SUM(CASE WHEN IF(SUM(CASE WHEN "b.count_students_status" = 1 THEN 1 ELSE 0 END) >= 1, 1, 0) = 1 THEN 1 ELSE 0 END)
It says Query Error: Improper usage of Group Function
Below is the complete code:
SELECT
"a.batch" AS Batch,
SUM(
CASE
WHEN IF(
SUM(
CASE
WHEN "b.count_students_status" = 1
THEN 1
ELSE 0
END
) >= 1,
1,
0
) = 1
THEN 1
ELSE 0
END
) AS Payments_Not_Received
FROM
"DBU - Complete"
WHERE "a.suspended" = 'no'
GROUP BY "a.batch"
I wanted to convert the status to 1, if there are multiple occurrences and then sum the total occurrences.
Any help please - I am using ZOHO to build the query?
You're trying to reference the result of the GROUP BY before is has run. Try something like this:
select t.BATCH,
sum(case when t.pnr >=1 then 1 else 0 end) as Payments_Not_Received
from
(
select
a.batch as BATCH,
SUM(CASE WHEN "b.count_students_status" = 1 THEN 1
ELSE 0
END) as pnr
from yourTable a
WHERE a.suspended = 'no'
group by a.batch
) t
group by t.BATCH;
I have a Query which is separated in different parts. (distance, score and rank)
SELECT Entry.*, Address.*,
(6367.41 * SQRT(2 * (1-cos(RADIANS(Entry.latitude)) * cos(0.92640848333131) * (sin(RADIANS(Entry.longitude)) * sin(0.15361853481704) + cos(RADIANS(Entry.longitude)) * cos(0.15361853481704)) - sin(RADIANS(Entry.latitude)) * sin(0.92640848333131))))
AS distance,
(CASE WHEN `Entry`.`title` LIKE '%%' THEN 50 ELSE 0 END +
CASE WHEN `Entry`.`description` LIKE '%%' THEN 30 ELSE 0 END +
CASE WHEN `Entry`.`description_long` LIKE '%%' THEN 10 ELSE 0 END +
CASE WHEN `Entry`.`product_type` = 1 THEN 0 ELSE 0 END +
CASE WHEN `Entry`.`product_type` = 2 THEN 40 ELSE 0 END +
CASE WHEN `Entry`.`product_type` = 3 THEN 50 ELSE 0 END )
AS score,
(CASE WHEN (score > 100 AND distance <= 10) THEN 1 ELSE 0 END)
rank
FROM `usr_web12_1`.`entries` AS `Entry`
inner JOIN `usr_web12_1`.`entrieslocations` AS `Entrieslocation` ON (`Entry`.`id` = `Entrieslocation`.`entry_id`)
inner JOIN `usr_web12_1`.`addresses` AS `Address` ON (`Address`.`id` = `Entry`.`address_id`)
WHERE ((`Entry`.`title` LIKE '%%') OR (`Entry`.`description` LIKE '%%') OR (`Entry`.`description_long` LIKE '%%') OR (`Entry`.`meta_keywords` LIKE '%%') OR (`Entry`.`filter_keywords` LIKE '%%')) AND `Entry`.`status` = 1 AND
`Entry`.`latitude` Between 52.179974594081 AND 53.978617805919 AND `Entry`.`longitude` Between 7.3045938084915 AND 10.298793591508 AND `Entrieslocation`.`category_id` = 1
GROUP BY `Entry`.`id`
ORDER BY `Entry`.`product_type` desc
LIMIT 10
Question: The rank-part doesn't work "Column not found: 1054 Unknown column 'score' in 'field list'", how can I access a dynamic AS-Field???
Same problem with distance...
Thanks a lot!
You can't use an aliased column name in the select or where clause.
You could use it later in the group by, order by, having clause. See the MySQL doc for that.
My first time working with CASE Logic in SQL statements. Everything works if I remove the CASE statements, so the SQL is valid without it.
I need to calculate the total item price based on a couple of things.
If "Sales Price" is active AND "Option Upcharge" has a value, the total is: Qty * (Sales Price + Option Upcharge)
If "Sales Price is inactive AND "Option Upcharge" has a value, the total is: Qty * (Price + Option Upcharge)
If "Sales Price" is active AND "Option Upcharge" has NO value, the total is: Qty * Sales Price
If "Sales Price is inactive AND "Option Upcharge" has NO value, the total is: Qty * Price
If no Option was added, the value for tblproduct_options.option_upcharge is NULL in the output.
Thanks for the help.
Brett
Here is my SQL:
SELECT tblshopping_cart.session_id, tblshopping_cart.product_id, tblshopping_cart.product_qty, tblshopping_cart.product_option, tblproducts.product_title, tblproducts.product_price, tblproducts.product_sale_price_status, tblproducts.product_sale_price, tblproduct_options.option_text, tblproduct_options.option_upcharge,
CASE
WHEN (tblproducts.product_sale_price_status = 'Y')
CASE
WHEN (tblproduct_options.option_upcharge IS NOT NULL)
THEN (tblshopping_cart.product_qty * (tblproducts.product_sale_price + tblproduct_options.option_upcharge))
ELSE (tblshopping_cart.product_qty * tblproducts.product_sale_price)
END
ELSE
CASE
WHEN (tblproduct_options.option_upchage IS NOT NULL)
THEN (tblshopping_cart.product_qty * (tblproducts.product_price + tblproduct_options.option_upcharge))
ELSE (tblshopping_cart.product_qty * tblproducts.product_price)
END
END AS product_total
FROM tblshopping_cart
INNER JOIN tblproducts ON tblshopping_cart.product_id = tblproducts.product_id
LEFT JOIN tblproduct_options ON tblshopping_cart.product_option = tblproduct_options.option_product_id
ORDER BY tblshopping_cart.product_qty ASC
It fails with with message:
CASE
WHEN (tblproduct_options.option_upcharge IS NOT NULL)
THEN (tblshopping_' at line 4
You are missing a THEN in your first CASE Statement. (sorry I had to add table aliases)
SELECT sc.session_id
, sc.product_id
, sc.product_qty
, sc.product_option
, p.product_title
, p.product_price
, p.product_sale_price_status
, p.product_sale_price
, po.option_text
, po.option_upcharge
, CASE
WHEN (p.product_sale_price_status = 'Y')
THEN <-- add this
CASE
WHEN (po.option_upcharge IS NOT NULL)
THEN (sc.product_qty * (p.product_sale_price + po.option_upcharge))
ELSE (sc.product_qty * p.product_sale_price)
END
ELSE
CASE
WHEN (po.option_upchage IS NOT NULL)
THEN (sc.product_qty * (p.product_price + po.option_upcharge))
ELSE (sc.product_qty * p.product_price)
END
END AS product_total
FROM tblshopping_cart sc
INNER JOIN tblproducts p
ON sc.product_id = p.product_id
LEFT JOIN tblproduct_options po
ON sc.product_option = po.option_product_id
ORDER BY sc.product_qty ASC
It looks like you're missing THEN in the outer CASE:
CASE
WHEN (tblproducts.product_sale_price_status = 'Y') THEN
^^^^ add this
I think your problem is the way you write you case.
You missed a THEN after your first when. You also missed a END.
CASE
WHEN (tblproducts.product_sale_price_status = 'Y')
THEN
CASE
WHEN (tblproduct_options.option_upcharge IS NOT NULL)
THEN (tblshopping_cart.product_qty * (tblproducts.product_sale_price + tblproduct_options.option_upcharge))
ELSE (tblshopping_cart.product_qty * tblproducts.product_sale_price)
END
ELSE
CASE
WHEN (tblproduct_options.option_upchage IS NOT NULL)
THEN (tblshopping_cart.product_qty * (tblproducts.product_price + tblproduct_options.option_upcharge))
ELSE (tblshopping_cart.product_qty * tblproducts.product_price)
END
END AS product_total