count ocurrences over each day in mysql - mysql

I'm having some problems figuring out how to solve but I can't come with an answer at all.This is my problem.
I have a mySQL table like the following:
cust_id,date_removed,station_removed,date_arrived,station_arrived
6,"2010-02-02 13:57:00",56,"2010-02-02 13:58:00",77
6,"2010-02-02 15:12:00",66,"2010-02-02 15:12:00",56
30,"2010-02-05 11:36:00",32,"2010-02-05 11:37:00",14
30,"2010-02-05 11:37:00",14,"2010-02-05 11:37:00",20
30,"2010-02-05 12:41:00",85,"2010-02-05 12:43:00",85
30,"2010-02-05 12:44:00",85,"2010-02-05 12:46:00",85
30,"2010-02-06 13:15:00",8,"2010-02-06 13:17:00",20
30,"2010-02-06 13:18:00",23,"2010-02-06 13:19:00",23
30,"2010-02-06 13:20:00",32,"2010-02-06 13:21:00",39
30,"2010-02-06 13:21:00",11,"2010-02-06 13:21:00",23
30,"2010-02-06 13:21:00",76,"2010-02-06 13:22:00",32
which the corresponding datatypes in each field is the following:
cust_id: varchar()
date_removed: datetime
station_removed: int
date_arrived: datetime
station_arrived: int
Next, I was asked to make a query to get the count over every station used along the day, to get a table like this one:
station 2010-02-02 2010-02-05 2010-02-06
56 2 0 0
66 1 0 0
32 0 1 2
14 0 2 0
85 0 2 0
8 0 0 1
23 0 0 2
11 0 0 1
76 0 0 1
77 1 0 0
20 0 1 1
39 0 0 1
where the columns are the days and the rows are each station. I'm not a very good mySQL user neither.
Could somebody help me on this one?.
Thank you in advance

Use this query :
select stations.name as station,
(select count(*) from table where date(date_arrived)='2010-02-02' and (station_removed=stations.name or station_arrived=stations.name)) as '2010-02-02'
(select count(*) from table where date(date_arrived)='2010-02-05' and (station_removed=stations.name or station_arrived=stations.name)) as '2010-02-05'
(select count(*) from table where date(date_arrived)='2010-02-06' and (station_removed=stations.name or station_arrived=stations.name)) as '2010-02-06'
from
(select station_removed as name from table
union
select station_arrived from table ) stations ;

Related

Using sum for two conditions with same attribute

When I am creating a table to count the user between 18-40, I need to have two conditions about BirthYear in the expression like sum((age>=18 and age<=40) and (Gender='M')), but the return for this one always equal 0, the whole query and output like below, (MySQL 5.30)
create table AgeUser
select
Id as 'Id',
sum((age<18) and (Gender='M')) as 'MaleUsersUnder18',
sum((age>=18 and age<=40) and (Gender='M')) as 'MaleUsers18To40',
sum((age>40) and (Gender='M')) as 'MaleUsersOver40',
sum((age<18) and (Gender='F')) as 'FemaleUsersUnder18',
sum((age>=18 and age<=40) and (Gender='F')) as 'FemaleUsers18To40',
sum((age>40) and (Gender='F')) as 'FemaleUsersOver40'
from User group by Id;
id MUUnder18 MU18To40 MUOver40 FUUnder18 FU18To40 FUOver40
72 2137 0 1316 645 0 123
79 2613 0 1616 1064 0 676
82 592 0 363 203 0 554
example for User table
The Id mean the Service Station ID
Id userid Name age gender UserType
72 12 L 18 M customer
How do I fix the query?
Best sum something
sum(case when age<18 and Gender='M' then 1 else 0 end) as 'MaleUsersUnder18',
etc..

mysql pick up a random entry selected from certain criterial

I want sort one user from table who never had a note different from 0
then update the record,
then insert a new row
user note
12 1
23 0
88 0
45 0
12 0
23 0
12 0
88 2
sort a user except user 12 and user 88 becouse the have already a note somewhere
somethink like
SELECT * FROM table WHERE note=0 ORDER BY rand() LIMIT 1
the problem is that i've many users duplicate so i dont know how to exlude that...
let's say that i randomly choose the user 23
the table should become
user note
12 1
23 0
88 0
45 0
12 0
23 X <--- mark the random user choosen
12 0
88 2
23 0 <--- add a new line
in the next random pick up only the number 45 will be avaiable becouse other user has somewhere a note != 0
for this last request i've to do 2 query UPDATE then INSERT or i can do with just one query?
You can avoid subqueries for improved performance and go like this:
SELECT *
FROM YourTable
GROUP BY name
HAVING SUM(note)=0 ORDER BY rand() LIMIT 1;
Here is an SQL Fiddle DEMO.
You can use nested select :
SELECT * FROM table WHERE note=0 and user not in (select user from table where note>0) ORDER BY rand() LIMIT 1
however u should really use primary unique index on users

can i create 100 rows with IDs from 1 to 100 without inserting them one by one?

id status
1 0
2 0
3 0
4 0
5 0
.
.
100 0
This should be the format. i need to create id and status column putting 1 to 100 in id and zero in status. i would like to know is there any way i can create it with query or i have to do it manually
select #i:=#i+1 as id
,0 as status
from mysql.help_keyword,(select #i:=0) t
limit 100
;

Using MySQL to select data from the last three months

I have a table which contains the following columns:
ID = unique identifier in ascending order
ProductId = Id of a product
rate1 to rate5 = number of times that product has recived a 1 star, 2
star, 3 star, 4 star or 5 star rating
aveRate = average rating for that product
lastSubDate = last time a review was submitted for that product
And I'm trying to write an SQL statement which selects the ProductId's which have the highest number of 5 star ratings also with an average rating of 5 and the last review submission was within the last three months.
The table looks like this example:
ID ProductId rate1 rate2 rate3 rate4 rate5 aveRate lastSubDate
18 9996637 0 0 0 0 1 5 2011-08-10 12:00:34
26 9996628 1 0 0 0 0 1 2010-05-06 05:45:05
34 9996618 0 0 0 1 0 4 2011-10-09 09:00:45
36 9996614 5 0 0 0 0 1 2011-01-05 09:30:32
48 9996592 5 0 1 0 3 3 2012-11-28 19:00:06
66 9996566 0 0 0 1 3 5 2011-04-06 06:45:34
70 9996562 0 0 0 1 1 5 2011-05-17 18:30:03
This is the query I've got so far:
SELECT `ProductId`,`rate5`,`aveRate`,`lastSubDate`
FROM ratings
WHERE `aveRate` = 5 AND `lastSubDate` > '24 Feb 2013'
ORDER BY `rate5` DESC
LIMIT 3
This returns the products with the most 5 star reviews which also have an average rating of 5, however it doesn't limit the results to the last three months as I want. How can I amend this statement to also select data from only the last three months?
Your date constant is in the wrong format. Try this if you're using a constant date:
... AND `lastSubDate` > '20130224'
You can also use 20130224 (without the quotes) or '2013-02-24' above - see the MySQL Date and Time Literals documentation.
Or to calculate "three months ago" just do this:
... AND `lastSubDate` > CURDATE() - INTERVAL 3 MONTH
Finally, you don't need the backticks here. If you find them distracting you can drop them:
... AND lastSubDate > whatever

mySQL to retrieve records that repeat a value in order

I have a big table with 300,000 records. This table has a integer value called "velocity" and it`s value is from 0 to 100.
In the firsts records, the value is 0 and I want to remove. I want to remove from the query, the records where the velocity field repeats more than 10 times. For example:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 2 3 5 10 12 13 15 20 30 20 15 10 8 5 2 1 0 0 0 0 4 5 10 20...
[-------remove this-----------].......................................................................[---------] <- do not remove this
Thanks
The easiest way to do this is with a loop.
You can write a stored procedure that iterates through the records, or you might do it outside of the database. I'd do it like that if this needs to be done once. If this is a continuous process, it's better to make sure that the extra data is just not inserted into the database in the first place.
Anyway, if you insist on doing this in pure SQL, without stored procedures with loops, you can use a query like this:
set #groupnum=0;
select
GroupNum,
count(*) as RecsInGroup
from
(
select
t1.id as Id,
t1.velocity as velocity1,
t2.velocity as velocity2,
if(t1.velocity<>t2.velocity,#groupnum:=#groupnum+1,#groupnum) as GroupNum
from
VelocityTable as t1
join
VelocityTable as t2
on
t1.id=t2.id-1
) as groups
group by
GroupNum
having RecsInGroup>10
What happens here?
Step 1
The inner query just selects all records in your table, but splits the data in sequential groups.
So, using your example, it does this:
velocity : 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 2 3 5 10 12 13 15 20 30 20 15 10 8 5 2 1 0 0 0 0 4 5 10 20
Groupnum : 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 18 18 18 19 20 21 22
It does that by joining the table to itself, by linking subsequent records in the table. Every time the left and right velocity are different, the GroupNum is increased. Otherwise it's left unchanged.
Step 2
The result if the query is wrapped in an outer query, and grouped by GroupNum. Again, using your example it would result in this:
GroupNum,RecsInGroup
0,15 // !!
1,1
2,1
3,1
4,1
5,1
6,1
7,1
8,1
9,1
10,1
11,1
12,1
13,1
14,1
15,1
16,1
17,1
18,4 // !!
19,1
20,1
21,1
By Adding the having RecsInGroup>10 clause, the result becomes this:
GroupNum,RecsInGroup
0,15
Now, with this list of GroupNum's you can delete records.
Step 3
With the query above you have:
A list of all your records, with an added GroupNum column.
The list of GroupNum's that need to be removed.
Deleting the records should be easy at this point.
I'd just rip through the records sequentially, with a variable sized window that expands and contracts to comprehend identical values. Whenever the size is >= 10 when the value changes, delete the rows using the primary keys.
You can put BEGIN TRAN and COMMIT TRAN at the beginning and end of the DELETE statements to make things reasonably efficient.
thank you very much. I'm allmost there, but i tried it with a mySQL View as table source and it's not working (unkown table xxx). I can't use the whole table because it's have more than 19 millions records, I just need the record from a specific day, vehicle plate and city.