How do I format numbers in and Access crosstab query to show two decimal places? - sql

I have an Access crosstab query that displays the following results:
| SHORE_TYPE | Total Miles | Class 1 | Class 2 | Class 4 |
| ONSHORE | 31.37 | 0.337121212121212 | 12.4617424242424 | 0 |
I'd like it to display the following results instead. Note the 'Class' columns here show two decimal places:
| SHORE_TYPE | Total Miles | Class 1 | Class 2 | Class 4 |
| ONSHORE | 31.37 | 0.34 | 12.46 | 0.00 |
I've been able to configure the 'Total Miles' column by changing the Format and Decimal Places properties (in the Design View) to "Fixed" and "2," respectively. However, the query column (in Design View) that determines the value in the Class column has only a Format property, which I set to "Fixed"; there is not a Decimal Places property for me to adjust.
I have some similar crosstab queries that are showing the results in the way I desire, but I can't determine any differences between this one and those. Also, I've sometimes seen some of my queries display it the wrong way one time, then the desired way the next time.
This makes me wonder if the problem is a bug in Access, or if there is a something implicitly defined in my code that I should explicitly define.
Here is my SQL:
TRANSFORM IIf(IsNull(Sum([qryPartL].[MILES_OF_PHYS_LENGTH])),0,
Sum([qryPartL].[MILES_OF_PHYS_LENGTH])) AS SumOfMILES_OF_PHYS_LENGTH
SELECT qryPartL.SHORE_TYPE, Sum(qryPartL.MILES_OF_PHYS_LENGTH) AS [Total Miles]
FROM qryPartL
GROUP BY qryPartL.SHORE_TYPE
PIVOT qryPartL.CLASS_LOC_text In ("Class 1","Class 2","Class 4");
EDIT:
After closing and re-opening this query, the Total Miles column is now displaying 31.3714015..., and the properties I had previously set for this column in the Design View are now blank. So, it looks like Access does not consistently save these property settings. At least not in the context in which I was using them.

The trick is to use a series of nested functions.
CDbl: Converts the data to a Double number data type
FormatNumber: Returns an expression formatted as a number with a specified precision (2)
Nz: Returns the specified value (0) when a field is null
The CDbl function won't work if a value is Null.
I also removed the IIf function from the TRANSFORM clause since Nz works better in this case.
Here is the new SQL that returns the desired results. (I've added new lines and indents to make it easier to read. This is a not necessary step, and may in fact not be remembered by Access.)
TRANSFORM
CDbl(
FormatNumber(
Nz(
Sum([qryPartL].[MILES_OF_PHYS_LENGTH])
,0)
,2)
) AS SumOfMILES_OF_PHYS_LENGTH
SELECT qryPartL.SHORE_TYPE,
CDbl(
FormatNumber(
Nz(
Sum(qryPartL.MILES_OF_PHYS_LENGTH)
,0)
,2)
) AS [Total Miles]
FROM qryPartL
GROUP BY qryPartL.SHORE_TYPE
PIVOT qryPartL.CLASS_LOC_text In ("Class 1","Class 2","Class 4");
Thanks to Allen Browne and a tip on his awesome Access website for leading me to this answer.

Related

MariaDB - embed function to automatically sum columns and store result?

it is possible to store a function IN the table to automatically sum a group of columns and store the result in a final column?
ie:
+----+------------+-----------+-------------+------------+
| id | appleCount | pearCount | bananaCount | totalFruit |
+----+------------+-----------+-------------+------------+
| 1 | 300 | 60 | 120 | 480 |
+----+------------+-----------+-------------+------------+
where the column totalFruit is automatically calculated from the previous three columns and updated as the other columns update. in this specific application, there is ONLY going to be the one row. it would be spanky-handy to be able to just push the updated counts and then pull the calculated total out. i seem to recall reading about this ability somewhere, but for the life of me, i can't recall where... :poop:
if there is not way to do this, that's cool. but if there is... :smile:
TIA!
WR!
Yes, it is possible. But is it worth it? It is simple enough to do
SELECT ...
appleCount + pearCount + bananaCount AS totalFruit
...
See MariaDB Generated Columns for how to generate the extra column -- either as a real extra column or "virtual". What version of MariaDB?--There are a number of changes over time.
(MySQL users: 5.7.6 has a similar MySQL Generated Columns.)

SAP BO - Compare values between rows and conditionally display

I have a request where I need to create an integrity report where I am looking at sales data and verifying that the payment terms are consistent on all items (rows).
Sample data:
Sales # | Line # | Terms Code
100 | 1.0 | N90
100 | 2.0 | N90
101 | 1.0 | N60
101 | 2.0 | P45
101 | 3.0 | N60
Notice that on SO 101, line 2.0 has a different terms code. I need to detect that and only display order 101 on the report.
I was looking at using the PREVIOUS operator in a variable like this:
=If(Previous([Payment Terms Code])=[Payment Terms Code]) Then 0 Else 1
but that looks at the Order 100 line 2.0 and would flag it incorrectly.
I am not sure how I can do this, but maybe using the IN operator some how. Suggestions?
So is appropriate to describe the requirement as you need to see any Sales Orders that have more than one distinct terms code? If so, you can accomplish it like this:
Create a new variable:
Terms Count =Count([Terms Code]) in ([Sales #])
Add this object to the report. You should see "1" for all of the 100 records, and "2" for the 101 records.
Finally, add a filter to the report on Trans Count > 1.
The syntax for the Previous() function is:
Previous(dimension|measure|Self [;Row|col][;(reset_dims)][;offset][;NoNull])
The third parameter, reset_dims allows you to specify when the list of dimensions used to reset the calculation.
Thus, your formula would then be:
Previous([Payment Terms Code]; ([Sales #];[Line #]))
Notes:
You must always place dimensions in parentheses even if there is only one dimension in the list of reset dimensions.
When you specify a set of reset dimensions you must separate them with semi-colon

Spitting long column values to managable size for presenting data neatly

Hi I was wondering if there is a way to split long column values in this case I am using SSRS to get the distinct values with the number of product ID against a category into a matrix/pivot table in SSRS. The problem lies with the amount of distinct category makes it a nightmare to make the report look pretty shall we say. Is there a dynamic way to split the columns in say groups of 10 to make the table look nicer and easy to read. I was thinking of using in operator then the list of values but that means managing the data every time a new category gets added. Is there a dynamic way to present the data in the best way possible? There are 135 distinct category values
Also I am open to suggestions to make the report to nicer if anyone has any thoughts. I am new to SSRS and trying to get to grips with its.
Here is an example of my problem
enter image description here
Are your column names coming back from the database under the SubCat field you note in the comments above? If so I imagine your dataset looks something like this
Subcat | Logno
---------+---------------
SubCatA | 34
SubCatB | 65
SubCatC | 120
SubCatD | 8
SubCatE | 19
You can edit this so that there is an index of each individual category being returned also, using the Row_Number() function. Add the field
ROW_NUMBER() OVER (ORDER BY SubCat ASC) AS ColID
To your query. This will result in the following.
Subcat | LogNo | ColID
-----------+--------------+----------
SubCatA | 34 | 1
SubCatB | 65 | 2
SubCatC | 120 | 3
SubCatD | 8 | 4
SubCatE | 19 | 5
Now there is a numeric identifier for each column you can perform some logic on it to arrange itself nicely on the page.
This solution involves a Tablix, nested inside a Matrix nested inside a Matrix as follows
First create a Matrix (Matrix1), and set it’s datasource to your dataset. Set the Row Group Properties to group on the following expression where ‘4’ is the number of columns you wish to display horizontally.
=CInt(Floor((Fields!ColID.Value - 1) / 4))
Then in the data section of the Matrix (bottom right corner) insert a rectangle and on this insert a new Matrix (Matrix 2). Remove the leftmost row. Set the column header to be the Column Name SubCat. This will automatically set the column grouping to be SubCat.
Finally, in the Data Section of Matrix 2 add a new Rectangle and Add a Tablix on it. Remove the Header Row, and set it to be one column wide only. Set the Data to be the information you wish to display, i.e. LogNo.
Finally, delete the Leftmost and Topmost rows/columns from Matrix 1 to make it look tidier (Note Delete Column Row only! Not associated groups!)
Then when the report is run it should look similar to the following. Note in my example SubCat = ColName, and LogNo = NumItems, and I have multiple values per SubCat.
Hopefully you find this helpful. If not, please ask for clarification.
Can you do something like this:
The following gives the steps (in two columns, down then across)

How do you replace nulls in a crosstab query with zeroes?

Based on the following SQL in Access...
TRANSFORM Sum([Shape_Length]/5280) AS MILES
SELECT "ONSHORE" AS Type, Sum(qry_CurYrTrans.Miles) AS [Total Of Miles]
FROM qry_CurYrTrans
GROUP BY "ONSHORE"
PIVOT qry_CurYrTrans.QComb IN ('1_HCA_PT','2_HCA_PT','3_HCA_PT','4_HCA_PT');
... my results returned the following datasheet:
| Type | Total Of Miles | 1_HCA_PT | 2_HCA_PT | 3_HCA_PT | 4_HCA_PT |
| ONSHORE | 31.38 | | 0.30 | 7.80 | |
This result is exactly what I want except I want to see zeroes in the cells that are null.
What are some options for doing this? If possible, I'd like to avoid using a subquery. I'd also prefer the query to remain editable in Access' Design View.
I think you have to use the Nz function, which will allow you to convert NULLs to another value. In this case, I used the (optional) part of the function to say, "If Sum([Shape_Length]/5280) is NULL, set it to 0". You may have to use quotes around the 0, I can't recall.
TRANSFORM Nz(Sum([Shape_Length]/5280), 0) AS MILES
SELECT "ONSHORE" AS Type, Sum(qry_CurYrTrans.Miles) AS [Total Of Miles]
FROM qry_CurYrTrans
GROUP BY "ONSHORE"
PIVOT qry_CurYrTrans.QComb IN ('1_HCA_PT','2_HCA_PT','3_HCA_PT','4_HCA_PT');

query column returns multiple readings and needs to be multiple cells in ssrs

I have a cell from a query, returning multiple readings as below with a maximum of 8
|_____readings_____|
|1;2;3;..., 8 |
In my SSRS report, I need each reading to be in a seperate column, e.g
| a | b | c | ...|
| 1 | 2 | 3 | ...|
I am using the 2005 version of ssrs and sql server
Could anyone help? Kind regards
Report-level
You can use the Split function to take a delimited string and return an array; based on this you can specify the element you want from 0-7 to get your eight columns.
In an expression you'd do something like this:
=Split(fields!readings.Value, ";")(0) (1st element) or
=Split(fields!readings.Value, ";")(7) (8th element)
The problem with this is when there is less than 8 elements in the readings field; you will get an error reported - wrapping the expression in an IIf is not enough as this doesn't short circuit in SSRS and any problem string will error regardless.
To deal with these issues you can move the logic to custom code embedded in the report:
Function ElementByNumber(fieldValue As String, elementNumber As Integer) As String
If Split(fieldValue, ";").Length < elementNumber
ElementByNumber = Nothing
Else
ElementByNumber = Split(fieldValue, ";")(elementNumber - 1)
End If
End Function
You can then reference this in the report like:
=Code.ElementByNumber(fields!readings.Value, 8)
Repeat as required for each column you need.
Database level
The other non-SSRS specific workaround would be to handle this, if possible, at the database level, and just use the unpivoted data as a base for a Matrix in the report.
Erland Sommarskog has a series of articles under Arrays and Lists that present any number of methods to split strings in SQL Server; this SO question has a bunch of other alternatives.
Obviously if you're dealing with a fixed Data Source/DataSet this might not be an option.