count 0.5 for some IDs - ms-access

So this is for my dissertation and it is coming allong pretty well. almost finished it now xD
Anyway, I'm making a pub Epos system in access and its all OK, except now I have reached the stock control.
To get the query (Stock = Stock - Sales) I need to do a count query, which is easy enough, though the problem with pubs is they often serve half pints...
Is there any way to get the count SQL function to count certain ProductID's as 0.5?
This is a part of the table, and Product ID 2,4,6,8 and 10 are all relating to half pints and so count needs to recognise them as 0.5 instead of 1.
[URL=http://imageshack.us/photo/my-images/688/2121212e.png/][IMG]http://img688.imageshack.us/img688/64/2121212e.png[/IMG][/URL]
Thanks
Sam

It seems that you need to separate products purchased from products sold, lets say you called products sold "servings".
product (productId, supplierId, orderQuantity, reorderTrigger)
serving (servingId, productId, servingDesc, volumeOfServe)
this way you can have two servings of the one product (eq guiness)
servingDesc - "Guiness 1/2 pint" volumeOfServe - 0.5
servingDesc - "Guiness pint" volumeOfServe - 1

Related

What is the best DB schema to handle exceptions and prevent redundancy?

There are some products that can be sold in multiple units and the base unit is kilogram. The unit of each product has different weight, for example a Bundle of product A is 20kg but a Bundle of product B is 30kg
The following is my initial design.
product:
id name
product_variation:
id product_id name price
product_variation_unit:
id product_variation_id unit_id
product_variation_unit_weight:
product_variation_unit_id weight
The problem is than most of the variation of a product have the same unit and weight. So here is my solution but I'm not sure it's a good idea:
selectable_unit:
id selectable_id selectable_type unit_id
1 10 product 100
2 20 product_variation 101
selectable_unit_weight:
selectable_unit_id weight
And when I want to get the unit and weight of a variation, first I will check the variation and if there isn't, I will check it's product.
I't a little complicated, can you help me to find a better solution or improve it?
Have a look at the following diagram (Oracle Data Modeler notation). Usually, a price is depending on the SKU (stock keeping unit) and the period; a weight is depending on the SKU (packed product).
An SKU weight is limited by units allowed for its product (foreign key contains 2 columns).
SKU.value is the value in the unit to sell, i.e. kg, pcs, liters etc.

time difference grouped by 2 different parameters

I am wondering if anyone could help me with a current problem i am having.
I have a database table which has the following columns
enter image description here
I have various different Areas which can have the same product and what i am looking to do is to sum the difference for all the different products in their respective areas. for example in the table above i would get the below results ( product 1 in area 1 total is 3 minutes etc)
Area Product Time
Retail Product 1 00:03:00'
Packing Product 2 00:02:00'
The start and stop columns have the DATETIME datatype
I have tried sum(timediff(stop,start)) and group it by product but this does not appear to work.
Consequently I also want to sum the difference based on the area bit no matter what I put in i always get 181
I would appreciate any help on this
All you need to do is group by your area and product, and sum the times for each. The answer here should help. For your query, you'll just need something like this:
select
area
,product
,SEC_TO_TIME(SUM(TIME_TO_SEC(`stop`) - TIME_TO_SEC(`start`))) as timediff
from products_in_area
group by area, product
If you're not tied to showing the end result as a TIME value (e.g. you can do the formatting in your calling application), your query would look a bit cleaner:
select
area
,product
,SUM(TIMESTAMPDIFF(MINUTE, `start`, `stop`)) Minutes
from products_in_area
group by area, product

SSRS Show a percentage of a a total value

I have a regular Table in SSRS. With 3 Groups...
(Parent) STORE - CLERK - PRODUCT (Child)
I have some regular aggregations. How many PRODUCTS Sold by a CLERK , How Many CLERKS Per STORE and Eventually How many PRODUCTS Per STORE
On top of the Regular Sums And Avgs, I need To Find Out The Percentage of PRODUCT (Type) Meaning a Particular value of that Group.
Example STORE 001 Has Sold 10 RADIOS (a PRODUCT) and There has Been 100 RADIOS sold by all Stores
So Basically What I Need is to show STORE 001 is Responsible for 10% of all RADIO Sales.
(A note: Ideally , I would Like to show this To adjust to the Data - So if I add new products It will group those as products (Naturally) but still give me those percentages)
= fields!product.value / sum(fields!product.value)
in its most basic form you would want to use something like this.
The first will give you the total of the current row of data and the second will give you the total of all rows of that product.
Thus you would have 10 / 100 (per your example).
This is assuming that you have your data structured correctly. Depending on the structure of you report you may need to add a scope to your total summation to make sure that you are not totaling any other datasets that may reference the same product or field.
sum(fields!product.value, "--your dataset here--")

Adding or subtracting values based on another field's contents

I have a table with transactions. All transactions are stored as positive numbers, if its a deposit or withdrawl only the action changes. How do i write a query that can sum up the numbers based on the action
-actions-
1 Buy 2 Sell 5 Dividend
ID ACTION SYMBOL PRICE SHARES
1 1 AGNC 27.50 150
2 2 AGNC 30.00 50
3 5 AGNC 1.25 100
So the query should show AGNC has a total of 100 shares.
SELECT
symbol,sum(shares) AS shares,
ROUND(abs(sum((price * shares))),2) AS cost,
FROM bf_transactions
WHERE (action_id <> 5)
GROUP BY symbol
HAVING sum(shares) > 0
I was originally using that query when i had positive/negative numbers and that worked great.. but i dont know how to do it now with just positive numbers.
This ought to do it:
SELECT symbol, sum(case action
when 1 then shares
when 2 then -shares
end) as shares
FROM bf_transactions
GROUP BY symbol
SQL Fiddle here
It is however good practice to denormalize this kind of data - what you appear to have now is a correctly normalized database with no duplicate data, but it's rather impractical to use as you can see in cases like this. You should keep a separate table with current stock portfolio that you update when a transaction is executed.
Also, including a HAVING-clause to 'hide' corrupted data (someone has sold more than they have purchased) seems rather bad practice to me - when a situation like that is detected you should definitely throw some kind of error, or an internal alert.

Microsoft Access 2010 - Filtering by a caculated field in a query-based report

Ok, here's the condensed form. I have three main tables to draw data from:
StudentData - PK is the student's ID Number. Contains contact info, their current status ('00' for none, 'P' for Probation, 'S' for Suspension), and cumulative gpa data.
CourseData - PK is the CRN. Contains just the abbreviated subject and the course number (IE ECON 200)
StudentCourses - PK is an AutoNum. Many-to-Many relationship table between StudentData and CourseData. Also contains stats for the particular student's class (grade, credit hours, etc).
So some sample data would look like:
-
StudentData
ID: 12345678
Name: John Doe; ...[Other contact info]; 00; CumCreditHours: 100; CumCoursePoints: 190
-
CourseData
CRN: 0001; Abbrev: ECON; CourseNumber: 101
CRN: 0002; Abbrev: CSCI; CourseNumber 201
-
StudentCourses
AutoNum: 1
StudentID: 12345678; CRN: 0001; Grade: A-; Credits: 3
AutoNum: 2
StudentID 12345678; CRN: 0002; Grade: B; Credits: 3
-
At this point, this is how I have things set up:
First, a query runs that finds all of the courses a student takes and converts the letter grades into a point value. Another query based on the first sums the point totals and the credit hours. A third query takes those totals and calculates the GPA by dividing their point total by the credit hour total.
Separately, a query runs that calculates the student's cumulative GPA by taking their cumulative points and hours from the StudentData table (again dividing the points by the hours).
Then, another query takes both the semester GPA, cumulative GPA, current status, and cumulative credit hours and calculates the recommended action for the student. So for our example data it would look like:
_
SemesterGPA: 3.33; CumulativeGPA: 1.90; CurrentStatus: 00; CumulativeCreditHours: 100
_
I have the formula to determine their recommended action set up as a series of nested IIF statements that looks like:
IIf([CurrentStatus]="00" And [CumulativeGPA]<2 And [CumulativeCreditHours]>=12 And [CumulativeCreditHours]<=23,"PFY",IIf([CurrentStatus]="00" And [CumulativeGPA]>=1.7 And [CumulativeGPA]<=1.99 And [CumulativeCreditHours]>=24,"P",IIf([CurrentStatus]="00 " And [CumulativeGPA]<2 And [SemesterGPA]<2 And [CumulativeCreditHours]<12,"W",IIf([CurrentStatus]="00" And [CumulativeGPA]>=2 And [CumulativeGPA]<=2.5 And [SemesterGPA]<2 And [CumulativeCreditHours]>=12,"WUP",IIf([CurrentStatus]="00" And [CumulativeGPA]<1.7 And [CumulativeCreditHours]>=24,"SUSP",IIf([CurrentStatus]="P" And [CumulativeGPA]<2 And [SemesterGPA]>=2 And [CumulativeCreditHours]>=12,"CP",IIf([CurrentStatus]="P" And [CumulativeGPA]>=2 And [SemesterGPA]<2 And [CumulativeCreditHours]>=12,"SPCP",IIf([CurrentStatus]="P" And [CumulativeGPA]>=2 And [CumulativeCreditHours]>=12,"RP",IIf([CurrentStatus]="P" And [CumulativeGPA]<2 And [SemesterGPA]<2 And [CumulativeCreditHours]>=12,IIf(IsNull([PriorSuspension]),"S(FD)","D(SD)"),"None")))))))))
For our example, John Doe's recommended action would be 'P' since his current status is 00, he has over 24 cumulative credits, and his cumulative gpa is between 1.7 and 1.99.
Now for the problem: I would like to be able to make a report that can filter by the recommended action. I've detailed the issues I've had previously, but in short, the way I have the reports set up now (with the information being displayed in a sub-report inside a report that is based on the StudentData table to provide the aforementioned queries with the StudentID) doesn't allow me to do this because the field I want to filter by exists in the sub-report and not the main report (and you can't set the filter properties for a sub-report).
Any ideas?
Ok, I've got this to work now. I realized that as I was calculating the semester GPAs that I had grouped the results together by ID at some point during the process. This allowed me to remove the part of the initial query that grabbed the ID number from the form. Since the last query in the process linked everything together by student ID, the individual records held all of the information I needed and everything fell into place after that.
I half expected something simple like this to be the answer to my problem, but I'm still disappointed that it took me this long to figure it out...
Anyway, thanks to anyone who might have been thinking of possible solutions for this problem.