×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Nested Select statement (error output)

Nested Select statement (error output)

Nested Select statement (error output)

(OP)
Hi, im newbie of SQL user

Note: I use in MySQL
I attach sql script called testdb.sql
http://www.filefactory.com/file/cbe7a69/n/testdb.sql

I have 2 table which is product1,product2

Product1
PID ProductName ProductCode
1   Nike        N1
2   Adidas      A2
3   NewBalance  NB1
4   Puma        P3
.... etc

Product2
Date       PID
2009/1/14   1
2009/1/15   1
2009/2/1    2
2009/3/3    3
.........etc

Expected out is

ProductName Jan2009 Feb2009 Mar2009
 Adidas        1       1       0  
 NewBalance   30       0       1
 Nike          2       0       0   
 Puma         20       0       0

But, i tried wrote Nested Select statement query like this, the output is wrong :
select product1.ProductName ,
    (select count(product2.PID)
    from product1 as product1 left join product2 as product2
    on product1.pid=product2.pid
    and product2.pdate BETWEEN '2009/1/1' AND '2009/1/31')
    as 'Jan 2009',
   (select count(product2.PID)as 'Feb 2009'
    from product1 as product1 left join product2 as product2
    on product1.pid=product2.pid
    and product2.pdate BETWEEN '2009/2/1' AND '2009/2/28')as 'Feb 2009',
    (select count(product2.PID)
    from product1 as product1 left join product2 as product2
    on product1.pid=product2.pid
    and product2.pdate BETWEEN '2009/3/1' AND '2009/3/31') as 'Mar 2009'
    from product1 Group by product1.productName;



My output looks like this :(
ProductName Jan2009 Feb2009 Mar2009
 Nike         53      1       1  
 Adidas       53      1       1
 NewBalance   53      1       1   
 Puma         53      1       1


any solution ?

thanks
Regards
s1

RE: Nested Select statement (error output)

what about this ?

CODE

SELECT O.ProductName
, (SELECT COUNT(P2.PID) FROM product1 P1 LEFT JOIN product2 P2
    ON P1.PID=P2.PID AND P2.pdate BETWEEN '2009/1/1' AND '2009/1/31'
    WHERE P1.PID=O.PID) AS 'Jan 2009'
, (SELECT COUNT(P2.PID) FROM product1 P1 LEFT JOIN product2 P2
    ON P1.PID=P2.PID AND P2.pdate BETWEEN '2009/2/1' AND '2009/2/28'
    WHERE P1.PID=O.PID) AS 'Feb 2009'
, (SELECT COUNT(P2.PID) FROM product1 P1 LEFT JOIN product2 P2
    ON P1.PID=P2.PID AND P2.pdate BETWEEN '2009/3/1' AND '2009/3/31'
    WHERE P1.PID=O.PID) AS 'Mar 2009'
FROM product1 O

Hope This Helps, PH.
FAQ219-2884: How Do I Get Great Answers To my Tek-Tips Questions?
FAQ181-2886: How can I maximize my chances of getting an answer?

RE: Nested Select statement (error output)

(OP)
Hi, PH

Thanks you so much for the help.
Its works :D


thanks
regards
s1

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close