Search code examples
where-clausehavingsqlanywherewhere-in

How to return a group of rows when one row meets "where" criteria in SQL Anywhere


I am somewhat overwhelmed by what I am trying to do, since I have only been using SQL for 3 days now, but I already love the increased functionality over MS query. The need for the IN function is what drove me to learn about this, and I thank the community for the info here to get me through learning that.

I tried looking thru other questions, but I couldn't find one in which the intent was to group more than two rows, or to group a varying number of rows. This means that count and duplicate are both out as options.

What I am doing is analyzing a table of part number information that spans multiple store locations. The table gives a row to each instance of a part number, so if all 15 stores have some sort of history for a given part number, that part number will have 15 rows in the table.

I am wanting to look at other store's history for parts that meet the criteria of 0 sales history for my location. The purpose is to see if they can be transferred to another store instead of being returned to the vendor and incurring a restock fee.

Here is a simplified version of the table organized in the way I would want the output to be structured. I got here by having suspected part numbers and using the list of them as a text string in IN() but I want to go about this the other way and build a list of part numbers from sales data in this table.

Branch| Part_No| Description|  Bin Qty|current 12 mo sales|previous 12 mo sales|
------|--------|------------|---------|-------------------|--------------------|
20     CA38385   SUPPORT        2       1                   1
23     CA38385   SUPPORT        1       0                   0
25     CA38385   SUPPORT        0       0                   1

20     DFC10513   Hdw Kit       0       1                   0
23     DFC10513   Hdw Kit       1       0                   0
07     DFC10513   Hdw Kit       0       1                   0

3      D59096     VALVE         0       0                  12
5      D59096     VALVE         0       0                   4
6      D59096     VALVE         4       6                  12
8      D59096     VALVE         0       0                   0
33     D59096     VALVE        11      14                  18
21     D59096     VALVE         4       4                   4
22     D59096     VALVE         0       0                   0
23     D59096     VALVE        10       0                   0
24     D59096     VALVE         0       0                   0
25     D59096     VALVE         0       0                   0
26     D59096     VALVE         2       2                   0

1      TE67401  Repair Kit      1       1                   2
21     TE67401  REPAIR KIT      1       3                   0
22     TE67401  REPAIR KIT      0       1                   0

I am branch 23, so the start of the query as I understand it would be

Select * from part_information
Group By part_number
Having IN(Branch) 23 and bin qty > 0 and current_12_mo_sales=0 and previous_12_mo_sales = 0

Can you point me down the right track? This table has approx. 200000 rows in it, so I really need to learn how to do this. I really don't see a better way.

Thank you in advance for your help and or criticism -Cody


Solution

  • This ended up working the way I wanted

        SELECT pi_Branch, pi_Franchise, pi_Part_No, pi_Description, pi_Bin_Qty,
               pi_Bin, pi_current_12_mo_sales, pi_previous_12_mo_sales, pi_Inventory_Cost, 
               pi_Return_Indicator 
        From Part_Information
                 Where pi_Part_No IN (Select pi_Part_No
                                      From Part_Information
                                      Where pi_Branch=23 And 
                                      pi_Bin_Qty>0 And pi_current_12_mo_sales<=0 
                                      And pi_previous_12_mo_sales<=0)
    

    I was thinking that this had to be some complex process, but in reality, two simple queries were all that was needed.

    I would still be interested in anyone's opinion on a better or more efficient way of handling this.

    Thanks Mischa for getting me there!