Pie Chart in Report Builder is divided in lots pieces? - sql

My Query creates lots of entries because there are a lot of entries for dates in the view, and when I try to filter out the information in report builder it doesn't divide the pie graph into four pieces but divides it into the number of date entries in the view.
What I am supposed to do is get number parts sold for each class between dates. The dates are to be selected in report builder.
SQL Developer Query:
CREATE OR REPLACE VIEW QUERY3 AS
SELECT partclass, COUNT(p.partno) AS "Parts Sold", orderdate
FROM part p, salesorder s, orderprod o
WHERE p.partno = o.partno
AND o.orderno = s.orderno
GROUP BY partclass, orderdate;
Report Builder Query
SELECT * FROM QUERY3

You didn't post a query you use in Reports Builder. Apparently, you'll need to include a WHERE clause which restricts the result set by dates. Something like this:
select partclass, "Parts Sold", orderdate
from query3
where orderdate between :par_date_from and :par_date_to; --> this
A side note: consider recreating the view and naming the "Parts Sold" column differently, such as parts_sold. The way you put it, you'll always have to reference that column using double quotes and paying attention to mixed case.
:par_date_from and :par_date_to represent references to parameters you should create in the report. If you use the built-in Parameter Form, you'll be prompted to enter those values. Or, if you run the report differently, you'll have to pass those values to the report in order to use them.

Related

MS Access 2013, How to add totals row within SQL

I'm in need of some assistance. I have search and not found what I'm looking for. I have an assigment for school that requires me to use SQL. I have a query that pulls some colunms from two tables:
SELECT Course.CourseNo, Course.CrHrs, Sections.Yr, Sections.Term, Sections.Location
FROM Course
INNER JOIN Sections ON Course.CourseNo = Sections.CourseNo
WHERE Sections.Term="spring";
I need to add a Totals row at the bottom to count the CourseNo and Sum the CrHrs. It has to be done through SQL query design as I need to paste the code. I know it can be done with the datasheet view but she will not accept that. Any advice?
To accomplish this, you can union your query together with an aggregation query. Its not clear from your question which columns you are trying to get "Totals" from, but here's an example of what I mean using your query and getting counts of each (kind of useless example - but you should be able to apply to what you are doing):
SELECT
[Course].[CourseNo]
, [Course].[CrHrs]
, [Sections].[Yr]
, [Sections].[Term]
, [Sections].[Location]
FROM
[Course]
INNER JOIN [Sections] ON [Course].[CourseNo] = [Sections].[CourseNo]
WHERE [Sections].[Term] = [spring]
UNION ALL
SELECT
"TOTALS"
, SUM([Course].[CrHrs])
, count([Sections].[Yr])
, Count([Sections].[Term])
, Count([Sections].[Location])
FROM
[Course]
INNER JOIN [Sections] ON [Course].[CourseNo] = [Sections].[CourseNo]
WHERE [Sections].[Term] = “spring”
You can prepare your "total" query separately, and then output both query results together with "UNION".
It might look like:
SELECT Course.CourseNo, Course.CrHrs, Sections.Yr, Sections.Term, Sections.Location
FROM Course
INNER JOIN Sections ON Course.CourseNo = Sections.CourseNo
WHERE Sections.Term="spring"
UNION
SELECT "Total", SUM(Course.CrHrs), SUM(Sections.Yr), SUM(Sections.Term), SUM(Sections.Location)
FROM Course
INNER JOIN Sections ON Course.CourseNo = Sections.CourseNo
WHERE Sections.Term="spring";
Whilst you can certainly union the aggregated totals query to the end of your original query, in my opinion this would be really bad practice and would be undesirable for any real-world application.
Consider that the resulting query could no longer be used for any meaningful analysis of the data: if displayed in a datagrid, the user would not be able to sort the data without the totals row being interspersed amongst the rest of the data; the user could no longer use the built-in Totals option to perform their own aggregate operation, and the insertion of a row only identifiable by the term totals could even conflict with other data within the set.
Instead, I would suggest displaying the totals within an entirely separate form control, using a separate query such as the following (based on your own example):
SELECT Count(Course.CourseNo) as Courses, Sum(Course.CrHrs) as Hours
FROM Course INNER JOIN Sections ON Course.CourseNo = Sections.CourseNo
WHERE Sections.Term = "spring";
However, since CrHrs are fields within your Course table and not within your Sections table, the above may yield multiples of the desired result, with the number of hours multiplied by the number of corresponding records in the Sections table.
If this is the case, the following may be more suitable:
SELECT Count(Course.CourseNo) as Courses, Sum(Course.CrHrs) as Hours
FROM
Course INNER JOIN
(SELECT DISTINCT s.CourseNo FROM Sections s WHERE s.Term = "spring") q
ON Course.CourseNo = q.CourseNo

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.

MS Access Query to output all values and treat unavailable values as zero

I've created a query MS Access 2010 that is intended to take all fuel types (all values) for each date. I have the query relationship below: ) and have the following 6 values for fuel type: Diesel #2, MSFO, ULSD, Biodiesel, Used Oil, Heat Recovery.
I'm trying to output the fuel delivery for each fuel type for each date regardless if there's any fuel delivered that date
and
. What I'm getting as my output is below
.
I have tried to change the relationships such that all values on tbl_FuelType would output. This gave me the output on the figure above. I've tried entering a criteria to look for the specific fuel type (e.g. "ULSD") but if there's no data for that day, it will output with Null values, which I don't want. See criteria below
I've tried some program flow functions such as IIF and Switch but still getting null values. Is there an easy way to do this without having to go into the table and filling out values as zero's for all the different fuel types on the tbl_FuelDelivery? The SQL view is as follows:
SELECT tbl_FuelDelivery.DateLog, Sum(Nz([tbl_FuelDelivery].[F_FO_gal_Gross],0)) AS Fuel_Delivery_Gross, Sum(Nz([tbl_FuelDelivery].[F_FO_gal_Net],0)) AS Fuel_Delivery_Net, tbl_FuelType.FuelType
FROM tbl_FuelType LEFT JOIN tbl_FuelDelivery ON tbl_FuelType.ID = tbl_FuelDelivery.FuelType
GROUP BY tbl_FuelDelivery.DateLog, tbl_FuelType.FuelType
ORDER BY tbl_FuelDelivery.DateLog;
​
What you need to add to your query is a table with all dates. You can have a separate table where all dates are entered, such a calendar table or have it derived from your tbl_FuelDelivery like this:
SELECT DISTINCT tbl_FuelDelivery.DateLog
FROM tbl_FuelDelivery;
Now, you need to CROSS JOIN this table with tbl_FuelType. Access does not natively support cross joins, so you'll have to use a workaround: just add the cross join as a comma separated table to your FROM clause:
SELECT a.DateLog, tbl_FuelType.FuelType, tbl_FuelType.ID
FROM (SELECT DISTINCT tbl_FuelDelivery.DateLog FROM tbl_FuelDelivery) a,
tbl_FuelType;
The query above will give you the all fuel types for all dates. You can save it as a new query (let's call it allDatesFuels). Now, all you need to do is to join it with your query:
SELECT
allDatesFuels.DateLog,
Sum(Nz([tbl_FuelDelivery].[F_FO_gal_Gross],0)) AS Fuel_Delivery_Gross,
Sum(Nz([tbl_FuelDelivery].[F_FO_gal_Net],0)) AS Fuel_Delivery_Net,
allDatesFuels.FuelType
FROM allDatesFuels LEFT JOIN tbl_FuelDelivery ON allDatesFuels.ID = tbl_FuelDelivery.FuelType And allDatesFuels.DateLog = tbl_FuelDelivery.DateLog
GROUP BY allDatesFuels.DateLog, allDatesFuels.FuelType
ORDER BY allDatesFuels.DateLog;
​

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

SQL Query For Product Sales Report

I have a query that gives a product sales report by whatever date range I specify.
Something like select whatever from wherever where date ordered between start date and end date order by product id.
My page then loop through the recordset and displays the results on the page in a list.
What I would like to do is provide a list showing PRODUCT A total sales = whatever, PRODUCT B total sales = whatever so on and so forth. So as the loop runs product a = product a + 1
I do this already with staff sales, but there are only 5 staff so I have managed to do this, but there are over 300 product codes.
What is the best way to proceed.
Possible solutions:
Do this in your application code by keeping track of the product code and running totals. When the product code changes, emit an extra row with the totals into the output.
Do something similar to #1, but use a separate GROUP BY query to get the totals.
Create a SELECT statement that UNIONs together two queries, one for the product detail lines and one with the summary information.
Use some product specific command (you don't say what database you're using) to accomplish #3 without having to do the UNION yourself. Both MySQL and SQL Server offer (different) ROLL UP clauses that can do what you want.