Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Database > SQL Language
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 14th, 2007, 01:30 AM
Authorized User
 
Join Date: Oct 2006
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

 
Old September 14th, 2007, 07:24 AM
Friend of Wrox
 
Join Date: Jun 2003
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
================================================== =========
 
Old September 14th, 2007, 08:13 AM
Authorized User
 
Join Date: Oct 2006
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

 
Old September 22nd, 2007, 06:56 AM
Authorized User
 
Join Date: Dec 2006
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.

 
Old September 24th, 2007, 08:56 AM
Friend of Wrox
 
Join Date: Jun 2003
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
================================================== =========





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





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