I have two select statement for find avg economy for car I need connect to single statement. I tired but showig error
Basically in first statement includes cast, avg, nullif groupby
In second statement contains where, when, case then
SELECT vehicle_id,CAST(AVG((NULLIF(economy,0))) AS int) FROM fillups group BY vehicle_id
WHERE vehicle_id <= 2 THEN 'Prius'
AND
vehicle_id >= 2 THEN 'Other';
Thats the code when put into single statement
Select vehicle_id, CAST(AVG((NULLIF(economy,0))) AS int),
when vehicle_id < 2 then 'Prius'
when vehicle_id >= 2 then 'other'
from fillups
group by vehicle_id
This may help !!
If not please mention some data schema structure.
Please try this:
Select vehicle_id, CAST(AVG((NULLIF(economy,0)) as int) as columnA,
case when vehicle_id < 2 then 'Prius'
when vehicle_id >= 2 then 'other'
end as columnB
from fillups
group by vehicle_id
Related
I have tbl_events
key userID date
1 1 1.1.2000
2 1 1.1.2017
3 2 2.2.1990
I'm trying to get this:
select distinct userID, date [that is earliest eg row 1] as earliest_date,
date [that is latest eg row 2] as latest_date
from tbl_events
if a userid has only one row in tbl_events then tbl_events.date would serve as both the earliest and latest_date
ie my result would be:
userID earliest_date latest_date
1 1.1.2000 1.1.2017
2 2.2.1990 2.2.1990
I can do this rather inefficiently with lots of looping but I'm wondering if there is a way to do this with "group by" or i have seen queries that seem to contain additional select statements in brackets
If anyone can please point me in the right direction I'd be very grateful.
Thank you.
Nick
You can use MAX() and MIN() aggregate function probably saying
select userid, max(`date`) as earliest_date,
min(`date`) as furthest_date
from tbl
group by userid;
I have a transaction table with datetime, type, measure. I want to produce the day, count, count with measure>20 for type=12.
Not sure how to go about it.
To get the day, count, type I'd write
select date(datetime), count from table where type=12 group by date(datetime)
Just not sure how to add the 3rd col (count with measure>20).
I've thought of trying a left self join, or a corelated subquery.
Appreciate any advice.
Table Name Alerts
Col1 Alert_time (datetime)
Col 2 Alert_type (integer)
Col 3 Measure (integer)
Sample data
2015/01/20 9:00|12|10
2015/01/20 8:00|12|30
2015/01/20 7:00|12|40
2015/01/21 5:00|13|30
2015/01/21 8:00|12|10
Desired Output
2015/01/20|3|2
2015/01/21|1|0
You could perform a count operation on a case expression:
SELECT DATE(datetime),
COUNT(*),
COUNT(CASE WHEN measure > 20 THEN 1 ELSE NULL END)
FROM mytable
WHERE type = 12
GROUP BY date(datetime)
You can have you query like this.
SELECT DATE(datetime),
COUNT(*),
COUNT(CASE WHEN measure > 20 THEN 1 ELSE 0 END)
FROM mytable
WHERE type = 12
GROUP BY date(datetime)
I have list of ids and corresponding creation dates
for Exmple :
1 2014-05-01
2 2014-07-01
3 2014-08-01
Need suggestion regarding writing a MySQL select statement which gives id details after corresponding creation date.
select id,count(*) from id_details where id IN(1,2,3) where resolved_at >(2014-05-01,2014-07-01,2014-08-01) group by id
The date condition for resolved_at column is not correct. Again, if you have two WHERE clause in your query, that as well not correct. You can's specify > condition in IN clause like you are trying. Your query should look like
select id,count(*)
from id_details
where id IN (1,2,3)
and (resolved_at >= '2014-05-01'
and resolved_at <= '2014-08-01')
group by id
I think you are just trying to use IN operator for resolved_at column like
select id,
count(*)
from id_details
where id IN (1,2,3)
and resolved_at IN ('2014-05-01','2014-07-01','2014-08-01')
group by id
I have a table with emp_id, income, etc.
I wish to get number of records for a query like
select * from table_name where income <= 500;
There will be at least 3 such income groups - which will b given at report generation time.
Further I wish to get all 3 Counts - and group the results by the count of their respective income group - all this in a single query.
What is the easiest way to do this?
Can you try this ,if this doesn't suite your need you may need to write a custom stored procedure
SELECT
sum((income <= 500)) as range1,
sum((income <= 1000)) as range2
FROM table_name
sample fiddle
You can use a CASE expression to create your categories, and then a GROUP BY to summarize the categories.
SELECT COUNT(*) AS num,
CASE WHEN income IS NULL THEN 'missing'
WHEN income <= 0 THEN '0'
WHEN income <= 500 THEN '1 - 500'
WHEN income <= 1000 THEN '501-1000'
ELSE '> 1000'
END AS category
FROM table_name
GROUP BY category WITH ROLLUP
Including the WITH ROLLUP clause will give you an overall count as well as the count of each category.
I have two sql queries
SELECT sub_id,
sub_sent_code,
date_time
FROM moviedatabase.reg_sub_master
WHERE (date_time >= '2013-01-19'
AND date_time <= '2013-01-29');
and
SELECT sub_id,
sub_sent_code,
date_time
FROM moviedatabase.reg_sub_master
WHERE date_time <= '2013-01-19';
My requirment is from 1st query what ever the result comes that sub_id,sub_sent_code,date_time should not be present in 2nd query .
I want to make it single query.
EDIT : actually i want the result where date_time >='2013-01-19' AND date_time <='2013-01-29' and the sub_id which i am getting should not the be there before '2013-01-19'
Try :
SELECT sub_id,sub_sent_code,date_time
FROM moviedatabase.reg_sub_master
WHERE (date_time >='2013-01-19'
AND date_time <='2013-01-29')
AND sub_id NOT IN (
SELECT DISTINCT sub_id
FROM moviedatabase.reg_sub_master
WHERE date_time <='2013-01-19;