Solved

mysql sum total amout

Posted on 2011-03-13
15
330 Views
Last Modified: 2012-05-11
table member have a column staff1, and staff2 that take care for customer how can i can count the amount of people that  each staff take care
CREATE TABLE IF NOT EXISTS `member` (
  `idmember` int(11) NOT NULL AUTO_INCREMENT,
  `own` varchar(255) DEFAULT NULL,
  `Sname` varchar(255) DEFAULT NULL,
  `Ssurname` varchar(255) DEFAULT NULL,
  `Sex` enum('¿','¿') NOT NULL,
  `Hno` varchar(255) DEFAULT NULL,
  `Moo` varchar(255) DEFAULT NULL,
  `tambol` enum('¿¿¿¿¿¿¿¿¿','¿¿¿¿¿¿¿') NOT NULL,
  `dateofbirth` date DEFAULT NULL,
  `migratedate` date DEFAULT NULL,
  `status` enum('5','4','3','2','1') DEFAULT '5',
  `Unit` int(4) DEFAULT NULL,
  `staff1` int(11) DEFAULT NULL,
  `staff2` int(11) DEFAULT NULL,
  PRIMARY KEY (`idmember`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=5186 ;




CREATE TABLE IF NOT EXISTS `staff` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) DEFAULT NULL,
  `surname` varchar(255) DEFAULT NULL,
  `remark` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;

Open in new window

0
Comment
Question by:teera
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 3
  • +1
15 Comments
 
LVL 5

Expert Comment

by:mayankagarwal
ID: 35125547
select a.id, a.name, a.surname, (select count(b.*) from member b where member where b.staff1 = a.id or b.staff2 = a.id) as count_customer from staff a
0
 

Author Comment

by:teera
ID: 35125559
Hi mayankagarwal
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '*) from member b where member where b.staff1 = a.id or b.staff2 = a.id) as count' at line 1
0
 
LVL 5

Expert Comment

by:mayankagarwal
ID: 35125561
select a.id, a.name, a.surname, (select count(b.idmember) from member b where b.staff1 = a.id or b.staff2 = a.id) as count_customer from staff a
0
Business Impact of IT Communications

What are the business impacts of how well businesses communicate during an IT incident? Targeting, speed, and transparency all matter. Find out more in this infographic.

 
LVL 19

Expert Comment

by:Bhavesh Shah
ID: 35125605

SELECT A.id, A.name, A.surname, Count(B.idmember)CustomerCount
FROM Staff A LEFT OUTER JOIN member B
ON B.staff1 = A.id
OR B.staff2 = A.id
0
 
LVL 5

Expert Comment

by:mayankagarwal
ID: 35125616
SELECT A.id, A.name, A.surname, Count(B.idmember) as CustomerCount
FROM Staff A LEFT OUTER JOIN member B
ON (B.staff1 = A.id
OR B.staff2 = A.id)
0
 
LVL 2

Expert Comment

by:sihar86
ID: 35125749
what is the difference staff1 and staff2?
0
 
LVL 5

Expert Comment

by:mayankagarwal
ID: 35125757
according to me there can be two staff member catering to the same customer
0
 
LVL 2

Expert Comment

by:sihar86
ID: 35125860
Thanks for explanation mayankagarwal

Alternative way

SELECT * FROM
(
SELECT a.staff1 staff, MAX(b.name) NAME, MAX(b.surname) surname, COUNT(1) total FROM member a, staff b
WHERE a.staff1=b.id
GROUP BY a.staff1
UNION
SELECT a.staff2 staff, MAX(b.name) NAME, MAX(b.surname) surname, COUNT(1) total FROM member a, staff b
WHERE a.staff2=b.id
GROUP BY a.staff2
)

Open in new window

0
 

Author Comment

by:teera
ID: 35125914
Hi sihar86
1248 - Every derived table must have its own alias
0
 
LVL 5

Expert Comment

by:mayankagarwal
ID: 35125922
the query i have given is not working??
0
 

Author Comment

by:teera
ID: 35125935
hi experts

Every member have staff to take care  member a was take care by staff1 and incase staff1 can not take care staff2 will take care the
   i want to see how many member that staff take care if it to much so will consider to look fore anoter staff to take cate

                     staff1   staff2
david               20       5
robert              19       6
Susan              18       4
0
 
LVL 5

Accepted Solution

by:
mayankagarwal earned 350 total points
ID: 35125960
select a.id, a.name, a.surname, (select count(b.idmember) from member b where b.staff1 = a.id)  as count_staff1, (select count(c.idmember) from member c where c.staff2 = a.id)  as count_staff2 from staff a
0
 
LVL 19

Expert Comment

by:Bhavesh Shah
ID: 35126035
Hi,

try this

- Bhavesh
SELECT A.idmember,A.Sname,
		Count(CASE WHEN A.staff1 = B.id THEN 1 ELSE 0 END) AS Staff1Count,
		Count(CASE WHEN A.staff2 = B.id THEN 1 ELSE 0 END) AS Staff2Count
FROM member A LEFT OUTER JOIN staff B
ON A.staff1 = B.id 
OR A.staff2 = B.id
Group By 
	A.idmember,A.Sname

Open in new window

0
 
LVL 2

Expert Comment

by:sihar86
ID: 35126987
I forget the table name
for the result staff and amount of member
SELECT * FROM
(
SELECT a.staff1 staff, MAX(b.name) NAME, MAX(b.surname) surname, COUNT(1) total FROM member a, staff b
WHERE a.staff1=b.id
GROUP BY a.staff1
UNION
SELECT a.staff2 staff, MAX(b.name) NAME, MAX(b.surname) surname, COUNT(1) total FROM member a, staff b
WHERE a.staff2=b.id
GROUP BY a.staff2
)

Open in new window


for the result member and amount of staff1 and staff2
SELECT a.name, 
(SELECT COUNT(1) FROM tes b WHERE b.name=a.name AND staff1 <> '') staff1,
(SELECT COUNT(1) FROM tes c WHERE c.name=a.name AND staff2 <> '') staff2
FROM tes a
GROUP BY a.name

Open in new window

0
 
LVL 2

Assisted Solution

by:sihar86
sihar86 earned 150 total points
ID: 35126999
Sorry, I correct my answer before
I forget the table name
for the result staff and amount of member
SELECT * FROM
(
SELECT a.staff1 staff, MAX(b.name) NAME, MAX(b.surname) surname, COUNT(1) total FROM member a, staff b
WHERE a.staff1=b.id
GROUP BY a.staff1
UNION
SELECT a.staff2 staff, MAX(b.name) NAME, MAX(b.surname) surname, COUNT(1) total FROM member a, staff b
WHERE a.staff2=b.id
GROUP BY a.staff2
) x 

Open in new window

for the result member and amount of staff1 and staff2
SELECT a.name, 
(SELECT COUNT(1) FROM tes b WHERE b.name=a.name AND staff1 <> '') staff1,
(SELECT COUNT(1) FROM tes c WHERE c.name=a.name AND staff2 <> '') staff2
FROM tes a
GROUP BY a.name

Open in new window

0

Featured Post

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question