Solved

how do I fix this COUNT query?

Posted on 2012-03-16
5
227 Views
Last Modified: 2012-04-11
I have four tables:

shops (shop,shop_id), products(product_id,date), sex(sex,product_id), users(user_id,prior_login)

shops.shop and shops.shop_id values are unique

I wish to COUNT the number of distinct shops for which at least one row in the products table exists such that shops.shop_id=products.shop_id, sex.product_id=products.product_id, products.date >= users.prior_login, users.user_id=22, and sex.sex=0.

Right now I am trying:

SELECT COUNT (shop_id) AS total_shops FROM shops
WHERE EXISTS (
SELECT * FROM
products, sex WHERE
shops.shop_id=products.shop_id AND
sex.product_id=products.product_id AND
sex.sex=0 AND
(products.date >= (SELECT
prior_login FROM
users WHERE
user_id = 22)))

Open in new window


This produces a syntax error. What am I doing wrong?
0
Comment
Question by:bitt3n
[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
  • 3
  • 2
5 Comments
 
LVL 9

Expert Comment

by:macarrillo1
ID: 37730701
Your not joining your tables correctly. But to provide the correct syntax, I need to know are you using MySQL or SQL Server?
0
 
LVL 9

Expert Comment

by:macarrillo1
ID: 37730777
If you have SQL Server you can try:


SELECT COUNT (shop_id) AS total_shops
FROM shops
WHERE EXISTS(
SELECT *
FROM shops s inner join products p
on s.shop_id=p.shop_id
inner join sex x
on p.product_id=x.product_id
WHERE
x.sex=0 AND
(p.date >= (SELECT
prior_login FROM
users WHERE
user_id = 22)  ) )
0
 

Author Comment

by:bitt3n
ID: 37730935
I am using MySQL. Looking at your query, I'm confused why it is not necessary to correlate 'shops s' in the EXISTS query with 'shops' in the outer query. (I tried using your query in MySQL but presumably there is some syntax difference between SQL Server and MySQL that prevents it from working.)
0
 

Author Comment

by:bitt3n
ID: 37731009
it appears the problem was merely the space in 'COUNT (shop_id)', as when I remove this space, I get the expected result.

I'll leave this question open in case anyone wishes to suggest some other correction or improvement, but I assume the problem was just this stupid syntax mistake.
0
 
LVL 9

Accepted Solution

by:
macarrillo1 earned 500 total points
ID: 37731012
Actually Shops is in the Exists query.  I am using an alias of 's'.  So instead of saying:
shops.shops_id
I can say
s.shops_id

Yes the syntax is different for MySLQ than for SQL Server.
 
Try:

SELECT COUNT (shop_id) AS total_shops
FROM shops
WHERE EXISTS (
SELECT *
FROM Products
Join Sex on  products.product_id=sex.product_id
Join Shops on products.shop_id=shops.shop_id
WHERE
sex.sex=0 AND
products.date >= (SELECT
prior_login FROM
users WHERE
user_id = 22)  
);
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Foreword This article was written many years ago, in the days when PHP supported the MySQL extension (http://php.net/manual/en/function.mysql-connect.php).  Today (http://php.net/manual/en/migration70.removed-exts-sapis.php) you would not use MySQL…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

729 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