Solved

how do I fix this COUNT query?

Posted on 2012-03-16
5
225 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Suggested Solutions

Title # Comments Views Activity
sql_mode 1 34
AWS EC2 & RDS Instance 5 64
How to use 2 ON statements in inner join 3 27
Dynamic Table mySQL stored procedure 5 36
All XML, All the Time; More Fun MySQL Tidbits – Dynamically Generate XML via Stored Procedure in MySQL Extensible Markup Language (XML) and database systems, a marriage we are seeing more and more of.  So the topics of parsing and manipulating XM…
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 …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

749 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