Results 1 to 8 of 8

Thread: [RESOLVED] i need a proper/right query

  1. #1

    Thread Starter
    Hyperactive Member jp26198926's Avatar
    Join Date
    Sep 2008
    Location
    General Santos City, Philippines
    Posts
    310

    Resolved [RESOLVED] i need a proper/right query

    hello guys..

    can anyone tell me what is the proper query of display item that i want..
    i want to display the # of available,sold,used,defective items in every shipment.


    Shipment | Available | Sold | Used | Defective
    ---------+-----------+------+------+----------+


    i tried this query but not displaying the expected result..


    Code:
    rs.Open "Select DISTINCT [shipment],(Select count([status]) from item where [status]='available') as Available," & _
                              "(Select count([status]) from item where [status]='sold') as Sold," & _
                              "(Select count([status]) from item where [status]='used') as Used," & _
                              "(Select count([status]) from item where [status]='defective') as Defective" & _
                              " from item", cn, adOpenDynamic, adLockOptimistic
    thnk you...

    note: all fields are in the same table..
    "More Heads are Better than One"

  2. #2
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,957

    Re: i need a proper/right query

    For this sort of thing there's a neat trick using the sum, case and group by statements. Something like:-
    Code:
    Select Shipment,
             Sum(Case When Status = 'available' then 1 else 0 end) as Available,
             Sum(Case When Status = 'sold' then 1 else 0 end) as Sold,
             Sum(Case When Status = 'used' then 1 else 0 end) as Used,
             Sum(Case When Status = 'defective' then 1 else 0 end) as Defective,
    From Item
    Group By Shipment
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

  3. #3
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,969

    Re: i need a proper/right query

    SQL 2005

    Code:
    If object_id('tempdb..##Item') is not null drop table ##Item
    
    create table ##Item(Status VarChar(10))
    insert into ##Item(Status) Values('available')
    insert into ##Item(Status) Values('available')
    insert into ##Item(Status) Values('available')
    insert into ##Item(Status) Values('Sold')
    insert into ##Item(Status) Values('Sold')
    insert into ##Item(Status) Values('used')
    insert into ##Item(Status) Values('used')
    insert into ##Item(Status) Values('used')
    insert into ##Item(Status) Values('used')
    insert into ##Item(Status) Values('used')
    insert into ##Item(Status) Values('defective')
    insert into ##Item(Status) Values('defective')
    insert into ##Item(Status) Values('defective')
    insert into ##Item(Status) Values('defective')
    insert into ##Item(Status) Values('defective')
    insert into ##Item(Status) Values('defective')
    insert into ##Item(Status) Values('defective')
    
    SELECT * FROM ##Item
     PIVOT (COUNT(Status) FOR status IN ([available],[defective],[Sold],[used])) p
    
    drop table ##Item

  4. #4

    Thread Starter
    Hyperactive Member jp26198926's Avatar
    Join Date
    Sep 2008
    Location
    General Santos City, Philippines
    Posts
    310

    Re: i need a proper/right query

    @FunkyDexter and TysonLPrice: thank you for your reply, i will try your suggestion sir..

    btw i am using ms access and adodb

    thanks..
    "More Heads are Better than One"

  5. #5
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: i need a proper/right query

    @Tyson - Interesting solution to the problem. I don't have nearly enough PIVOT experience I should have... it's not something I think of out of the box. Very cool though.

    @jp261... - sigh... why do people wait until the discussion has started to mention things like the type of database? I mean, you wouldn't take your Honda to a BMW shop, as for a tune up and then as they are pulling it into the garage "Oh, by the way, it's a Honda..." No... that's part of the initial discussion.

    In this case I don't know that Access supports the CASE statement, but it should support the PIVOT... so that's going to be your best bet.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6
    Lively Member
    Join Date
    Jun 2008
    Location
    Bayang Magiliw, Perlas Ng Silangan
    Posts
    100

    Re: i need a proper/right query

    since you're using access, try this query

    Code:
    Transform count(Status) as StatusCount
    SELECT Shipment
    FROM Item
    group by Shipment
    pivot status

  7. #7

    Thread Starter
    Hyperactive Member jp26198926's Avatar
    Join Date
    Sep 2008
    Location
    General Santos City, Philippines
    Posts
    310

    Re: i need a proper/right query

    Quote Originally Posted by techgnome View Post

    @jp261... - sigh... why do people wait until the discussion has started to mention things like the type of database? I mean, you wouldn't take your Honda to a BMW shop, as for a tune up and then as they are pulling it into the garage "Oh, by the way, it's a Honda..." No... that's part of the initial discussion.

    In this case I don't know that Access supports the CASE statement, but it should support the PIVOT... so that's going to be your best bet.

    -tg
    heheeh i sorry sir.. i forgot to mentioned that yesterday.. i will be careful next time.. anyway thanks for your reply,..

    @nubie: Thank you sir.. your solution solves my thread..
    "More Heads are Better than One"

  8. #8
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: i need a proper/right query

    Quote Originally Posted by jp26198926 View Post
    heheeh i sorry sir.. i forgot to mentioned that yesterday.. i will be careful next time.. anyway thanks for your reply,..

    @nubie: Thank you sir.. your solution solves my thread..
    Sorry... I jsut see it so much... you simply got the brunt of it (and it was even watered down a bit) ... at least it only made it three posts... I've seen threads where we're 12 posts in and the OP will go "I'm using Access does that matter?".... somedays it's enough to make you bang your head in to the desk.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width