I am new to mysql. In the above table i have to SUM attendance_status(column) depending on the date like
01/05/2013 att_stat=0
01/05/2013 att_stat=1
02/05/2013 att_stat=0
02/05/2013 att_stat=1
It's working pretty well when thr is only 3 record when I insert 4th one its not working
SELECT SUM(Attendance_Status) as total FROM student_attendance1 Where Attendance_Status='1' and Date= '"+datevalue+"'";'
Basically you are close just have to put GROUP BY
SELECT
SUM(Attendance_Status) AS total
FROM student_attendance1
WHERE Attendance_Status = '1'
AND DATE = '"+datevalue+"'"
GROUP BY DATE
Related
Same-Store Sales concept if where you check how store performing today against same store yesterday to show if revenue grow or decrease.
so I have table with 5+ millions of records structured like
store_id , stats_date, trans_cnt (number of transactions),
revenue, time_period(week, day , year)
is there a way to avoid using cursor, to check if store_id record exist yesterday day and today and see if revenue goes up or down?
It can be achieved join on same filter data from table or sub table .
ie
select tdate.store_id ,(tdate.revenue - ydate.revenue) as diffrence
from (select store_id ,revenue from tablename where stats_date =getdata()) tdate
join ( select store_id ,revenue from tablename where stats_date = DATEADD(day, -1,getdata()) ) as ydate
on tdate.store_id = ydate.store_id
Note:
ydate filter data for yesterday
tdate filter data for yesterday
More filter condition can be added .
Or you are looking for
select tdate.store_id ,(tdate.revenue - ydate.revenue) as diffrence
from tablename tdate
join tablename as ydate
on tdate.store_id = ydate.store_id
and tdate.stats_date =ydate.DATEADD(day, -1,stats_date)
I am trying to retrieve last 3 months records. I need to sum order total amount by week. I have made following query.
select CONCAT("Week", WEEK(created_at)) as week_number, sum(total_cost)
from certified_diamond_orders
where created_at > 2016-11-22
and status = "delivered"
group by week("created_at")
But I am only getting one record with this. Infact my table has 2 years entries. Also I was trying to figure out how I can pull week start date and end date to diplay on my chart.
Any suggestions where I am making mistake?
week("created_at") looks like you're trying to determine the week of the string "created_at" rather than the column created_at. Which might explain why you're only getting one row in your result.
The date 2016-11-22 also looks suspiciously like a sum instead of a date (2016 - 11 - 22 = 1983 vs "2016-11-22"
Try this:
SELECT
CONCAT('Week', WEEK(created_at)) AS week_number,
SUM(total_cost)
FROM certified_diamond_orders
WHERE
created_at > '2016-11-22' AND
status = 'delivered'
GROUP BY WEEK(created_at)
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 am using SQL Server 2008 R2 and trying to find total records inserted between a range of dates grouped by insertdate. Since my insertdate contains hour,min and sec, all the records are being counted individually with same date but different time.
I want to keep the time information in my database but only in the query, I want to group by date omitting time.
#Created Date TotalRegistration
2014-10-20 14:40:47.757 1
2014-10-20 12:27:27.923 1
2014-10-20 12:25:25.613 1
should be
#Created Date TotalRegistration
2014-10-20 3
This is what I tried. Any suggestion will be really appreciated.
Thank You
Select Insertdate, COUNT(Insertdate) as TotalDailyRegistration from Customer where Insertdate between '2014-10-01 00:00:00.001' and '2014-11-08 23:59:59.743' group by Insertdate
Update: Followed Lamak's suggestion and it worked out fine. Since the dates were not ordered correctly, I also added order by CONVERT(DATE,Insertdate) after group by and its perfect.
It should be better to just group by the date part of your column:
SELECT CONVERT(DATE,Insertdate) InsertDate,
COUNT(Insertdate) as TotalDailyRegistration
FROM Customer
WHERE Insertdate >= '20141001'
AND InsertDate < '20141002'
GROUP BY CONVERT(DATE,Insertdate)
Just try this:
create table #test( createdDate datetime,totalRegistration int)
insert into #test values('2014-10-20 14:40:47.757',1)
insert into #test values('2014-10-20 12:27:27.923',1)
insert into #test values(' 2014-10-20 12:25:25.613',1)
select *From test
Select convert(nvarchar(10),createdDate,103), sum(totalRegistration) as TotalDailyRegistration
How does one sum up czas column which is time(H:m) records from table raport and set it as a new record (as total time of each member per month) in new table's (time) june column?
I'm trying something like this, but it doesn't work:
mysql_query("INSERT INTO time (czerwiec) SELECT SUM(czas) FROM raport WHERE data BETWEEN '2012-06-01' AND '2012-06-30'") or die ('{"success":"false"}');
Try this SELECT query -
SELECT
SEC_TO_TIME(SUM(TIME_TO_SEC(czas))) time_sum
FROM
raport
-- WHERE
-- YEAR(data) = 2012 AND MONTH(data) = 6
GROUP BY
YEAR(data), MONTH(data)
Uncomment WHERE condition to filter dates.
Then use INSERT...SELECT construction to populate another table.