Select Product Order data with multiple criteria

my_sunshine

New Member
Joined
Sep 28, 2016
Messages
5
Hi all,

I hope you can help me to extract a new data table with all the fields from the attached product order data, using Access SQL or Access Design View. Please note each ID has one (ID 16) or more quotes (ID 13) and the quotes within an ID can be the same (A1) or different (A2-A4) but each row of data is an unique combination of the columns' values. The data is selected based on the Primary (0 or 1), Last change date, and no Product B nor Product C.

IDJudgmentPrimaryQuoteProductsQuantityLast change date
1311 A1Product A48/8/2016
1311 A1… Product B ….28/8/2016
1310 A2Product D Level 348/17/2016
1310 A3… Product B ….28/17/2016
1310 A4… Product B ….28/27/2016
1610 A5Product D Level 348/27/2016
1700 A6… Product B ….28/20/2016
1700 A15Product D Level 348/17/2016
1700 A7Product A28/27/2016
1700 A7… product C ….28/27/2016
1700 A7… product C ….28/27/2016
1700 A7… product C ….28/27/2016
1700 A7… product C ….28/27/2016
1700 A7… product C ….28/27/2016
1700 A7… product C ….28/27/2016
1400 A8Product A28/27/2016
1400 A8… product C ….28/27/2016
1400 A9Product A28/27/2016
1400 A9… product C ….28/27/2016
1400 A10Product A28/27/2016
1401 A10… product C ….28/27/2016
1400 A11Product A28/27/2016
1400 A11… product C ….28/27/2016
1400 A11… product C ….28/27/2016
1400 A11… product C ….28/27/2016
1400 A11… product C ….28/27/2016
1400 A11… product C ….28/27/2016
1500 A12Product A28/27/2016
1500 A12… product C ….28/27/2016
1500 A12… product C ….28/27/2016
1500 A12… product C ….28/27/2016
1500 A12… product C ….28/27/2016
1500 A12… product C ….28/27/2016
1500 A12… product C ….28/27/2016
1501 A13Product A28/27/2016
1501 A13Product D Level 328/27/2016
1500 A14Product A28/27/2016
1500 A14… product C ….28/27/2016
1500 A16Product A28/27/2016
1500 A16… product C ….28/27/2016
1500 A16… product C ….28/27/2016
1500 A16… product C ….28/27/2016
1500 A16… product C ….28/27/2016
1500 A16… product C ….28/27/2016
1801 a17… Product B ….28/27/2016
1800 a17… Product B ….28/27/2016
1801 a17… Product B ….28/27/2016
1800 a18… product C ….28/27/2016
1800 a18… product C ….28/27/2016
1800 a18… product C ….28/27/2016

<colgroup><col span="4"><col><col span="2"></colgroup><tbody>
</tbody>


The criteria are:

1) If an ID has only 1 quote with Primary = 0, and no Product B nor Product C, e.g. ID 16, we'll keep ID 16 quote A5 Product D level 3.
2) If an ID has multiple quotes with all the Primary = 0, e.g. ID 17, choose the quote with the latest Last Change date (8/27/16) and no Product B nor Product C. In this case, we'll keep ID 17 quote A7 Product A.
3) If an ID has all the Products = Product B or Product C, we'll NOT keep the ID e.g. ID 18.
4) If an ID has multiple quotes with more than 1 quote of Primary = 1 e.g. ID 13, we'll keep the ID that has no Product B nor Product C. In this case ID 13 quote A1 Product A, ID 15 quote A13 Product A and Product D Level 3 will be keep.
5) If an ID has multiple quotes and the quote with Primary = 1 has Product B or C, we will NOT keep the ID e.g. ID 14.

After the new data table is created, I'd like to add a new field (named Level 3) to the table to count the quantity of Level 3 for each ID, and subtract the same quantity from the Quantity field.

The new table will look like this:
IDJudgment PrimaryQuoteProductsQuantityLevel 3Last change date
1311 A1Product A48/8/2016
1610 A5Product D Level 348/27/2016
1700 A7Product A28/27/2016
1501 A13Product A28/27/2016
1501 A13Product D Level 328/27/2016

<colgroup><col span="4"><col><col><col><col></colgroup><tbody>
</tbody>
Please help! Thank you so so so much!
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Has anyone got the solution?

I have the diagram of selecting the quotes, but don't know how to attached the pdf/jpeg to this message.
 
Upvote 0
Can an expert give me a conclusion if this task is doable in Access? If not, what would be your work-around suggestion? Which software would you use? Thank you so so much!
 
Upvote 0
Looks to me like a succession of Union queries would be the way I'd attempt this. Simply put, if you include all the necessary fields and create a sql statement for each condition you outlined, you can assemble a set of records that satisfy your criteria. With the resulting query output, you can either populate an existing table, create a new one, or delete the records from the primary table that don't match the output. Research Union query if you don't know how they work. They're not too difficult to understand, but do have their design limitations. To create the individual sql portions of a Union query, I'd suggest creating separate select queries and copying the portions to your Union query. Keeping the select portion of each as a select query makes it easier to tweak a portion in design view since you cannot have a design view for a Union query.
 
Upvote 0
Thank you, Micron, for the advice. I've tried to create 4 tables for each of the conditions:
1) Table 1: IDs with the latest "last change date"
2) Table 2: IDs with NO product B nor product C
3) Table 3: IDs with Primary =1
4) Table 4: IDs with Primary =0

I used the Union query:
* Left Union Table 2 & Table 3 to a new table 5 (i.e. all rows of Table 2 and unique rows of Table 3).

Now I'm stuck with the next sql statement on table 5: if a data row of an ID with Primary =1 and have Product B or Product C, delete all rows of that ID. In this case, the new table 6 won't have ID 14 nor ID 18.

Please help.

Thank you so so much!
 
Upvote 0
I think this is going somewhere I hadn't intended. I'll review your 5 sets of criteria and data and post back.
 
Upvote 0
Well, I gave it a shot but I'm afraid there are too many parameters for me to figure out. I got as far as this, which seems to bring back the first two requirements but that might be a fluke. Sorry, but I have to admit defeat. I split up the products and level since they should not be mixed into the same field. Hopefully you have not done that.
Code:
SELECT tblSunshine.ID, Max(tblSunshine.Primary) AS MaxOfPrimary1, 
Max(tblSunshine.Quote) AS MaxOfQuote, Max(tblSunshine.Products) AS 
MaxOfProducts1, Max(tblSunshine.[Last change date]) AS [MaxOfLast change date]
FROM tblSunshine GROUP BY tblSunshine.ID
HAVING (((Max(tblSunshine.Primary))=0) AND ((Max(tblSunshine.Products)) Not In ("B","C")));
 
Upvote 0
Thank you, Micron. I'm wondering if SQL has a query that delete all rows of the same ID. I hope the query would help me to create the table 6.
 
Upvote 0
If it is as simple as you just put it, that would be
DELETE tblName.FieldName
FROM tblName
WHERE (((tblName.FieldName)=[ID value]));

A delete query cannot also make a table.
 
Upvote 0

Forum statistics

Threads
1,214,980
Messages
6,122,563
Members
449,088
Latest member
Motoracer88

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top