Results 1 to 8 of 8
  1. #1
    GeorgeB is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Jan 2012
    Posts
    18

    Question Help : Query based on data from multiple rows

    Hello guys



    I am here again with help needed

    this is a simple of what I am trying to do :

    ContractNo ProjectNo WorkNo WoStatus WoType
    11111 2222 1 A x
    11111 2222 2 B Y
    11111 2222 3 B Z
    11111 2222 4 A S
    11111 3333 1 B Y
    11111 3333 2 C Z

    and so on

    I need to select or have a column that gives me only the "ProjectNo"s that got WoStatus 1 and 2and 3 and 4 in WoStatus A or B And WoType X and Y and Z and S

    if this possible ?

    I've read about aggregations but I didn't work our for me

    Thanks very much

  2. #2
    recyan's Avatar
    recyan is offline Expert
    Windows 2K Access 2000
    Join Date
    Dec 2011
    Posts
    662
    Just check if below gives some guidelines :
    Code:
    SELECT 	myTable.ContractNo, myTable.ProjectNo, myTable.WorkNo,myTable.WoStatus, myTable.WoType FROM 	myTable WHERE (	((myTable.WorkNo)="1") 	OR 	((myTable.WorkNo)="2") 	OR 	((myTable.WorkNo)="3") 	OR 	((myTable.WorkNo)="4")) AND (	((myTable.WoStatus) = "A") 	OR 	((myTable.WoStatus) = "B") ) AND (	((myTable.WoType) = "X") 	OR 	((myTable.WoType) = "Y") 	OR 	((myTable.WoType) = "Z") 	OR 	((myTable.WoType) = "S") );
    Edit : The Code tags don't appear to be working. Thanks

  3. #3
    GeorgeB is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Jan 2012
    Posts
    18
    Thanks very much Recyan for your input

    to clarify what I want

    I want to extract the Project number Where :

    1st condition) : It must have at least 4 "WorkNo"
    2nd
    Condition) : The "WoStatus" must be whether A OR B only
    3rd Condition) : Must have the 4 different "WoType" (x,y,z,s)


    Thanks again

  4. #4
    recyan's Avatar
    recyan is offline Expert
    Windows 2K Access 2000
    Join Date
    Dec 2011
    Posts
    662
    Totally misread your requirement.
    Is there a possibility that the same project number can have 2 or more same "WoType".
    Eg:.
    ContractNo ProjectNo WorkNo WoStatus WoType
    11111 2222 1 A x
    11111 2222 2 B Y
    11111 2222 3 B
    Z
    11111 2222 4 A
    Z

    Thanks


  5. #5
    GeorgeB is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Jan 2012
    Posts
    18
    Yeah in some cases in happens

  6. #6
    recyan's Avatar
    recyan is offline Expert
    Windows 2K Access 2000
    Join Date
    Dec 2011
    Posts
    662
    Did not ask you earlier, but am assuming that there will be no duplication of "WorkNo" against a particular "ProjectNo".
    If there is a possibility, then ignore / try modifying logic of below :

    Just check out if below gives some guidelines :
    Save below as sub-query qryWoType :
    Code:
    SELECT 
        myTable.ProjectNo, 
        myTable.WoType
    FROM 
        myTable
    WHERE 
        (
            (
                (myTable.WoStatus)="A"     Or (myTable.WoStatus)="B"
            )
        )
    GROUP BY 
        myTable.ProjectNo, 
        myTable.WoType
    HAVING 
        (((myTable.WoType)="X" Or (myTable.WoType)="Y" Or (myTable.WoType)="Z" Or (myTable.WoType)="S"));
    Run this as final query :
    Code:
    SELECT 
        qryWoType.ProjectNo, 
        Count(qryWoType.WoType) AS CountOfWoType
    FROM
        qryWoType
    GROUP BY 
        qryWoType.ProjectNo
    HAVING 
        (((Count(qryWoType.WoType))>=4));
    You can check what is happening if something goes wrong, by running the sub-query seperately & taking a look at the results.

    Thanks

  7. #7
    GeorgeB is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Jan 2012
    Posts
    18
    Hi Recyan

    I tried the first query but I get an error msg "You tried to execute a query that does not include the specified expression WO_STATUS as part of an aggregate function" do you have any idea why

    SELECT WO.SP_NUMBER,
    WO.WO_STATUS
    FROM
    WO
    WHERE
    (
    (
    (WO.WO_STATUS)="dlp" Or (WO.WO_STATUS)="fc" Or (WO.WO_STATUS)="pw"
    )
    )
    GROUP BY
    WO.SP_NUMBER,
    WO.CPU_TYPE
    HAVING
    (((WO.CPU_TYPE) like "mobiles*detai*" Or (WO.CPU_TYPE) like "mobiles*Pre*" Or (WO.CPU_TYPE) like "mobiles*rf*" Or (WO.CPU_TYPE) like "mobiles*sae*"));
    The actual code

    by the way this my first time to deal with SQL

  8. #8
    recyan's Avatar
    recyan is offline Expert
    Windows 2K Access 2000
    Join Date
    Dec 2011
    Posts
    662
    In the SELECT you have :
    Code:
    SELECT 
        WO.SP_NUMBER, 
        WO.WO_STATUS
    and in the GROUP BY you have :
    Code:
    GROUP BY 
            WO.SP_NUMBER, 
            WO.CPU_TYPE
    Instead of "WO.WO_STATUS" in the SELECT have "WO.CPU_TYPE". Again, check your data functionality. I have written based on the fields you had provided.

    Thanks

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Query to find and COMBINE rows based on 2 fields
    By admessing in forum Queries
    Replies: 2
    Last Post: 12-13-2011, 12:59 PM
  2. Query on multiple rows
    By TPH in forum Access
    Replies: 10
    Last Post: 09-02-2011, 12:52 PM
  3. Replies: 2
    Last Post: 08-28-2011, 06:06 AM
  4. Editing Multiple Rows of Data with (Ctrl F)
    By Orangeworker in forum Access
    Replies: 2
    Last Post: 11-11-2010, 03:31 PM
  5. Replies: 11
    Last Post: 09-02-2010, 01:59 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums