Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Database > SQL Language
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old September 14th, 2007, 01:30 AM
Authorized User
 
Join Date: Oct 2006
Location: Kolkata, West Bengal, India.
Posts: 63
Thanks: 0
Thanked 0 Times in 0 Posts
Default Please provide solution

Hi Guys,
          I have a problem regarding one query,please solve this as soon as possible....because it's very urgent.

I have following datas in a table:

Company Product Status
======= ======== ======
ABC Shirt 0
XYZ Jeans 0
ABC T-Shirt 1
ABC Rice 0
XYZ Sugar 1

Here Status 0 means INSTOCK and 1 means OUTSTOCK...
Now I have to show the following result-

Company Total INSTOCK OUTSTOCK
======= ======= ========= =========
ABC 3 2 1
XYZ 2 1 1

Please solve this.Thanks in advance
Prasanta

Reply With Quote
  #2 (permalink)  
Old September 14th, 2007, 07:24 AM
Friend of Wrox
Points: 7,395, Level: 36
Points: 7,395, Level: 36 Points: 7,395, Level: 36 Points: 7,395, Level: 36
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Capital Federal, , Argentina.
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

hi there.. you are not being very polite.. instead of say please solve this, you should say can you please help me solving this?? we are not here to solve things, we are here to help you understand how things works, so in the future you can solve it by yourself...

anyway this query could be something like this
Code:
SELECT DISTINCT zzz_1.company, tabla2.total, derivedtbl_1.INSTOCK, TABLA3.OUTSTock
FROM         (SELECT     company, COUNT(Status) AS INSTOCK
                       FROM          zzz
                       GROUP BY company, Status
                       HAVING      (Status = 0)) AS derivedtbl_1 INNER JOIN
                      zzz AS zzz_1 ON derivedtbl_1.company = zzz_1.company INNER JOIN
                          (SELECT     company, COUNT(company) AS total
                            FROM          zzz AS zzz_2
                            GROUP BY company) AS tabla2 ON zzz_1.company = tabla2.company INNER JOIN
                          (SELECT     company, COUNT(Status) AS OUTSTock
                            FROM          zzz AS zzz_3
                            GROUP BY company, Status
                            HAVING      (Status = 1)) AS TABLA3 ON zzz_1.company = TABLA3.company
this code works on SQL Server.. for oracle could be a little diferent...
the main table name is zzz, all the rest of the alias could be anything you want...
and the name of the company shouldn't be repeated in your main table, you should have there a numeric key and the name should be in another table....

HTH

Gonzalo

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from dparsons signature and he Took that from planoie's profile
================================================== =========
My programs achieved a new certification (can you say the same?):
WORKS ON MY MACHINE
http://www.codinghorror.com/blog/archives/000818.html
================================================== =========
I know that CVS was evil, and now i got the proof:
http://worsethanfailure.com/Articles...-Hate-You.aspx
================================================== =========
Reply With Quote
  #3 (permalink)  
Old September 14th, 2007, 08:13 AM
Authorized User
 
Join Date: Oct 2006
Location: Kolkata, West Bengal, India.
Posts: 63
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi gbianchi,
             I am extremely sorry for way of approaching. Thank you very much for this solution.

Prasanta

Reply With Quote
  #4 (permalink)  
Old September 22nd, 2007, 06:56 AM
Authorized User
 
Join Date: Dec 2006
Location: Mumbai, Maharastra, India.
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Guys,

Why r u doing a very simple query in a very complicated manner.

This is good and simple query for your purpose.

select
    company,
    count(*) as Total,
    sum(case status when 0 then 1 else 0 end) as InStock,
    sum(case status when 1 then 1 else 0 end) as OutStock
from t_data
group by company

Try it.

Reply With Quote
  #5 (permalink)  
Old September 24th, 2007, 08:56 AM
Friend of Wrox
Points: 7,395, Level: 36
Points: 7,395, Level: 36 Points: 7,395, Level: 36 Points: 7,395, Level: 36
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Capital Federal, , Argentina.
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

Hi there... I didn't try your aproach, but looks very good, so thanks for getting a better solution :)

HTH

Gonzalo

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from dparsons signature and he Took that from planoie's profile
================================================== =========
My programs achieved a new certification (can you say the same?):
WORKS ON MY MACHINE
http://www.codinghorror.com/blog/archives/000818.html
================================================== =========
I know that CVS was evil, and now i got the proof:
http://worsethanfailure.com/Articles...-Hate-You.aspx
================================================== =========
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to provide download link for images snowbydave1 PHP How-To 2 March 27th, 2007 12:11 AM
You should provide WROX Certification zach007 Forum and Wrox.com Feedback 2 January 11th, 2006 10:58 PM
Why provide both books chrisda BOOK: Beginning VB.NET 2nd Edition/Beginning VB.NET 2003 0 February 24th, 2005 08:13 PM
how to provide synchronisation to the access of db sajeeshtk Beginning VB 6 0 April 26th, 2004 02:22 AM
Hope if any one can provide some help RahilPatwa All Other Wrox Books 0 November 8th, 2003 01:31 AM



All times are GMT -4. The time now is 07:27 AM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.