I am currently working on a MySQL db with MySQL Workbench.
My objective is to retrieve the signups from a database to establish my company's KPIs on an Excel spreadsheet.
I wrote some sql queries that worked but I want to set up a very complete one in order to avoid using xxx different queries.
To get the signups for each month (based on 'created_at'), this makes the job:
SELECT year(u.created_at) year, monthname(u.created_at) month, COUNT(DISTINCT u.id) as 'New shoppers signups'
FROM users u
GROUP BY year, month
ORDER BY u.created_at
But I also wanted to have the total of previous signups for each month
Jan : 12
Feb : 14 (12 + 2 new signups)
March : 22 (14 + 8 new signups)
...
Where I get the sum of all the previous signups
I was thinking about something like:
DECLARE #month = '2012-01-01' //startdate
WHILE #month < curdate()
BEGIN
SELECT count(distinct u.id)
WHERE u.created_at < #month
dateadd(month, 1, #month) // incrementing to next month
END
But neither the while loop, the declare, set, or date function do work on MySQL Workbench.
I heard I have to declare procedures but I didn't have any more success...
I know I could use excel to get the result, but I want to improve my use of SQL and make this a very clear work.
You are actually close to the answer. Take your results and make that an inner query. Then that is basis of an outer query using MySQL variables to accumulate for each row.
select
pq.yearAdded,
pq.monthAdded,
pq.NewShoppers as 'New shoppers signups',
#runBal := #runBal + pq.NewShoppers as TotalNewShoppers
from
( SELECT
year(u.created_at) yearAdded,
monthname(u.created_at) monthAdded,
COUNT(DISTINCT u.id) as NewShoppers
from
users u
GROUP BY
year(u.created_at),
monthname(u.created_at)
ORDER BY
year(u.created_at),
monthname(u.created_at) ) pq,
( select #runBal := 0 ) sqlvars
I would just suggest having column names stay away from possible reserved words, such as Year, Month and other standard SQL commands and function names... otherwise you typically need to add tick-marks around the column names
Related
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 an SSRS table with the last 12 months' balance details. I need to create column that calculates the difference between the last and (last but one) months' balance. Eg: (July balance - June balance). Any suggestions on how to go about this?
Thanks in advance.
If you Database is Sql Server 2012 or later you should use Lag function. Something like this:
select Month, balance, lag(balance) over (order by Month) as PreviousBalance
from table
order by Month;
I have used AdventureWorks/MDXStepByStep database for query below that joins current month and prior month.
SELECT CurruentDate = a.[DateKey]
,LastMonthDate = b.DateKey
,CurrentValue = a.[FullDateAlternateKey]
,LastMonthValue = b.[FullDateAlternateKey]
,[Difference] = a.[DateKey] - b.[DateKey]
FROM [dbo].[DimDate] a
JOIN [dbo].[DimDate] b
on DATEADD(month, 1, b.FullDateAlternateKey) = a.FullDateAlternateKey
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Calculate a running total in MySQL
I'm monitoring the number of users created since 2011 in an application by month for a chart using MySQL and PHP. As a part of the query I would also like to include a running total.
SELECT
DATE_FORMAT(created,'%Y%m%d') as 'Date',
COUNT(item_id) as 'NewUsers'
FROM AP_user
WHERE YEAR(created) > 2011
AND user_groups = '63655'
AND user_active = 1
AND userID NOT IN $excludedUsers
GROUP BY MONTH(created) ASC
I'm able to return the "users by month" but how do I include a running total as a part of this query?
Unfortunately, MySQL doesn't provide analytic functions, like Oracle and SQL Server do.
One way to get a "running total" is to make use of a user variable, something like this:
SELECT t.Date
, t.NewUsers
, #rt := #rt + t.NewUsers AS `Running Total`
FROM (SELECT #rt := 0) i
JOIN (
SELECT DATE_FORMAT(created,'%Y%m%d') AS `Date`
, COUNT(item_id) as `NewUsers`
FROM AP_user
WHERE YEAR(created) > 2011
AND user_groups = '63655'
AND user_active = 1
AND userID NOT IN $excludedUsers
GROUP BY DATE_FORMAT(created,'%Y-%m')
ORDER BY DATE_FORMAT(created,'%Y-%m') ASC
) t
NOTE: The behavior of memory variables like used above is not guaranteed in this context. But if we are careful with the query, we can get predictable, repeatable results in SELECT statements. The behavior of memory variables may change in a future release, rendering this approach unworkable.
NOTE: I basically wrapped your query in parentheses, and gave it an alias as an inline view (what MySQL calls a "derived table"). I made a few changes to your query, your GROUP BY has potential to group January 2012 together with January from 2013, I changed that. I also added an ORDER BY clause.
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)
Is there an easy way to do a GROUP BY DATE(timestamp) that includes all days in a period of time, regardless of whether there are any records associated with that date?
Basically, I need to generate a report like this:
24 Dec - 0 orders
23 Dec - 10 orders
22 Dec - 8 orders
21 Dec - 2 orders
20 Dec - 0 orders
Assuming you have more orders than dates something like this could work:
select date, count(id) as orders
from
(
SELECT DATE_ADD('2008-01-01', INTERVAL #rn:=#rn+1 DAY) as date from (select #rn:=-1)t, `order` limit 365
) d left outer join `order` using (date)
group by date
One method is to create a calendar table and join against it.
I would create it permanently, and then create a task that will insert new dates, it could be done weekly, daily, monthly, etc.
Note, that I am assuming that you are converting your timestamp into a date.
Instead of using GROUP BY, make a table (perhaps a temporary table) which contains the specific dates you want, for example:
24 Dec
23 Dec
22 Dec
21 Dec
20 Dec
Then, join that table to the Orders table.
you need to generate an intermediate result set with all the dates in it that you want included in the output...
if you're doing this in a stored proc, then you could create a temp table or table variable (I don't knoiw MySQL's capabilities), but once you have all the dates in a table or resultset of some kind
Just join to the real dataa from the temp table, using an outer join
In SQL Server it would be like this
Declare #Dates Table (aDate DateTime Not Null)
Declare #StartDt DateTime Set #StartDt = 'Dec 1 2008'
Declare #EndDt DateTime Set #EndDt = 'Dec 31 2008'
While #StartDt < #EndDt Begin
Insert #Dates(aDate) Values(#StartDt)
Set #StartDt = DateAdd(Day, 1, #StartDt)
End
Select D.aDate, Count(O.*) Orders
From #Dates D Left Join
OrderTable O On O.OrderDate = D.aDate
Group By D.aDate
In a data warehouse, the method taken is to create a table that contains all dates and create a foreign key between your data and the date table. I'm not saying that this is the best way to go in your case, just that it is the best practice in cases where large amounts of data need to be rolled up in numerous ways for reporting purposes.
If you are using a reporting layer over SQL Server, you could just write some logic to insert the missing dates within the range of interest after the data returns and before rendering your report.
If you are creating your reports directly from SQL Server and you do not already have a data warehouse and there isn't the time or need to create one right now, I would create a date table and join to it. The formatting necessary to do the join and get the output you want may be a bit wonky, but it will get the job done.
There's a pretty straightforward way to do this… except that I can't remember it. But I adapted this query from this thread:
SELECT
DISTINCT(LEFT(date_field,11)) AS `Date`,
COUNT(LEFT(date_field,11)) AS `Number of events`
FROM events_table
GROUP BY `Date`
It works in MySQL too