I need to know how can I fetch data from Database, months wise, using a single query as of now I am fetching data inside the loop which ended up in 12 queries which causing the speed issue. Basically, I need this for the chart. What I am doing is getting months in a year and looping all the months. Inside that loop, i am putting the following query
Here is the query which I am using. I am saving the timestamp for the date.
SELECT SUM(value) AS sale
, COUNT(id) as tot
FROM tablename
WHERE 1
AND ud = 451
AND oreatedTime BETWEEN 1514782800 AND 1517461199
AND uaaZc = "0082844000224"`
You can try below -
SELECT year(FROM_UNIXTIME(oreatedTime)) as yr,MONTH(FROM_UNIXTIME(oreatedTime)) as mon,SUM(value) AS sale, COUNT(id) as tot
FROM tablename WHERE 1 AND ud="451" AND
oreatedTime BETWEEN 1514782800 AND 1517461199 AND uaaZc = "0082844000224"
group by year(FROM_UNIXTIME(oreatedTime)),MONTH(FROM_UNIXTIME(oreatedTime))
Related
I have a table in mySQL workbench that has several columns and 11 million rows.
One of the columns is a time stamp in the following format:
2014-01-01 00:12:54
There is another column which I'm doing a group by on.
My query currently looks something like this:
SELECT app_type, count(series_id) as 'Num of Series Downloaded' from access_log
WHERE action = 'download' AND org_id != 1
GROUP BY app_type;
Which produces a result this:
What I'd like to do is get the average number of series downloaded (series_id) per month given that I have a time column. Keeping in mind there are 5 years of data and 11 million rows.
Ideally, the result would be formatted something like this:
What could my query look like to format a result like what I'm aiming to achieve?
I think you just want a basic aggregation query:
SELECT DATE_FORMAT(`timestamp`, '%Y-%m') as yyyymm,
SUM(app_type = 'API') as API,
SUM(app_type = 'Web') as Web,
SUM(app_type = 'Excel') as Excel
FROM access_log
WHERE `action` = 'download' AND org_id <> 1
GROUP BY yyyymm;
I have a temp table and I'm trying to sum data but can't seem to get the logic right for it. The table contains customer level data and now I'm trying to aggregate it by fiscal year, quarter, and product description. I'm trying to sum by going back 1 year and using the same quarter to sum the # of units sold.
I can do this in excel, but the table is too large for that. This is what the formula in Excel looks like:
=SUMIFS(Units,FiscalYearQuarter >= Concat(FiscalYear -1 & FiscalQuarter, FiscalYearQuarter <= Concat(FiscalYear, FiscalQuarter)
Here's an example of the table:
Here's what the results should looks like (This does not include productdescription, but I will want to add that in):
Every time I try to group by or do a Sum(Case When...) I keep getting the results only by the fiscal year/quarter instead of the sum of historical for 1 year.
A simple GROUP BY will work (although I don't quite understand your Excel logic with concatenation):
SELECT t1.FiscalYear, t1.FiscalQuater, sum(t2.UnitsPurchased)
FROM `table` t1
LEFT JOIN `table` t2
ON ( t1.FiscalYear = t2.FiscalYear + 1
AND t1.FiscalQuater < t2.FiscalQuater)
OR ( t1.FiscalYear = t2.FiscalYear
AND t1.FiscalQuater >= t2.FiscalQuater)
GROUP BY t1.FiscalYear, t1.FiscalQuater
EDIT 1
modified query based on author's feedback
I am using the Graph Reports for the select below. The MySQL database only has the active records in the database, so if no records are in the database from X hours till Y hours that select does not return anything. So in my case, I need that select return Paypal zero values as well even the no activity was in the database. And I do not understand how to use the UNION function or re-create select in order to get the zero values if nothing was recorded in the database in time interval. Could you please help?
select STR_TO_DATE ( DATE_FORMAT(`acctstarttime`,'%y-%m-%d %H'),'%y-%m-%d %H')
as '#date', count(*) as `Active Paid Accounts`
from radacct_history where `paymentmethod` = 'PayPal'
group by DATE_FORMAT(`#date`,'%y-%m-%d %H')
When I run the select the output is:
Current Output
But I need if there are no values between 2016-07-27 07:00:00 and 2016-07-28 11:00:00, then in every hour it should show zero active accounts Like that:
Needed output with no values every hour
I have created such select below , but it not put to every hour the zero value like i need. showing the big gap between the 12 Sep and 13 Sep anyway, but there should be the zero values every hour
(select STR_TO_DATE ( DATE_FORMAT(acctstarttime,'%y-%m-%d %H'),'%y-%m-%d %H')
as '#date', count(paymentmethod) as Active Paid Accounts
from radacct_history where paymentmethod <> 'PayPal'
group by DATE_FORMAT(#date,'%y-%m-%d %H'))
union ALL
(select STR_TO_DATE ( DATE_FORMAT(acctstarttime,'%y-%m-%d %H'),'%y-%m-%d %H')
as '#date', 0 as Active Paid Accounts
from radacct_history where paymentmethod <> 'PayPal'
group by DATE_FORMAT(#date,'%y-%m-%d %H')) ;
I guess, you want to return 0 if there is no matching rows in MySQL. Here is an example:
(SELECT Col1,Col2,Col3 FROM ExampleTable WHERE ID='1234')
UNION (SELECT 'Def Val' AS Col1,'none' AS Col2,'' AS Col3) LIMIT 1;
Updated the post: You are trying to retrieve data that aren't present in the table, I guess in reference to the output provided. So in this case, you have to maintain a date table to show the date that aren't in the table. Please refer to this and it's little bit tricky - SQL query that returns all dates not used in a table
You need an artificial table with all necessary time intervals. E.g. if you need daily data create a table and add all day dates e.g. start from 1970 till 2100.
Then you can use the table and LEFT JOIN your radacct_history. So for each desired interval you will have group item (group by should be based on the intervals table.
I have a table which contains date (Field Type: Date and Date Format: %Y-%m-%d) as a field. I need to select all the rows from the table for all the years whose date is not between Dec 3rd and Dec 24th.
The table contains month and day as a separate fields.
The result can be obtained by using the following query:
select * from mytable where date not in (select date from mytable where month=12 and day between 3 and 24);
But i m trying to get the result in a single query like the below one but it gave empty rows:
select * from mytable where date not between '%Y-12-03' and '%Y-12-24';
Can it be done in a single query like the above one?
SELECT *
FROM mytable
WHERE MONTH(`date`) <> 12
OR DAY(`date`) NOT BETWEEN 3 AND 24
;
This will give you every row that meets the requirements. I'm sure someone has a faster way of doing this, since this will ignore all indexes and will likely be slow on a large dataset, but it does work and return the data you require, so if no-one can suggest an improvement this will answer your question.
I have a table with a list of tasks. Each task has a datetime field called "completedTime". Basically everytime a task is marked completed that field gets updated with the correct time.
Now I need to do a graph (using jQuery) for this result where the x axis is the months of the year (jan-dec) and the y axis is a number.
What is the sql query can I use so it would spit out 12 columns (Jan-Dec) with a number in each depending on how many tasks have a completedTime in that month.
I don't want to run the query below 12 times or each month.
SELECT * FROM `tasks` WHERE month(completedTime) between '02' and '03';
Any ideas?
If I understand correctly, your want it to return 12 rows (one for each month) with a count of the number of tasks.
If that is correct, then something like this should work. I added the year, which could be parametrized.
SELECT Count(*)
FROM Tasks
WHERE Year = 2011
GROUP BY Month(completedTime);
Revised with name for Month
SELECT Count(*) as total,
DateName(month, DateAdd(month, Month(completedTime), 0 ) - 1 ) as Month
FROM tasks
WHERE year(completedTime) = '2011'
GROUP BY Month(completedTime)