Ms ACCESS and SQL: round to two decimals through query - sql

I use queries to calculate all kinds of supplier information (average lead time, total spend for that supplier, average price, etc.). All output is shown in listboxes in forms in Ms ACCESS.
Example of a calculated number:
How do I format the output of these queries to be rounded to two decimals? I've been playing around with the listbox settings but cannot find it there. I believe I will have to do it in the query itself, but I'm not sure how.
Query code for the above number:
SELECT Avg([Item Master].PlannedLeadTime) AS AverageLeadTime
FROM [Item Master]
WHERE ((([Item Master].DateStamp)>=[Forms]![History Supplier Tool]![List2] And ([Item Master].DateStamp)<=[Forms]![History Supplier Tool]![List3]) AND (([Item Master].SupplierName)=[Forms]![History Supplier Tool]![List1]));
Note: List1 is a listbox where the user can select a certain supplier (for which the calculations are performed) and list2 and list3 are dates the user can select (as to determine a date range for the calculations).

Access SQL has a rich function set one of which is the round function.
Example:
SELECT Round(Avg([Item Master].PlannedLeadTime),2) AS AverageLeadTime
FROM [Item Master]
WHERE (...)
Further information:
http://www.techonthenet.com/access/functions/numeric/round.php

Use ROUND() Function:
The ROUND() function is used to round a numeric field to the number of decimals specified.
SQL ROUND() Syntax:
SELECT ROUND(column_name,decimals) FROM table_name;
|Parameter |Description
________________________________________________
|column_name |Required. The field to round.
|decimals |Required. Specifies the number of decimals to be returned.
So, Your required query will be:
SELECT ROUND(AVG([Item Master].PlannedLeadTime),2) AS AverageLeadTime
FROM [Item Master]
WHERE ((([Item Master].DateStamp)>=[Forms]![History Supplier Tool]![List2]
And ([Item Master].DateStamp)<=[Forms]![History Supplier Tool]![List3]) AND (([Item Master].SupplierName)=[Forms]![History Supplier Tool]![List1]));

Related

SQL sum, multiple Group By's and Date criteria

I'm looking to perform a sum calculation on a SQL table to find the quantity of a particular stock item held against a particular salesperson up to and including a specific date.
I'm able to perform the sum function to find the quantities on a Salesperson/item basis whenever I do not factor in the date range criteria, but as soon as i add that aspect, it all goes a bit pear shaped! Here is my code so far:
SELECT Salesperson, Item No, Sum(Quantity) AS 'Quantity'
FROM dbo
WHERE (Location Code='VAN')
GROUP BY Salesperson, Item No,
HAVING (Registering Date<={ts '2017-05-03 00:00:00'})
The location code = VAN filter is required to ensure it ignores Warehouse quantities.My SQL knowledge is limited to the few instances I run into it at work and my interaction is largely based through Microsoft Query in Excel. When looking at the above code, i figured that the 'Registering date' criteria should be in the 'WHERE' section, however when i add the criteria using the options available in Microsoft Query, it creates the 'HAVING' line.
If anyone could provide any pointers, it would be much appreciated!
Cheers
Peter
I would imagine a query like this:
SELECT Salesperson, [Item No], Sum(Quantity) AS Quantity
--------------------^ escape the non-standard column name
FROM dbo.??
---------^ table name goes here
WHERE Location Code = 'VAN' AND
[Registering Date] <= '2017-05-03'
------^ put the filtering condition in the correct clause
GROUP BY Salesperson, Item No
-----------------------------^ remove the comma
Your code, as written, has multiple errors. I am guessing that most are transcription errors rather than in the original query (queries don't run if no table is given in the FROM for instance). The "major" error would then be filtering in the HAVING clause rather than the WHERE clause.

SSRS Divide Sums from Different Scopes Returns 0

I have a grouped dataset. From the parent group down, the group names are:
Company > Plant > Details. In the end, I want to be able to take the sales of each plant and see what percentage it is of the entire company's sales. Let's say the company has a total sales of $500 and the sales of "Plant A" were $100 and "Plant B" sales were $400. I would image that I would need an expression at the Plant grouping level that was like:
=Sum(Sales)/Sum(Sales, "Company")
And I would get .2 for Plant A and .8 for Plant B. But if I do that, I get 0. I am at a complete loss. Any help with this would be greatly appreciated.
Additional information:
My exact setup is a little more complex than the example I gave below, but I believe the general idea still holds the same. I have a total of 6 groups:
The circled group is the equivalent of the "Plant" Group in my example. Here is the row group in my tablix for the GroupBy group (the one with the arrow pointing to it).
The expression that is circled in the picture above is the expression in question to get my percentage (right now really just a decimal, not formatted to be a percentage yet).
=sum(Fields!ActualCurrent.Value)/sum(Fields!ActualCurrent.Value, "Company")
Fields!ActualCurrent.Value is the equivalent of "Sales" in my example above. The expression above returns 0 for all groups. But yet, if I change it to
=sum(Fields!ActualCurrent.Value)+sum(Fields!ActualCurrent.Value, "Company")
It will produce the equivalent of $600 for "Plant A" and $900 for "Plant B."
I can't seem to find how it reacts as expected when adding the two sums, but produces 0 when I divide them.
It would be useful to see where are you using that expression to determine what is wrong here, but I think you can use this guide to get your desired result.
Create a tablix like this:
Note I've added Company and Plant fields as groups. Also I've deleted details group. Right click details and select Delete group and set Delete group only option.
Now in the percentage column use the following expression:
=FORMAT(
SUM(Fields!Sales.Value,"Plant")/SUM(Fields!Sales.Value,"Company")
,"P2"
)
The sum of every plan divided by the sum of the whole company group. It is not necessary but I am using FORMAT function to format the float value returned by the expression to percentage format using two decimal places.
It should show something like this:
UPDATE: Try scoping the sum to your specific group: GroupBy
=sum(Fields!ActualCurrent.Value, "GroupBy")/sum(Fields!ActualCurrent.Value, "Company")
UPDATE 2: Format the cell to show decimal digits.
Use thiss expression:
=FORMAT(
sum(Fields!ActualCurrent.Value, "GroupBy")/sum(Fields!ActualCurrent.Value, "Company"),
"F2"
)
It will format the value returned by the expression as a float with two decimal digits.
If you want to show the value in percentage format replace F2 in the expression for P2 (Percentage format with two decimal digits.)
Let me know if this helps.

Query cannot be parse SELECT avg_galoon, avg_galoon

I dont know if its the right way to multiply that way, I want to multiply the avg_galoon that is sold weekly by the price of it which is 20
SELECT avg_galoon,
avg_galoon * 20 + NVL(total income , 0)
FROM customer;
total income isn't a column name from the customer table.
If you disagree and can see that column name (via methods mentioned in the comments already) then you will need to enclose that column name in the appropriate syntax for the database engine you are using i.e. [total income] for Microsofts SQL Server, `total income` for MySQL etc.
In future avoid the use of spaces in column names.

Calculating a Sum Value on a report in Access

I have a report that is supposed to show the Stock Value of products before and after conversion.
I have the source products product code and stock value displayed on the form, now I need to get the value of the items converted from that one piece of stock.
I assumed this would be possible to achieve using some simple SQL to calculate the sum of all resulting products with the same SCID (Stock Conversion ID). My SCID is used to link a source product to a number of resulting products in a different table.
In this image I have named the SCID boc in the detail section sSCID to try and differentiate it from any fields in tables that it may try to pick up. But basically I need to get the code to look for result products that match the SCID displayed in that box and add them all together to get the sum total of converted stock.
If context helps I am trying to create a form that shows the value of stock before and after a conversion process in order to try and calculate the wastage.
Thanks,
Bob P
You can either use DSum or build your report query to include the calculation. I would prefer the second option. This assumes that SCID is numeric, you would need quotes for a text SCID.
=DSum("sValue","[Stock Conversion Items]","SCID=" & sSCID)
Or
SELECT r.This,r.That,s.ConvValue
FROM ReportTable r
INNER JOIN (
SELECT SCID, Sum(sValue) As ConvValue
FROM [Stock Conversion Items]
GROUP BY SCID) s
ON r.sSCID = s.SCID
The above query uses aliases r and s for the tables : ReportTable r or ReportTables As r

Access 2007 Crosstab Query Expression

Goal: to create a percentage column based off the values of calculated columns.
Here's the SQL code of the Crosstab query:
TRANSFORM Count(Master_Calendar.ID) AS CountOfID
SELECT Master_Calendar.Analyst, Count(Master_Calendar.ID) AS [Total Of ID]
FROM Master_Calendar
GROUP BY Master_Calendar.Analyst
PIVOT Master_Calendar.[Current Status];
This gives me a crosstab query that displays the amount of entries in the database that are "Completed", "In Process", or "Not Started", sorted by which Analyst they belong to.
What I'm trying to do is add another column to calculate the Percent Complete -- so (Completed / Total of ID) * 100. I tried putting that into an expression in another cell, but it returns with a "[Completed]" not found, even though it gives me it as an option in the Expression Builder.
Am I just naming my variables wrong, or is it not possible to do it this way? Can I reference the total count of the records that contain "Completed" using query code instead of finding out the value using a Pivot table?
Thanks for your help.
Try:
SELECT
xTab.Analyst,
[Completed]/([Total of ID]/100) AS [Complete%],
[In Process]/([Total of ID]/100) AS [In Process%],
[Not Started]/([Total of ID]/100) AS [Not Started%]
FROM xTab;