hi i am edited my question
there is 4 table
team
id | teamname |
1 | lal
2 | sobuj
member
id | membername| team |
1 | sagor| 1 |
2 | sumon| 1 |
dps
id | member| team | deposit | refund | ddate
1 | 1 | 1 | 100 | 12 | 2016-09-01 |
2 | 2 | 1 | 120 | 34 | 2016-09-01 |
3 | 1 | 1 | 130 | 22 | 2016-09-01 |
4 | 3 | 1 | 120 | 0 | 2016-09-01 |
and
mvs
id | member| team | mvs_total| refund | ddate
1 | 1 | 1 | 100 | 12 | 2016-09-01 |
2 | 2 | 1 | 120 | 34 | 2016-09-01 |
3 | 1 | 1 | 130 | 22 | 2016-09-01 |
4 | 3 | 1 | 120 | 0 | 2016-09-01 |
here is my query
$result=mysql_query(select * from member where team='1');
$row=mysql_fetch_array($result);
do{
$aresult=mysql_query(select sum(deposit)-sum(refund)as balance from dps where member='$row[id]');
$arow=mysql_fetch_array($aresult);
echo $arow[balance];
$bresult=mysql_query(select sum(mvs_total)as mvs_balance from mvs where member='$row[id]' and date1<='2016-09-01');
$brow=mysql_fetch_array($bresult);
}while($row=mysql_fetch_array($result));
its taking too much time
how to avoid sub query or how can i save time
advice plz
Thanks in advance
Try this,
select sum(deposit)-sum(refund) from dps JOIN member ON (dps. member = member.id) WHERE team='1';
Related
there is 4 table
team
---------------
id | teamname |
1 | lal |
2 | sobuj |
member
-----------------------
id | membername| team |
1 | sagor | 1 |
2 | sumon | 1 |
dps
---------------------------------------------------
id | member| team | deposit | refund | ddate |
1 | 1 | 1 | 100 | 12 | 2016-09-01 |
2 | 2 | 1 | 120 | 34 | 2016-09-01 |
3 | 1 | 1 | 130 | 22 | 2016-09-01 |
4 | 3 | 1 | 120 | 0 | 2016-09-01 |
mvs
----------------------------------------------------
id | member| team | mvs_total| refund | ddate |
1 | 1 | 1 | 100 | 12 | 2016-09-01 |
2 | 2 | 1 | 120 | 34 | 2016-09-01 |
3 | 1 | 1 | 130 | 22 | 2016-09-01 |
4 | 3 | 1 | 120 | 0 | 2016-09-01 |
here is my query:
$result=mysql_query(select * from member where team='1');
$row=mysql_fetch_array($result);
do{
$aresult=mysql_query(select sum(deposit)-sum(refund)as balance from dps where member='$row[id]');
$arow=mysql_fetch_array($aresult);
echo $arow[balance];
$bresult=mysql_query(select sum(mvs_total)as mvs_balance from mvs where member='$row[id]' and date1<='2016-09-01');
$brow=mysql_fetch_array($bresult);
echo $brow[mvs_total];
} while($row=mysql_fetch_array($result));
its taking too much time how to avoid sub query or how can i save time advice plz Thanks in advance.
First thing I notice in your table structure is that repetition of column.
E.g. team column not require in dps and mcs table, because from member column [connect to member table] you can easily find dps or mvs is under which team.
For your current problem you can use following query.
SELECT member.id,
member.membername,
Sum(dps.deposit) - Sum(dps.refund) AS balance,
Sum(mvs.mvs_total) AS mvs_balance,
Sum(loan.deposit) - Sum(loan.withdraw) AS balance,
Sum(loan.deposit2) - Sum(loan.withdraw2) AS balance2,
Sum(loan.total) - Sum(loan.lpay) AS outsstanding
FROM member AS member
LEFT JOIN dps AS dps
ON dps.member = member.id
LEFT JOIN mvs AS mvs
ON mvs.member = member.id
LEFT JOIN loan AS loan
ON loan.member = member.id
WHERE member.team = 1
AND mvs.ddate <= '2016-09-01'
-- AND loan.team = '10'
-- AND loan.cs = 'Core'
GROUP BY member.id
I am looking to set start time for first 20 students of a particular subject
at 9:00 AM and end time 10:00 AM, for the next 20 i want the time for the same subject 10:00 AM- 11 AM , this should happen for each subject, also i want that only 1 teacher for that particular subject should be alloted for each time slot,
i have tried a lot but still unable to come up with the solution.
user_table
user_id | user_name | contact_no | password | flag
6 | Abhis | 123456788 | 123 | s
5 | Abhish | 123456789 | 123 | s
8 | Sneha | 1111111111 | 123 | s
7 | Snehil | 1111112222 | 123 | s
1 | Narsingh | 1234567890 | 123 | t
2 | Abhinav | 1234567891 | 1234 | t
3 | Abhi | 1234567892 | 123 | s
4 | Abhishek | 1234567893 | 123 | s
subject_table
sub_id | sub_name
3 | CSS
1 | HTML
2 | JQUERY
enr_id | sub_id | user_id | date |
1 | 1 | 1 | 2016-04-01 |
2 | 2 | 1 | 2016-04-01 |
3 | 3 | 1 | 2016-04-01 |
4 | 1 | 5 | 2016-04-01 |
5 | 1 | 6 | 2016-04-01 |
6 | 1 | 7 | 2016-04-01 |
7 | 1 | 2 | 2016-04-01 |
8 | 2 | 2 | 2016-04-01 |
i have 3 tables. 2 reference table and one table transactions.
Table puskesmas
mysql> select id,nama from puskesmas;
+----+----------------------+
| id | nama |
+----+----------------------+
| 1 | BAMBANGLIPURO |
| 2 | BANGUNTAPAN |
| 3 | BANTUL |
| 4 | DLINGO |
| 5 | IMOGIRI |
| 6 | JETIS |
| 7 | KASIHAN |
| 8 | KRETEK |
| 9 | PAJANGAN |
| 10 | PANDAK |
+----+----------------------+
Table poli
mysql> select * from poli;
+----+---------------+
| id | nama |
+----+---------------+
| 1 | BP |
| 2 | KIA |
| 3 | GIGI |
| 4 | JIWA |
+----+---------------+
Table loket
mysql> select pasien_id,poli_id,puskesmas_id from loket limit 10;
+-----------+---------+--------------+
| pasien_id | poli_id | puskesmas_id |
+-----------+---------+--------------+
| 1 | 1 | 1 |
| 5 | 2 | 2 |
| 1 | 1 | 3 |
| 9 | 3 | 4 |
| 1 | 3 | 5 |
| 5 | 2 | 1 |
| 1 | 1 | 3 |
| 8 | 1 | 2 |
| 10 | 3 | 6 |
| 5 | 2 | 5 |
+-----------+---------+--------------+
I want to show the total number (count) of visits to puskesmas table. then made field all_total
fter that I want to take patient visits where poli_id = 1 and used as a column bp. for other poly are the same as the
+----+----------------------+--------------+------+------+------+------+
| id | nama | All Total | BP | KIA | GIGI | JIWA |
+----+----------------------+--------------+------+------+------+------+
| 1 | BAMBANGLIPURO | 30 | 10 | 3 | 15 | 2 |
| 2 | BANGUNTAPAN | 35 | 20 | 4 | 11 | 0 |
| 3 | BANTUL | 15 | 10 | 0 | 5 | 0 |
| 4 | DLINGO | 12 | 10 | 1 | 1 | 0 |
| 5 | IMOGIRI | 10 | 5 | 2 | 1 | 2 |
| 6 | JETIS | 25 | 13 | 3 | 7 | 2 |
| 7 | KASIHAN | 20 | 10 | 10 | 0 | 0 |
| 8 | KRETEK | 23 | 20 | 1 | 1 | 1 |
| 9 | PAJANGAN | 10 | 5 | 1 | 3 | 1 |
| 10 | PANDAK | 0 | 0 | 0 | 0 | 0 |
+----+----------------------+--------------+------+------+------+------+
I've tried to make a query to display like this but it always fails to hold. Can anyone help to make such queries desired results
i have tried query like this :
select puskesmas_id,
sum(case when poli_id=1 then 1 else 0 end) bp,
sum(case when poli_id=5 then 0 else 1 end) gigi,
count(id) total
from loket
group by puskesmas_id
and result like this :
+--------------+------+------+-------+
| puskesmas_id | bp | gigi | total |
+--------------+------+------+-------+
| 1 | 97 | 126 | 143 |
| 6 | 74 | 185 | 210 |
| 8 | 131 | 179 | 190 |
| 7 | 90 | 92 | 98 |
| 2 | 33 | 39 | 54 |
| 4 | 158 | 248 | 263 |
| 3 | 66 | 68 | 72 |
+--------------+------+------+-------+
but puskesmas_id the value 0 does not perform data when querying
I have a table jobs with following data
+----+------------+--------+---------------------+
| id | project_id | status | modified |
+----+------------+--------+---------------------+
| 1 | 1 | 0 | 2014-08-23 19:48:48 |
| 2 | 1 | 0 | 2014-08-29 18:18:39 |
| 3 | 1 | 0 | 2014-08-23 19:58:55 |
| 44 | 1 | 0 | 2014-08-23 19:45:50 |
| 45 | 1 | 2 | 2014-08-03 14:38:35 |
| 46 | 1 | 3 | 2014-08-05 14:38:35 |
| 47 | 1 | 2 | 2014-08-07 14:38:35 |
| 48 | 1 | 3 | 2014-08-10 14:38:35 |
| 49 | 2 | 4 | 2014-08-14 14:38:35 |
| 50 | 1 | 0 | 2014-08-23 19:43:01 |
| 51 | 1 | 2 | 2014-08-15 14:38:35 |
| 52 | 1 | 3 | 2014-08-18 14:38:35 |
| 53 | 1 | 0 | 2014-08-23 19:49:05 |
| 54 | 1 | 4 | 2014-08-22 14:38:35 |
| 55 | 1 | 2 | 2014-09-03 14:38:59 |
| 56 | 1 | 0 | 2014-08-23 19:59:23 |
| 57 | 1 | 3 | 2014-08-27 14:38:35 |
| 58 | 1 | 4 | 2014-09-03 14:39:04 |
| 59 | 1 | 0 | 2014-08-27 17:28:45 |
| 60 | 1 | 1 | 2014-08-27 17:29:00 |
| 61 | 1 | 1 | 2014-08-27 17:29:17 |
| 62 | 1 | 0 | 2014-08-29 16:36:07 |
| 63 | 1 | 0 | 2014-08-29 16:37:24 |
| 64 | 1 | 0 | 2014-08-29 18:19:01 |
| 65 | 1 | 0 | 2014-08-29 19:17:20 |
| 66 | 1 | 1 | 2014-08-29 19:17:36 |
| 67 | 1 | 1 | 2014-08-29 19:17:54 |
| 68 | 1 | 0 | 2014-08-29 19:26:16 |
| 69 | 1 | 1 | 2014-08-29 19:26:34 |
| 70 | 1 | 1 | 2014-08-29 19:26:54 |
| 71 | 1 | 0 | 2014-08-30 19:33:59 |
| 72 | 1 | 1 | 2014-08-30 19:34:23 |
| 73 | 1 | 1 | 2014-08-30 19:35:42 |
| 74 | 1 | 1 | 2014-08-30 19:36:39 |
| 75 | 1 | 0 | 2014-08-30 19:40:52 |
| 76 | 1 | 0 | 2014-08-30 20:42:02 |
| 77 | 1 | 0 | 2014-08-30 21:27:38 |
| 78 | 1 | 1 | 2014-08-30 21:28:28 |
| 79 | 1 | 1 | 2014-08-30 21:28:59 |
| 80 | 1 | 0 | 2014-08-30 21:47:02 |
| 81 | 1 | 1 | 2014-08-30 21:47:40 |
| 82 | 1 | 1 | 2014-08-30 21:48:20 |
| 83 | 2 | 3 | 2014-08-23 18:23:12 |
| 84 | 2 | 4 | 2014-08-23 18:23:12 |
| 85 | 1 | 4 | 2014-08-23 18:23:12 |
+----+------------+--------+---------------------+
from which i want to get the data on Weekly basis according to the "from" and "to" dates i pass from front end. so lets suppose want data from '2014-07-15' to '2014-09-15' that should be clubbed weekly and gives sum of all jobs whose status is either (2,3,4) on a particular week for each project.
my result should be something like this
+--------------+---------------+---------------------+
| project_name | reviewed_jobs | modified |
+--------------+---------------+---------------------+
| Project 1 | 0 | 2014-07-15 14:38:35 |
| Project 1 | 0 | 2014-07-22 14:38:35 |
| Project 1 | 1 | 2014-07-29 14:38:35 |
| Project 1 | 3 | 2014-08-05 14:38:35 |
| Project 1 | 2 | 2014-08-12 14:38:35 |
| Project 1 | 2 | 2014-08-19 18:23:12 |
| Project 1 | 1 | 2014-08-26 19:48:48 |
| Project 1 | 2 | 2014-09-02 18:18:39 |
| Project 1 | 0 | 2014-09-09 14:38:59 |
| Project 2 | 0 | 2014-07-15 14:38:35 |
| Project 2 | 0 | 2014-07-22 14:38:35 |
| Project 2 | 0 | 2014-07-29 14:38:35 |
| Project 2 | 0 | 2014-08-05 14:38:35 |
| Project 2 | 1 | 2014-08-12 14:38:35 |
| Project 2 | 2 | 2014-08-19 18:23:12 |
| Project 2 | 0 | 2014-08-26 19:48:48 |
| Project 2 | 0 | 2014-09-02 18:18:39 |
| Project 2 | 0 | 2014-09-09 14:38:59 |
+--------------+---------------+---------------------+
what i have tried so far using raw query and cake php way :
raw query :
SELECT projects.name as project_name,
SUM(if((jobs.status=2 || jobs.status=3 || jobs.status=4), 1, 0)) as reviewed_jobs,
jobs.modified FROM jobs LEFT JOIN projects ON jobs.project_id=projects.id
WHERE jobs.modified >= '2014:08:03 00:00:00' AND jobs.modified <= '2014:09:03 23:59:59'
GROUP BY projects.name, WEEK(jobs.modified)
ORDER BY DATE(jobs.modified) ASC;
but it gives the following result
+--------------+---------------+---------------------+
| project_name | reviewed_jobs | modified |
+--------------+---------------+---------------------+
| Project 1 | 3 | 2014-08-03 14:38:35 |
| Project 1 | 2 | 2014-08-10 14:38:35 |
| Project 2 | 1 | 2014-08-14 14:38:35 |
| Project 2 | 2 | 2014-08-23 18:23:12 |
| Project 1 | 3 | 2014-08-23 19:48:48 |
| Project 1 | 1 | 2014-08-29 18:18:39 |
| Project 1 | 2 | 2014-09-03 14:38:59 |
+--------------+---------------+---------------------+
which obviously is undesired and different from what i want and it has some potential problems as
if it doesn't get data of specific date (i.e 2014-08-17 in this case) it skips that week or count from the next date which is available.
result is not according to the dates passed in query indeed it's according to the data in table(dates available in table).
CakePHP way :
$options['joins']=array(array('table' => 'projects',
'alias' => 'Project',
'type' => 'LEFT',
'conditions' => array(
'Project.id = Job.project_id',
)
));
$options['fields'] = array(
'Project.name',
'SUM(if((Job.status=2 || Job.status=3 || Job.status=4), 1, 0)) as reviewed',
'DATE(Job.modified) as modified'
);
$options['order'] = array('modified'=>'ASC');
$options['conditions']=array(
array("Job.modified >=" => $from_date->format("Y-m-d") . ' 00:00:00'),
array("Job.modified <=" => $to_date->format("Y-m-d") . ' 23:59:59')
);
$options['group'] = array('Project.name', 'WEEK(Job.modified)');
Any help would be appreciable
Thanks
In mysql you would need another table containing all the weeks dates so you can join to that too and therefore return the weeks where there are no entries in the jobs table.
Quick and dirty way to construct weeks data:
SELECT STR_TO_DATE(CONCAT(yr.yy,'-',wk1.d,wk2.d,'-01'), '%Y-%u-%w') yyyywkd FROM
(SELECT 0 d UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5) wk1,
(SELECT 0 d UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) wk2,
(SELECT '2013' yy UNION SELECT '2014') yr
WHERE CONCAT(wk1.d,wk2.d) > 0 AND CONCAT(wk1.d,wk2.d) < 54
Try this in a sub query left joined to the jobs table.
I want to concatenate and count data of the same column, so I can concatenate but I can not count the repeated data.
Here's my table of data:
| ID | bills | class |
|-----|-------|-------|
| 1 | 0.5 | 2 |
| 2 | 1 | 1 |
| 3 | 0.5 | 2 |
| 5 | 1 | 3 |
| 6 | 0 | 2 |
| 7 | 0.5 | 1 |
| 8 | 1 | 2 |
| 9 | 1 | 3 |
| 10 | 0.5 | 1 |
| 11 | 0 | 2 |
| 12 | 1 | 1 |
| 13 | 0 | 3 |
| 14 | 1 | 2 |
| 15 | 0 | 1 |
| 16 | 0 | 1 |
| 17 | 0.5 | 3 |
| 18 | 0 | 3 |
| 13 | 0.5 | 3 |
Here's my sql query I'm using to concatenate data:
SELECT class AS lesson,
GROUP_CONCAT( bills ORDER BY bills ) AS bills
FROM tb_presence
GROUP BY class;
Here's my result below:
| class | bills |
|-------|------------------|
| 1 | 1,0.5,0.5,1,0,0 |
| 2 | 0.5,0,1,0,1 |
| 3 | 1,1,0,0.5,0,0.5 |
Now I would like to count the data that are equal, but continue with the same concatenation.
I want to "count" the data with the same values and display concatenated (column observation and only to help understanding)
| class | bills | observation |
|-------|-------|-----------------------------|
| 1 | 2,2,2 | (2=0+0) (2=0.5+0.5) (2=1+1) |
| 2 | 2,1,2 | (2=0+0) (1=0.5) (2=1+1) |
| 3 | 2,2,2 | (2=0+0) (2=0.5+0.5) (2=1+1) |
Is this really possible?
Here is a solution (thanks to #wchiquito for the sqlfiddle) See http://sqlfiddle.com/#!2/2d2c8/1
As you can see it cannot dynamically determine the bills' values and count them. But there is a count per bill value that you want.
SELECT class AS lesson,
GROUP_CONCAT( bills ORDER BY bills ) AS bills
,SUM(IF(bills=0,1,0)) AS Count0
,SUM(IF(bills=0.5,1,0)) AS Count05
,SUM(IF(bills=1,1,0)) AS Count1
,COUNT(*) AS totalRecords
,COUNT(*)
- SUM(IF(bills=0,1,0))
- SUM(IF(bills=0.5,1,0))
- SUM(IF(bills=1,1,0))
AS Missing
FROM tb_presence GROUP BY class;
I added an extra record to show how the 'missing' column could show if you were not taking all values into consideration.
Results
| LESSON | BILLS | COUNT0 | COUNT05 | COUNT1 | TOTALRECORDS | MISSING |
|--------|-----------------------------------------|--------|---------|--------|--------------|---------|
| 1 | 0.00,0.00,0.50,0.50,1.00,1.00,1.00,4.00 | 2 | 2 | 3 | 8 | 1 |
| 2 | 0.00,0.00,0.50,0.50,1.00,1.00 | 2 | 2 | 2 | 6 | 0 |
| 3 | 0.00,0.00,0.50,0.50,1.00,1.00 | 2 | 2 | 2 | 6 | 0 |