Solved

count with join statement...

Posted on 2014-12-14
6
132 Views
Last Modified: 2014-12-17
I have two tables as follows:
Table-01
id               Date
1001      11/1/2014
1002      12/1/2014
1003      11/12/2014
1004      11/28/2014
1005      11/14/2014
1006      11/29/2014
1007      11/14/2014
1008      12/10/2014
1009      12/12/2014
1010      12/12/2014
1011      12/8/2014

Table-02
id             status
1001      Started
1002      Started
1003      Started
1004      In Progress
1005      Complete
1006      Complete
1007      Complete
1007      Approved
1008      Approved
1009      Approved
1010      Complete
1011      Complete

I need to get an output that will be give me the count status depending on the date from table-01.

For eg, If I need the count of status for date >= 12/12/2014, I am looking for an output as follows:

Approve  1
Completed 1
Started  null
in Progress null

Can someone let me know how this can be done, thank you.

A
0
Comment
Question by:aej1973
  • 3
  • 3
6 Comments
 
LVL 7

Expert Comment

by:Robert Sherman
ID: 40499414
This will get you the ones that have values... it doesn't return the ones that are null.

SELECT Table2.status, Count(Table2.status) AS CountOfstatus
FROM Table1 INNER JOIN Table2 ON Table1.id = Table2.id
WHERE (((Table1.when)>=#12/12/2014#))
GROUP BY Table2.status;

Open in new window

0
 

Author Comment

by:aej1973
ID: 40499440
Is there a ".when" clause in MySQL also, what is this # symbol? I tried the following statement but it did not work:

SELECT table2.status, Count(table2.status) AS CountOfstatus
FROM table1 INNER JOIN table2 ON table1.id = table2.id
WHERE ((table1.when)>=#12/12/2014#)
GROUP BY table2.status

The error is :
#1064 - 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 'GROUP BY table2.status LIMIT 0, 30' at line 4
0
 
LVL 7

Accepted Solution

by:
Robert Sherman earned 500 total points
ID: 40499520
So sorry, I was being called away and posted that in haste, I forgot this was a MySQL question...  Here is an updated version that follows the naming used in your example and should run under MySQL:  

SELECT Table-02.status, Count(Table-02.status) AS CountOfstatus
FROM Table-01 INNER JOIN Table-02 ON Table-01.id = Table-02.id
WHERE (((Table-01.date)>='2014-12-12'))
GROUP BY Table02-status;

Open in new window

0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 7

Expert Comment

by:Robert Sherman
ID: 40499550
Also, if you need a query that returns null for those values in status that have no records within the date range you specify, it gets a little more complicated.  

Do you have the values for status stored in a separate table?  (one with four rows containing 'Approved' 'Started' 'In Progress' 'Complete')   If you do, you could use a left join from that table to a sub-query containing the query I posted above.   For example, if the lookup table is called 'status_tbl':

Select a.status, b.CountOfstatus from status_tbl as a LEFT JOIN 
      (
       SELECT Table2.status, Count(Table2.status) AS CountOfstatus
           FROM Table1 INNER JOIN Table2 ON Table1.id = Table2.id
           WHERE (((Table1.when)>=#12/12/2014#))
           GROUP BY Table2.status;
      )  AS b ON a.status = b.status;

Open in new window

0
 

Author Comment

by:aej1973
ID: 40503253
Sherman, looks like for the second query you have not done it with MySQL :-). Thank you very much for your help. I am taking a look at this.

A
0
 

Author Comment

by:aej1973
ID: 40504403
Sherman, thank you. I was able to figure it out with the guidance you gave me.  Thanks again for the help.

A
0

Featured Post

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Does PHPMyAdmin pose a security risk? 2 93
MySQL backend - what should be front end? 8 53
Mysql not caching queries 4 63
Php  POSTGRES  "  IN SIDE "  " 4 59
Fore-Foreword Today (2016) Maxmind has a new approach to the distribution of its data sets.  This article may be obsolete.  Instead of using the examples here, have a look at the MaxMind API (https://www.maxmind.com/en/geolite2-developer-package). …
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 …
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

910 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now