goal id total occurance of goal id total occurance when status is 1
1 5 3
This is schema of the table
CREATE TABLE `goal_objectives` (
`objective_id` int(11) NOT NULL ,
`objective_name` varchar(255) NOT NULL,
`objective_description` tinytext NOT NULL,
`goal_id` int(11) NOT NULL,
`objective_status` tinyint(4) NOT NULL
);
select goal_id, count(objective_status)as objective_done
from goal_objectives
where objective_status='1' group by goal_id;
select goal_id,count(goal_id) as total_current_goals
from goal_objectives
group by goal_id
order by goal_id DESC ;
I just want to show the combine result of these two queries.
Individually it returns required result but when i try to merge them is does not work
See the output in the link below:
https://i.imgur.com/6Rnac89.png
Use conditional aggregation:
select goal_id, count(*) as total_current_goals,
sum( objective_status = 1 ) as objective_done
from goal_objectives
group by goal_id
order by goal_id desc ;
Note that objective_status is a number. The comparison value should be a number, not a string.
I am using Alias.
Below is my query. I want to avoid showing rows with NULL counter.
SELECT activity_id, user_id,
(CASE WHEN activity_id = 1 OR activity_id = 2 THEN user_id END) AS counter
FROM eventedge_ticket_activity
It gives the following result:
How can I skip rows with counter NULL?
Not tested, but below should work.
SELECT activity_id, user_id,
CASE activity_id WHEN 1 THEN user_id
WHEN 2 THEN user_id
ELSE NULL
END AS counter
FROM eventedge_ticket_activity
WHERE counter IS NOT NULL
Below is the query which ran successfully.
SELECT * FROM (SELECT activity_id, user_id, case when activity_id=1 then user_id when activity_id=2 then user_id end as counter FROM eventedge_ticket_activity) db where counter != 'NULL'
I have a feeling that the sample data is over simplified so I may be quite wrong but this looks like the same query parsing the data only once.
SELECT activity_id, user_id,
user_id AS counter
FROM eventedge_ticket_activity
WHERE activity_id in(1,2)
BTW the answer provided by marmeladze should also work with the change I mentioned in my comment
Inner 'SELECT' gives table with NULL's, outer 'SELECT' cuts off rows with NULL counter
SELECT * FROM (
SELECT activity_id, user_id,
(CASE WHEN activity_id = 1 OR activity_id = 2 THEN user_id END) AS counter
FROM eventedge_ticket_activity) AS ResultTable
WHERE ResultTable.counter IS NOT NULL
I have two tables: vehicle_c2c_car_source and vehicle_c2c_appoint_task.
The structure of vehicle_c2c_car_source:
/********************************/
`id` int(11) NOT NULL AUTO_INCREMENT
`title` varchar(200) NOT NULL DEFAULT
/******************************/
vehicle_c2c_appoint_task
/******************************/
`id` int(11) unsigned NOT NULL AUTO_INCREMENT
`car_source_id` int(11) NOT NULL DEFAULT '0'
`status` tinyint(4) NOT NULL DEFAULT '0'
The value of status can be 0,1,2. one car_source_id can have many status.
The first goal is to get all the car_source that the amount of status records is less than 3.
The second goal is to get the amount of the status records when status is equal to 1.
The output should something like that:
22222(id of status records) 1111(car_source_id) title(title) 4(amount of the status records when status = 1)
My current solution is first to get all the car_source that meet the first goal and then query vehicle_c2c_appoint_task table in a loop to accomplish the second goal.
SELECT cs.id, pt.car_source_id, cs.title,
FROM vehicle_c2c_car_source AS cs
JOIN vehicle_c2c_appoint_task AS pt ON cs.id = pt.car_source_id
WHERE 1
AND pt.appoint_status NOT IN (2,6)
GROUP BY pt.car_source_id HAVING count(pt.car_source_id) < 3
In the loop:
SELECT count(*) as count
FROM vehicle_c2c_appoint_task
WHERE status = 1
group by car_source_id
I think it is not a good idea to put the query in a loop. How can i get all the value in one query?
Your question is hard to follow. But you can combine the two queries by using conditional aggregation, both in the SELECT and the HAVING clause:
SELECT cs.id, pt.car_source_id, cs.title, SUM(status = 1) as cnt
FROM vehicle_c2c_car_source cs JOIN
vehicle_c2c_appoint_task pt
ON cs.id = pt.car_source_id
GROUP BY pt.car_source_id
HAVING SUM(pt.appoint_status NOT IN (2, 6)) < 3;
I want to calculate total and unique clickouts based on country,partner and retailer.
I have achieved the desired result but i think its not a optimal solution and for longer data sets it will take longer time. how can I improve this query?
here is my test table, designed query and expected output:
"country_id","partner","retailer","id_customer","id_clickout"
"1","A","B","100","XX"
"1","A","B","100","XX"
"2","A","B","100","XX"
"2","A","B","100","GG"
"2","A","B","100","XX"
"2","A","B","101","XX"
DROP TABLE IF EXISTS x;
CREATE TEMPORARY TABLE x AS
SELECT test1.country_id, test1.partner,test1.retailer, test1.id_customer,
SUM(CASE WHEN test1.id_clickout IS NULL THEN 0 ELSE 1 END) AS clicks,
CASE WHEN test1.id_clickout IS NULL THEN 0 ELSE 1 END AS unique_clicks
FROM test1
GROUP BY 1,2,3,4
;
SELECT country_id,partner,retailer, SUM(clicks), SUM(unique_clicks)
FROM x
GROUP BY 1,2,3
Output:
"country_id","partner","retailer","SUM(clicks)","SUM(unique_clicks)"
"1","A","B","2","1"
"2","A","B","4","2"
And here is DDL and input data:
CREATE TABLE test (
country_id INT(11) DEFAULT NULL,
partner VARCHAR(256) CHARACTER SET utf8 DEFAULT NULL,
retailer VARCHAR(256) CHARACTER SET utf8 DEFAULT NULL,
id_customer BIGINT(20) DEFAULT NULL,
id_clickout VARCHAR(256) CHARACTER SET utf8 DEFAULT NULL)
ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO test VALUES(1,'A','B','100','XX'),(1,'A','B','100','XX'),
(2,'A','B','100','XX'),(2,'A','B','100','GG'),
(2,'A','B','100','XX'),(2,'A','B','101','xx')
SELECT
country_id,
partner,
retailer,
COUNT(id_clickout) AS clicks,
COUNT(DISTINCT CASE WHEN id_clickout IS NOT NULL THEN id_customer END) AS unique_clicks
FROM
test1
GROUP BY
1,2,3
;
COUNT(a_field) won't count any NULL values.
So, COUNT(id_clickout) will only count the number of times that it is NOT NULL.
Equally, the CASE WHEN statement in the unique_clicks only returns the id_customer for records where they clicked, otherwise it returns NULL. This means that the COUNT(DISTINCT CASE) only counts distinct customers, and only when they clicked.
EDIT :
I just realised, it's potentially even simpler than that...
SELECT
country_id,
partner,
retailer,
COUNT(*) AS clicks,
COUNT(DISTINCT id_customer) AS unique_clicks
FROM
test1
WHERe
id_clickout IS NOT NULL
GROUP BY
1,2,3
;
The only material difference in the results will be that any country_id, partner, retailed that previously showed up with 0 clicks will now not appear in the results at all.
With an INDEX on country_id, partner, retailed, id_clickout, id_customer or country_id, partner, retailed, id_customer, id_clickout, however, this query should be significantly faster.
I think this is what you are after:
SELECT country_id,partner,retailer,COUNT(retailer) as `sum(clicks)`,count(distinct id_clickout) as `SUM(unique_clicks)`
FROM test1
GROUP BY country_id,partner,retailer
Result:
COUNTRY_ID PARTNER RETAILER SUM(CLICKS) SUM(UNIQUE_CLICKS)
1 A B 2 1
2 A B 4 2
See result in SQL Fiddle.
i want to select a count returned by a query when a particular column is null and another query to select a count when that column is not null into single query how can i achieve it..?
i had tried some of the example that are avail in SOF but no use..
for example i want to
select students count of class table where the address null and notnull
In MySQL this can do it
SELECT
SUM(IF(address IS NULL,1,0)) as `Student_With_No_Address`,
SUM(IF(address IS NOT NULL,1,0)) as `Student_With_Address`
FROM students
SQL Fiddle Demo
Output :
Student_With_No_Address | Student_With_Address
---------------------------------------------
4 | 6
Try this
SELECT
COUNT(CASE when address is null then 1 end) AS StudentsWithNoAddress,
COUNT(CASE when address is not null then 1 end) AS StudentsWithAddress
FROM Class
You have to write two SELECT statements and combine them using UNION
SELECT 'No Address' AS AddressStatus, COUNT(*) AS NoOfStudents
FROM Class WHERE Address IS NULL
UNION
SELECT 'With Address' AS AddressStatus, COUNT(*) AS NoOfStudents
FROM Class WHERE Address IS NOT NULL
select
SUM( CASE when studentId is not NULL THEN 1 else 0 END ) as result1 ,
SUM( CASE when studentId is NULL THEN 1 else 0 END) as result2
from class