Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Count not working

Posted on 2008-06-16
8
Medium Priority
?
396 Views
Last Modified: 2013-12-13
Dear Experts,

select count(*) as numrows
from datatable dt
inner join categorytable ct on ct.mainid = dt.mainid
where ct.categoryid = 10
order by dt.date desc

This gives me 214 rows when I should be getting 142 rows

So I tried adding a group

select count(*) as numrows
from datatable dt
inner join categorytable ct on ct.mainid = dt.mainid
where ct.categoryid = 10
group by dt.mainid
order by dt.date desc

but then it goes completely wrong.

Can someone please help as I need to get the count I get from this section

select count(*) as numrows
from datatable dt
order by dt.date desc

But I need to specify the categoryid which only exists in the categorytable.

please help.

I am using mysql 3.25 I think.
0
Comment
Question by:narmi2
8 Comments
 
LVL 29

Assisted Solution

by:fibo
fibo earned 400 total points
ID: 21793829
select unique count(dt.*) as numrows
from datatable dt
inner join categorytable ct on ct.mainid = dt.mainid
where ct.categoryid = 10
order by dt.date desc
?? why this "order"?

select unique count( dt.mainid) as numrows
from datatable dt
inner join categorytable ct on ct.mainid = dt.mainid
where ct.categoryid = 10
order by dt.date desc

0
 
LVL 3

Expert Comment

by:pzurowski
ID: 21793905
how does look rows which they shoudln't be? Is there any NULLs in ct.mainid or dt.mainid ?
select * as numrows
from datatable dt
inner join categorytable ct on ct.mainid = dt.mainid
where ct.categoryid = 10
order by dt.date desc

Open in new window

0
 
LVL 1

Author Comment

by:narmi2
ID: 21793907
You're right I don't need the order.  However the above script is not working.  I am getting a query error.  How do I check what the error is?
0
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
LVL 29

Expert Comment

by:fibo
ID: 21794009
select unique count( dt.mainid) as numrows
from datatable dt, categorytable ct
where (ct.categoryid = 10) AND (ct.mainid = dt.mainid)
0
 
LVL 1

Author Comment

by:narmi2
ID: 21794010
pzurowski,

No nulls, but there are repeating rows because each main id in datatable has many categoryids in categorytable
0
 
LVL 1

Author Comment

by:narmi2
ID: 21794026
That query does not work fibo,  I think mysql 3.23 does not support the "unique".
0
 
LVL 32

Accepted Solution

by:
awking00 earned 1600 total points
ID: 21794320
Use distinct keyword instead of unique.
0
 
LVL 2

Expert Comment

by:raheel_lips
ID: 21795125
Hey narmi2,

Try following query. I think this will get rid of your problem. I think your query need left join instead of inner join

Regards,
Raheel

select count(*) as numrows
from datatable dt
left join categorytable ct on ct.mainid = dt.mainid
where ct.categoryid = 10
group by dt.mainid

Open in new window

0

Featured Post

Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

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

In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

972 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