Here's a little background on what I'm trying to do.
[All_Distributors] is a list of products and product info.
[Brand_Volume_Conversion] is a list of products Ive measured and weighed for shipping purposes, which allowed me to calculate a Volume per lb for each item. I would like to use this volume per lb measurement to help me predict the volume of a product based on Brand and similar weighted items.
This is what I have so far (image) but Im getting Duplicate entries per given Part Number and its taking forever to do so. Im kind of stuck at this point, I thought someone else might have a better way of doing this or have some suggestions for a newby. Thanks for the help.
Ive made a little head way since I asked this question. I have now got the results Im after, however its still very slow. Between the 500,000 records in [All_Distributors] and another 7,000 [Brand_Volume_Conversion] it creeps and unusable. Anyone have any suggestions? Second picture is of working but slow code.
I would stick with your first version of the query, but with the following changes:
Use Brand as the related column between the two tables (i.e. the line)
Make it a "Totals" query but clicking the sigma in the ribbon at the top
Set the "Total" property for your Distributer+Partnum,Brand and Weight columns in the grid to "Group By"
Add your "Distributor_Weight" column to the query grid; set its "Total" property to "Where"
Set this new entry's "Criteria" property to ">=[Weight]*.9 And <=[Weight]*1.1"
Change your Vol/lb to "Vol/lb: [Volume per lb]" and set its "Total" property to "Average"
The idea here is to avoid using the sub-query in your calculation. That's what's really slowing things down. Also, I'm a little confused your multiplying by -1 twice and am assuming that matching on Brand is what you want.
Related
I have a project table which lists every project. I have a cost center table which lists every cost center. I have an analyst table which shows the project, the cost center, and the analyst assigned to them. The projects and cost centers are dropdowns lists. Every project should have every cost center included in it. For every project and cost center combination, there should be an analyst assigned to it. How do I see which ones I have missed? The query I keep trying has two outer joins and Access doesn't like that. With 30 projects and 15 cost centers it is easy to forget to assign an analyst to one of the combinations.
It would also be helpful to have some kind of query that easily shows who is assigned to what projects, preferably in a crosstab format (similar to a pivot table). I think I can do that if I have the corect query that links these 3 tables together and shows every project with every cost center and which analyst is assigned to them.
If my setup with 3 tables is the main problem I can redo the database design. I thought I was designing it correctly by having a seperate table for projects and cost centers and a 3rd table that combines them with the analysts. But now that I can't figure out how to get this query to work I am thinking maybe that wasn't the best design idea.
Sorry. I figured it out. I guess writing the question helped me think it through.
I used one query that had the project table and the cost center table in it. This created a list of every possible combination.
I then made a second query that linked the first query to the analyst table. I forced the query to show every combination from the first query and then tell me when an analyst matched that combination. This way I get blanks for every time I missed adding an analyst. It was also very easy to turn this 2nd query into a pivot table that shows all of the blanks.
Sorry again for posting this question.
I have a module on my website which lists products by their popularity and the list changes quite regularly. I'd like to automatically discount the top 3 popular products, this is easily done through changing the discount column to 5 for the 3rd product 10 for the 2nd and 15 for the top. However, as the list is regularly changing I need this discount id to revert back to whatever it was before it was overwritten with the new discount id.
Would I have to create another column and call it something like old_discount and transfer the old discount to that before changing the new one and then have it copy it back when it is not longer in the top 3?
I'm hoping someone has an easier way this might be done, any help at all would be appreciated. Thanks for your time.
There's probably no way to get around adding something to manage the top three discounts. As far as actual implementation, there's a couple of ways you could do this that come to mind:
Create a table just to store the current list of top 3 products (or a one-row table with three fields for the top three). Refresh this table with the top 3 when you automatically check. Then in your code that calculates prices, reference this table first to see what is being discounted before doing the rest of the calculation.
If you are going to add a column to the table, I would add a temp_discount instead of old_discount so you can always leave the original alone. Then your code should check if temp_discount is set, use that, otherwise use the normal discount column. Then you simply set temp_discount to 0 before setting the next top 3 products every time you want to refresh.
I'm currently working on writing report generators. For one report I need to do a breakdown by a given characteristic (supplier, logging user, language, etc which for each row includes the name of the characteristic I'm interested in, the number of items that match that characterastic, and the percentage of total items this figure represents. The first two aren't a problem, the third is.
For example, to get a breakdown by language I'd be using a query like this.
SELECT lang_id,
COUNT(IF(open=TRUE,1,NULL)) AS lang_total
FROM table
GROUP BY lang_id;
This gives me the number of items per language.
I can get the total number of items in the table and store it in a variable simply enough with a plain count.
SELECT #totalOpen:=COUNT(*) FROM table WHERE open = TRUE;
Now I want to add a third column, which is the figure in lang_total divided by the value in #totalOpen multiplied by 100 (in other words, the percentage of all items that fit the criteria). Something along the lines of the following:
This is the bit I'm having trouble with, as because as far as I can tell you can't use aggregate columns in calculations.
SELECT lang_id,
COUNT(IF(open=true,1,NULL)) AS lang_total
(lang_total/#totalOpen)*100 as lang_percent
FROM table
GROUP BY lang_id;
I'm sure that there must be a way of doing this in MySQL, but I've not been able to track it down. Can anyone help out with this?
I read this question now for the first time. I know that probably it's too late to be useful for you but I would have solved in this way.
select lang_id,
sum(if(open= true,1,0)) as lang_total,
coalesce(sum(if(open= true,1,null)) / #r,0) as percentage
from table,(select #r:=count(*) from table where open = TRUE) as t
group by lang_id;
I have no clue how to achieve this, I tried for hours now. I implemented a drag&drop of a list and now I want to save the change of the order in my db. I think for this is an order field needed which stores an int. But the problem is: how should I populate this field?
E.g. I want to do an operation like "insert a new list item after item at position 2", i would define a value higher than that of position 2 and lower than that of position three. But this is limited somehow.
An idea was to write new order values for each record when changing one to keep a clean order value. But that seems odd because it is slow as hell.
How do good systems solve this problem?
I hope, this is what you want
http://www.ajaxprojects.com/ajax/tutorialdetails.php?itemid=462
My client wants to sort products by drag & drop. The drag & drop part is easy with javascript.
My problem is how do I save and get the sort order?
I'm using .net c# and SQL Server 2008.
When I move a product and drop it in a new position I get the id of the product that's moved, product in front and product behind. With this data I want to update the sort order of products.
I was thinking of adding a field with position, but then I guess I have to update every item when position changes.
In general adding an additional position field is the only thing you can do, to get truly arbitrary ordering.
But you can implement it in several ways. Here are two ways I've implemented myself some time ago.
1. Method: Update all position values, by looping over your items and performing an UPDATE statement for every position.
This is easy to implement, but because of the many updates, it's not good for many items and/or large tables. Especially if you do it via Ajax and perform a complete re-ordering on every change in the list.
2. Method: Do a smart update of only the affected rows.
SELECT all items in the current sort order (The "old list") (Usually fast compared to an UPDATE statement)
Iterate over all items from the "new list" and compare each item to the item from the old list at the same position/index. If the items are the same, don't do anything
If the items are different find that item from the old list, which should actually be at that position and update its position value accordingly (Some lookup data structure might be useful here)
That way you only have to perform minimal database updates, but you'll have more complex code.
Personally I'd go with the first way, until the database updates actually become a performance problem.
We have a sort column but yes we have to re-index all rows as things change. You could mitigate this by assigning sort's in large enough increments to allow some level of movability before you have to do this, such as in 10's or 100's but that's not the best solution and I'd be interested to see what other ideas people have.
If you can capture each move programatically (with up and down buttons for example) then you can just swap the position numbers of the row moving and the row being moved. Make sure that you add new rows at the max position + 1.