php mysql command for daily report - mysql

I have a mysql table with some entries. sample data
nid | news_date
1 | 16 July 2015, 2:31 pm
2 | 16 July 2015, 2:31 pm
3 | 17 July 2015, 12:31 pm
4 | 18 July 2015, 4:28 pm
5 | 20 July 2015, 12:31 pm
I want daily report, and i tried with this sql command
SELECT count(nid), DATE(news_date)
FROM tbl_news
GROUP BY DATE(tbl_news.news_date);
But i am getting output as
count(nid) | DATE(news_date)
5 | NULL
But i want daily record count report, Anybody help.

Please try this query:-
SELECT count(nid), news_date
FROM tbl_news
GROUP BY (STR_TO_DATE(tbl_news.news_date, '%d %M %Y')) ;
I have removed the date keyword.

Related

Rearrange the position of the values in the table

I am using MYSQL. I have a table containing a start date and an end date for a schedule.
| StartDate | FinishDate |
|Feb 1, 2016 11:50 AM |Feb 2, 2016 3:37 PM |
|Feb 2, 2016 4:29 PM |Feb 3, 2016 8:16 PM |
|Feb 3, 2016 8:17 PM |Feb 3, 2016 8:34 PM |
What I was hoping to do is to get the dates where there are no schedule entries.
The easiest way I thought of is to generate a new table wherein the values in the FinishDate and StartDate will be rearranged in this format (The FinishDate value in the first row, second column will be the value in the first row, first column, while the StartDate value in the second row, first column will be transported to first row, second cloumn, and so on):
Desired Output
Without allocation:
| StartDate | FinishDate |
|Feb 2, 2016 3:37 PM |Feb 2, 2016 4:29 PM |
|Feb 3, 2016 8:16 PM |Feb 3, 2016 8:17 PM |
|Feb 3, 2016 8:34 PM | - |
How can I achieve the desired output? Thank you very much in advance.
MySql doesn't have window functions, so generating this kind of result is always tricky. One possibility is:
SELECT
t1.FinishDate StartDate
, MIN(t2.StartDate) FinishDate
FROM table t1
LEFT JOIN table t2 ON t2.StartDate > t1.FinishDate
GROUP BY 1
This will work well if your date columns are indexed and you have relatively small number of rows to process.

STR_TO_DATE() returns invalid date

I was given a job to create a new table in the database with correct data type. Here are sample records:
RegisteredMonthYear
------------------------
May 2011
March 1998
January 2000
Before I will insert the converted value I tried to convert it using STR_TO_DATE() to check if the values are correct and the result were exactly not I want. This is my query:
SELECT RegisteredMonthYear,
STR_TO_DATE(RegisteredMonthYear, '%M %Y') NewDate,
STR_TO_DATE(CONCAT(RegisteredMonthYear, ' 01'), '%M %Y %d') newDate2,
STR_TO_DATE(RegisteredMonthYear, '%M %Y') + INTERVAL 1 DAY newDate3
FROM TableName
+---------------------+---------------------------------+--------------------------------+----------+
| REGISTEREDMONTHYEAR | NEWDATE | NEWDATE2 | NEWDATE3 |
+---------------------+---------------------------------+--------------------------------+----------+
| May 2011 | April, 30 2011 00:00:00+0000 | May, 01 2011 00:00:00+0000 | (null) |
| March 1998 | February, 28 1998 00:00:00+0000 | March, 01 1998 00:00:00+0000 | (null) |
| January 2000 | December, 31 1999 00:00:00+0000 | January, 01 2000 00:00:00+0000 | (null) |
+---------------------+---------------------------------+--------------------------------+----------+
see here for demo: http://www.sqlfiddle.com/#!2/89a67/7
As you can see, column NEWDATE is one day behind. Why are the result like this?
When I tried to concatenate 01 in the string in column NEWDATE2 the result is as expected. Going back on NEWDATE column, I tried to add one day thinking that it will give exact value in column NEWDATE3 but the result is NULL.
Any idea about this?
You can use following formula (SQLFiddle):
SELECT date(str_to_date(RegisteredMonthYear, '%M %Y'))
+ interval 1 day
FROM tablename
I have added extra DATE() call on top of STR_TO_DATE() - but it makes all the difference.
But in general I agree that this is one more really weird MySQL gotcha.
For example, in PostgreSQL, you don't need to add 1 day and you don't need extra casts, simple to_timestamp is enough:
SELECT to_timestamp('May 2011', 'Mon YYYY');
2013-05-01 00:00:00-07

MySql Daily Report

I am trying to get some help fixing my complex query. I am explaining below my situation, thanks.
I have the following two tables:
ACTIVITY TABLE:
ID USER_ID CARD_ID CLOCK
1 123 04675545 4/3/2013 1:07:06 PM
2 123 04675545 4/3/2013 2:08:06 PM
3 124 04675550 4/3/2013 2:07:06 PM
4 124 04675550 4/3/2013 4:07:06 PM
5 124 04675550 4/4/2013 10:07:06 AM
6 124 04675550 4/4/2013 2:00:00 PM
7 124 04675550 4/5/2013 4:07:06 PM
8 124 04675550 4/7/2013 8:00:00 AM
9 124 04675550 4/7/2013 5:00:00 PM
PRICE TABLE:
ID FROMTIME TOTIME PRICEPERHOUR
1 08:00:00 19:59:59 50.00
2 20:00:00 07:59:59 75.00
And the following query:
select a.user_id, date(a.clock), ABS(TIME_TO_SEC(TIMEDIFF(a.clock, b.clock))/3600)*c.PRICEPERHOUR as total from
(Select if((#rn:=#rn+1)%2=0,#rn,#rn-1) As rId, act.* from act
join (select #rn:=-1)a
order by user_Id, clock) a
inner join
(Select if((#rn1:=#rn1+1)%2=0,#rn1,#rn1-1) As rId, act.* from act
join
(select #rn1:=-1)b
order by user_Id, clock) b
ON a.rid=b.rid AND a.id <> b.id
inner join
price c
on
TIME_TO_SEC(a.clock) between TIME_TO_SEC(c.FROMTIME)
AND
TIME_TO_SEC(c.TOTIME)
group by a.user_id, date(a.clock)
And I am getting the following result:
USER_ID DATE(A.CLOCK) TOTAL
123 April, 03 2013 00:00:00+0000 50.8333
124 April, 03 2013 00:00:00+0000 100
124 April, 04 2013 00:00:00+0000 194.0833
124 April, 05 2013 00:00:00+0000 1,994.0833
124 April, 07 2013 00:00:00+0000 1,994.0833
However, I am trying to get this result instead:
USER_ID DATE(A.CLOCK) TOTAL
123 April, 03 2013 00:00:00+0000 50.8333
124 April, 03 2013 00:00:00+0000 100
124 April, 04 2013 00:00:00+0000 194.0833
124 April, 05 2013 00:00:00+0000 50
124 April, 07 2013 00:00:00+0000 450
This is part of a clock system. Each time the user check-in, one entry gets recorded on the database. A correct user behavior will be that it has always a pair record recorded. For example user_id 123 clocks at 1:07:06pm and clocks again at 2:08:06pm. However, in some situations, the user may do it just once (unpaired record on the database) and therefore it should only be charged that particular hour from the record. As an example, user 124 on day 4/5/2013.
I am trying all weekend to get this query working :(. Once I get the correct result, I will add a condition to get only one user_id also, (e.g. where user_id=124).
I think even if you manage to do this there are are some potential design pitfalls:
Can people clock in for more than 1 period per day? If so then 2 records for example
10am and 2pm could total 2hours or 4hours.
What happens if people clock in at 11pm and again at 2am?
From a quick glance I don't think your sql takes into account time periods that span across the 2 different pay rates? You should definitely include this scenario in your test data.
If I was going to implement this I would probably move the logic into code, and simplify the price table by only having one time column like:
TIME, PRICE
00:00, 75.00
08:00, 50.00
20:00, 75.00
Also if a user only has one card you may not need to have card_id and user_id in the activity table.

Get data from table where date between given date and 1 week back

I have database dbadmin, table - tbl_empreimburse with fields-emp_id,rem_amount,rem_date.
I want to retrieve data which comes from given date to a week back.
I tried this query,
SELECT SUM(rem_amount),DATEADD(dd, -7, "2012-01-10")
FROM tbl_empreimburse
GROUP BY emp_id
HAVING emp_id='5' AND rem_date BETWEEN DATEADD(dd, -7, "2012-01-10") AND "2012-01-10"
It gives me error "FUNCTION dbadmin.DATEADD does not exist". Do I need to convert "2012-01-10" to date format? Any Help, Please?
Try this:
This query gives result as you have specified for employee id 5 and date period of 7 days.
SELECT emp_id, SUM(rem_amount)
FROM tbl_empreimburse
WHERE emp_id='5' AND DATEDIFF('2012-12-31', rem_date) BETWEEN 0 AND 7;
OR
Below query gives you all employee data.
SELECT emp_id, SUM(rem_amount)
FROM tbl_empreimburse
GROUP BY emp_id
HAVING DATEDIFF('2012-12-31', rem_date) BETWEEN 0 AND 7;
Check this *SQLFIDDLE reference out. :)
I am not sure why you are using group by clause here...
Sample date:
ID AMOUNT RDATE
1 3400 January, 01 2012 00:00:00+0000
2 5000 January, 10 2012 00:00:00+0000
3 3000 January, 02 2012 00:00:00+0000
5 1000 January, 05 2012 00:00:00+0000
5 2000 January, 04 2012 00:00:00+0000
2 2000 February, 10 2012 00:00:00+0000
Query:
select * from emp
where id = 5;
here is the query to get sum
select id, sum(amount)
from emp
where rdate between '2012-01-10' - interval 7 day
and '2012-01-10'
and id = 5
;
Results:
all records by employee id = 5
ID AMOUNT RDATE
5 1000 January, 05 2012 00:00:00+0000
5 2000 January, 04 2012 00:00:00+0000
sum of amount by employee id = 5
ID SUM(AMOUNT)
5 3000

SSRS: Pivoting columns

I've got a list of raw data which is passed to SSRS from a stored procedure. I have a matrix which then pivots the data.
For example:
Raw data
WeekNumber Date
1 Mon 10th Dec
1 Tue 11th Dec
1 Wed 12th Dec
2 Mon 17th Dec
When pivoted, it becomes the following for the column names
Mon 10th Dec | Tue 11th Dec | Wed 12th Dec | Mon 17th Dec
Is it possible to have a pivot with a where condition? In this example say,
I'd want it to look like
Mon 10th Dec | Tue 11th Dec | Wed 12th Dec
and then another column with Mon 17th Dec since the WeekNumber is 2
I'm not sure I understand your question. But anyway, perhaps you could consider doing the pivot in your stored procedure as per :
http://msdn.microsoft.com/en-us/library/ms177410(v=sql.105).aspx
My secret to success when using reporting tools is to solve complex problems at the data level, rather than trying to get the reporting tool to do it.
Yes, this is not difficult.
What you are calling a pivot in SSRS is really just a column group. You can add either a filter or a parent group to your column group to filter out WeekNumber <> 2 or group above by WeekNumber. With a parent group you could get results like:
WeekNum: 1 | Total for week | |WeekNum: 2 | Total for week |
Mon 10th Dec | Tue 11th Dec | Wed 12th Dec | | |Mon 17th Dec
20 | 25 | 10 | 55 | | 15 | 15