I'm trying to count the number of occurrences of two values "1" and "2" in the "ticket" column, but I get an error when I add another line from "where" and "from".
"ticket" is a column, "tickets" is a table.
SELECT
COUNT(ticket) as child FROM tickets WHERE ticket = 1,
COUNT(ticket) as adult FROM tickets WHERE ticket = 2
GROUP by ticket
SELECT
COUNT(if(ticket = 1,1,NULL)) as child,
COUNT(if(ticket = 2,1,NULL)) as adult FROM tickets where ticket in (1,2)
GROUP by ticket
or
SELECT
sum(if(ticket = 1,1,0)) as child,
sum(if(ticket = 2,1,0)) as adult FROM tickets where ticket in (1,2)
GROUP by ticket
try like this
Use conditional aggregation:
SELECT SUM(t.ticket = 1) as t1,
SUM(t.ticket = 2) as t2
FROM tickets t;
You're looking for conditional aggregation, like so:
SELECT COUNT(IF(ticket=1, 1, NULL) AS child
, COUNT(IF(ticket=2, 1, NULL)) AS adult
FROM tickets
;
COUNT only counts non-null values.
Your query is syntactically invalid, try this one:
Select count(*)
, ticket
from tickets
where ticket In (1,2)
Group by ticket
;
Try this out and let me know in case of any queries.
SELECT SUM(ticket = 1) as t1,
SUM(ticket = 2) as t2
FROM tickets
group by tickets;
Related
I Have this table:
[Messages table]
I need to find the number of uniqe conversation -
conversation is define as senderid sent msg to reciverid, and reciverid has replied (no matter how many times or the thread length, it will be count as 1 conversation).
so if senderid = 1, reeiver id =2
and in the next row senderid = 2 and reciever id =1 this is one conversation (till the end of time)
Im really stock and not sure how to proceed.
Thanks!
You can use the functions LEAST() and GREATEST() to create unique combinations of the 2 ids and aggregate:
SELECT COUNT(DISTINCT LEAST(m1.senderid, m1.receiverid), GREATEST(m1.senderid, m1.receiverid)) counter
FROM Messages m1
WHERE EXISTS (SELECT 1 FROM Messages m2 WHERE (m2.receiverid, m2.senderid) = (m1.senderid, m1.receiverid))
See the demo.
Results (for your sample data):
counter
2
Here's one option using a self join with least and greatest to get your desired results:
select count(*)
from (
select distinct least(m1.senderid, m2.senderid), greatest(m1.receiverid, m2.receiverid)
from messages m1
join messages m2 on m1.senderid = m2.receiverid and m1.receiverid = m2.senderid
) t
i.e. when new trades are in the current date trade list but not in the previous date trade list.
I have tried
SELECT COUNT(*) AS "Number of New Trades"
FROM TRADE_REPORT
WHERE business_date='2018-05-08' and excluded='false'
MINUS
SELECT COUNT(*) AS "Number of New Trades"
FROM TRADE_REPORT
WHERE business_date='2018-05-07'
and excluded='false'
But it did not worked
No. MINUS is a set operator, not an arithmetic operator, and it is not even available in MySQL. I think you want:
select count(*)
from trade_report tr
where tr.business_date = '2018-05-08' and tr.excluded = 'false' and
not exists (select 1
from trade_report tr2
where tr2.? = tr.? and
tr2.business_date = '2018-05-07' and
tr2.excluded = 'false'
);
The ? is for the id of what you want compare from one day to the next.
Consider a NOT IN clause capturing non-matches of IDs between the two days.
SELECT COUNT(*) AS "Number of New Trades"
FROM TRADE_REPORT t1
WHERE t1.business_date='2018-05-08' AND t1.excluded='false'
AND t1.ID NOT IN
(SELECT sub.ID
FROM TRADE_REPORT sub
WHERE sub.business_date='2018-05-07' AND sub.excluded='false')
In MySql I obtained 2 list/tables from these 2 queries. Table 1 contains Quantities for a type of ticket, Table 2 contains Prices of a type of ticket.
Table 1:
SELECT Count(`ticket`.`Ticket_type`) AS Counter
FROM `ticket`
WHERE ((`ticket`.`Ticket_type` = 'Adult') OR (`ticket`.`Ticket_type` = 'Senior'))
GROUP BY `ticket`.`Ticket_type`
Table2 :
SELECT `ticketprice`.`price`
FROM `ticketprice`
WHERE ((`ticketprice`.`Ticket_type` = 'Adult') OR (`ticketprice`.`Ticket_type` = 'Senior'))
My question is how do I being to multiply these two tables? (Qunatity * Price) = Total
Will Appreciate any help!
Join the tables and multiply:
SELECT t.ticket_type, COUNT(*) AS quantity, p.price, p.price * COUNT(*) AS total
FROM ticket AS t
JOIN ticketprice AS p ON t.ticket_type = p.ticket_type
WHERE t.ticket_type IN ('Adult', 'Senior')
GROUP BY t.ticket_type
Query with OR which outputs wrong
SELECT DISTINCT
sm___employees.id,
sm___employees.employee_code,
sm___employees.leaving_date,
sm___employees.name_of_employee,
sm___employees.position,
sm___employees.rating,
sm___employees.entry_date
FROM
sm___employees
JOIN
sm___employee_skills
ON
sm___employees.id=sm___employee_skills.employee_id
WHERE
((sm___employee_skills.skill_id=1 AND sm___employee_skills.ans LIKE '%MBA%')
**OR**
(sm___employee_skills.skill_id=5 AND sm___employee_skills.ans IN (3)))
AND
sm___employees.rating IN (1)
ORDER BY
sm___employee_skills.date DESC
But I want it by And
SELECT DISTINCT
sm___employees.id,
sm___employees.employee_code,
sm___employees.leaving_date,
sm___employees.name_of_employee,
sm___employees.position,
sm___employees.rating,
sm___employees.entry_date
FROM
sm___employees
JOIN
sm___employee_skills
ON
sm___employees.id=sm___employee_skills.employee_id
WHERE
((sm___employee_skills.skill_id=1 AND sm___employee_skills.ans LIKE '%MBA%')
**AND**
(sm___employee_skills.skill_id=5 AND sm___employee_skills.ans IN (3)))
AND
sm___employees.rating IN (1)
ORDER BY
sm___employee_skills.date DESC
When am using first query with OR of MBA or 3, It gives me result for both which is correct as per OR operation
I want only those records which are having MBA AND 3 which gives me blank records when there are records available with this comparison
So please help me to resolve this.
Thank you in advance
To start with: DISTINCT often indicates a badly written query. This is the case here. You are joining records only to dismiss them later. If you want employee records, then select from the employee table. If you have criteria on the skills table check this in the WHERE clause. Don't join.
Then the WHERE clause looks at one row at a time. So neither skill_id = ... AND skill_id = ... nor skill_id = ... OR skill_id = ... can work for you. You must look up the skills table twice:
SELECT
id,
employee_code,
leaving_date,
name_of_employee,
position,
rating,
entry_date
FROM sm___employees
WHERE rating IN (1)
AND id IN
(
SELECT employee_id
FROM sm___employee_skills
WHERE skill_id = 1 AND ans LIKE '%MBA%'
)
AND id IN
(
SELECT employee_id
FROM sm___employee_skills
WHERE skill_id = 5 AND ans IN (3)
);
And here is a way to look up skills just once:
SELECT
id,
employee_code,
leaving_date,
name_of_employee,
position,
rating,
entry_date
FROM sm___employees
WHERE rating IN (1)
AND id IN
(
SELECT employee_id
FROM sm___employee_skills
WHERE (skill_id = 1 AND ans LIKE '%MBA%')
OR (skill_id = 5 AND ans IN (3))
GROUP BY employee_id
HAVING COUNT(DISTINCT skill_id) = 2 -- both skills
);
It seems strange though that you consider ans to be a string in one place (ans LIKE '%MBA%') and a number in another (ans IN (3)).
UPDATE: If you want to sort by skill date, you should consider by which skill's date. For this to happen, you would join, but not join the skills table, but the skills aggregate result. E.g.:
SELECT
e.id,
e.employee_code,
e.leaving_date,
e.name_of_employee,
e.position,
e.rating,
e.entry_date
FROM sm___employees e
JOIN
(
SELECT employee_id, MAX(date) AS max_date
FROM sm___employee_skills
WHERE (skill_id = 1 AND ans LIKE '%MBA%')
OR (skill_id = 5 AND ans = 3)
GROUP BY employee_id
HAVING COUNT(DISTINCT skill_id) = 2 -- both skills
) s ON s.employee_id = e.id
WHERE e.rating = 1
ORDER BY s.max_date;
Please try this :
SELECT DISTINCT
sm1.id,
sm1.employee_code,
sm1.leaving_date,
sm1.name_of_employee,
sm1.position,
sm1.rating,
sm1.entry_date
FROM sm___employees sm1
LEFT JOIN sm___employee_skills sm2 ON sm1.id = sm2.employee_id
WHERE ((sm2.skill_id=1 AND sm2.ans LIKE '%MBA%')
AND (sm2.skill_id=1 AND sm2.ans=3))
AND sm1.rating IN (1)
ORDER BY sm2.date DESC;
I'm having trouble creating a query that will report on training completions by users that are grouped by district within a region. For one training course the report needs to show for each district
Number of users assigned course
Number of users completed course
Percentage of users completed course
The output report should look like this (without the periods).:
District.........Assigned.......Completed....%
Arkansas..........20..............15...............75%
Illinois...............80..............80...............100%
Iowa.................10...............8.................80%
Michigan..........30..............20................66%
Here's the SQL query I have tried using
Select mytable.district as District,
(Select Count(Distinct mytable.user_id)
From mytable
Where mytable.district = District AND
mytable.training_title = 'My Course') As 'Assigned',
(Select Count(Distinct mytable.user_id)
From mytable
Where mytable.training_status = "Completed" AND
mytable.district = District AND
mytable.training_title = 'My Course') as 'Completed',
Concat(Round(100 * (Select Count(Distinct mytable.user_id)
From mytable
Where mytable.training_status = "Completed" AND
mytable.district = District AND
mytable.training_title = 'My Course') / (Select Count(Distinct mytable.user_id)
From mytable
Where
mytable.district = District AND
mytable.training_title = 'My Course'),0 ),"%") as '%'
From mytable
Where mytable.region = 'Midwest'
Group by District
It doesn't work at all like this. However if I substitute one of the district values (such as Arkansas) in for 'District' in the WHERE clause I can get the correct values for that district. However i need to find all of the districts for each region and calculate the values for district. This is only one of the regions I need to create the query for.
Two key issues:
All of the data exists in one table in my database. I have consolidated and imported it from another database.
The data contains duplicates Therefore I must use Distinct to eliminate the duplicates from the results.
Suggestions? Thanks in advance for your assistance!!!
As far as I can see, you could just sum things up as you want them and GROUP BY district. The subquery will take care of the duplicate rows.
SELECT
district,
COUNT(*) Assigned,
COUNT(CASE WHEN training_status='Completed' THEN 1 END) Completed,
CONCAT(COUNT(CASE WHEN training_status='Completed' THEN 1 END) /
COUNT(*) * 100, '%') `%`
FROM (
SELECT DISTINCT * FROM mytable
WHERE mytable.region = 'Midwest'
AND mytable.training_title = 'My Course') z
GROUP BY district;
An SQLfiddle to test with.