Crystal report - 2 logic in single column - sql-server-2008

We have requirement in Crystal report where we have to implement 2 concept in one column.
For eg: in below screenshot,
 if we have numbers we need to sum up and display as one row.
 if it is variable row. It should remain as single row.
Material Quantity Price
O Ring 1 5.61
O Ring 1 Fixed
O Ring 1 5.61
O Ring 1 Fixed
Output should be,
Material Quantity Price
O Ring 1 11.22
O Ring 1 Fixed
O Ring 1 Fixed
Kindly pour us some suggestions if any.

First note that your current table design is very flawed, because you are trying to store numeric and text information in the same Price column, both as text. This means that to sum up any prices, we have to cast to a number, and then cast back again to text to report with the other text only values. You would be much better off not storing text in the Price column, and making that column numeric.
That being said, the following query appears to do what you want:
SELECT
Material, MAX(Quantity) AS Quantity,
CAST(SUM(CAST(Price AS numeric(9,2))) AS varchar(15)) AS Price
FROM yourTable
WHERE Price <> 'Fixed'
GROUP BY Material
UNION ALL
SELECT Material, Quantity, Price
FROM yourTable
WHERE Price = 'Fixed';
Demo

Related

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

Compare 2 dynamically created matrix columns to get a difference

I have a matrix set up with a Row Group called "company", a Column Group called "Year" and a value field that is a sum of charges through the year called revenue. The column Group will end up having 2 columns in it that are provided through user input. The end results is a matrix that will look something like this:
Year 1 | Year 2
Company: $500 $250
Company2: $750 $250
What I would like to do is add a column to the matrix that calculates the change from year 1 to year 2. Is there a way to do this within the matrix such as adding a new column with an expression that compares the 2 entries in the row or will I need to manipulate the SQL code to create a column that does this within the code? To that end here is a view of the code for the dataset if that is the way I need to go:
SELECT
company.cmp_id
,company.Company
,ChargeDetails.[Bill To ID]
,ChargeDetails.[Delivery Year]
,ChargeDetails.Revenue
FROM
ChargeDetails
LEFT OUTER JOIN company
ON ChargeDetails.[Bill To ID] = company.cmp_id
WHERE
ChargeDetails.[Delivery Year] = #DeliveryYear
OR
ChargeDetails.[Delivery Year] = #ComparisonYear2
ORDER BY ChargeDetails.[Delivery Year] DESC,ChargeDetails.Revenue DESC;`
Try to add a column outside the Year group in header type Change or what you want to put, in the below cell use this expression:
=Sum(iif(Max(Fields!DeliveryYear.Value)=Fields!DeliveryYear.Value,Fields!Revenue.Value,0))-
Sum(iif(Min(Fields!DeliveryYear.Value)=Fields!DeliveryYear.Value,Fields!Revenue.Value,0))
Note I am substracting the min year (1) revenue sum to the max
year (2) revenue sum.
In your example it will produce:
+-----------+--------+--------+--------+
| Company | Year 1 | Year 2 | Change |
+-----------+--------+--------+--------+
| Company A | 500 | 250 | -250 |
+-----------+--------+--------+--------+
You can define change in many ways maybe in percentage or any other measure, this is only an example, learn what the above expression is doing in order to calculate the measure you need.
EDIT 1:
Add the matrix with this data arrangement.
The matrix looks like this one in preview:
Be sure you added the Change column outside the DeliveryYear group as shown in above image. Also check the fields names correspond to yours in the expression.
EDIT 2:
If Revenue has null values you can try this:
Try replace the null values at query level using T-SQL ISNULL() Function, change your query in this part as follows
,ISNULL(ChargeDetails.Revenue,0)
However if DeliveryYear has null values you may want to ignore that records, so try to exclude it in the where clause.
Let me know if you need further help.
I ended up using the following as the expression in the change column.
=sum(iif(Fields!Delivery_Year.Value=Parameters!DeliveryYear.Value,1,0)*Fields!Revenue.Value) - sum(iif(Fields!Delivery_Year.Value=Parameters!ComparisonYear2.Value,1,0)* Fields!Revenue.Value)

SSRS List formatting

Need to make a list in SSRS that has a numbered range from 0-30 in the rows and allows for null values to be entered in as a dash. i.e if I have 8 players who scored 10 points the list would show an 8 in the row value next to the row value 10 but for the other 29 numbers it would show a dash(-)?
You may have to make a couple of adjustments, but this should get you most of the way there. There wasn't a lot of information to go by to determine exactly what you want.
Adjust your dataset query in SSRS to the following, replacing the subquery Z with your current query that provides the points and player count. I inserted dummy data in there for now so I would have data for this example (8 = 1, 13 = 1, 17 = 2).
With X as
(
select top (30) n = ROW_NUMBER() OVER (ORDER BY m1.number)
from master.dbo.spt_values as m1
CROSS JOIN master.dbo.spt_values as m2
)
,Y as
(
Select Points, PlayerCount, ROW_NUMBER() OVER (Order by PlayerCount) RowNum
from (
--replace this with your query to return the data
--with your 2 columns for points and player count
Select 8 as Points, 1 as PlayerCount
UNION
Select 17 as Points, 2 as PlayerCount
UNION
Select 13 as Points, 1 as PlayerCount) Z
)
Select x.n as Points, /*isnull(Y.Points,0),*/ Isnull(Y.PlayerCount,0) PlayerCount
from X
left join Y on X.n = Y.Points;
The CTE labeled X is what creates the 30 spots. If you want it to be 31 spots (0 - 30 inclusive), change the query in X to be
select top (31) n = ROW_NUMBER() OVER (ORDER BY m1.number)-1
from master.dbo.spt_values as m1
CROSS JOIN master.dbo.spt_values as m2
You end up with a data set with 2 columns: Points and PlayerCount.
Now create your list in SSRS.
Insert a list. In that list, insert 2 columns to the right. Then delete the left most (original) column.
Set the expression for the left textbox in the list to the points field.
Set the expression for the right textbox in the list to the PlayerCount field.
Add a row outside the group above. Type in column headers for each column. I used Points and Player Count.
In the Group Properties for the Details row group, go to Sorting and set the Sort By column to points. The order should be A to Z.
Adjust the height of the rows to whatever looks best to you. I used .25 for the header and detail rows.
In the Tablix properties, check the box next to Keep together on one page if possible.
On the text box containing the Player Count field (bottom right), go to text box properties, Number category. Set it to 0 decimal places. Check the box next to Show zero as:. Make sure - is selected.
That gives you a list that looks like this:
If for some reason you want to see only the numbers that have > 0 players with that amount of points at the top and then the rest, you can do this with a calculated column.
Right click on your data set and add a calculated field. Set the Name to PointsCountSort. The the expression to =iif(Fields!PlayerCount.Value = 0, 2, 1)
. Click OK.
In the Sort Order of the Details group. Change the sort order to go by PointsCountSort A to Z then Points A to Z.
That makes the list sorted like this:

Graphing a single row in an SSRS Line Graph

I have a table setup with the following columns:
Product Name
SalesMonth1
SalesMonth2
SalesMonth3
SalesMonth4
SalesMonth5
An example of a row is as follows:
Bread
300
600
800
900
1000
I am trying to put this into a line graph in SSRS but am having trouble figuring out which fields go where. In my dataset, I have a field for each "SalesMonth" column. So in my dataset, I have 6 fields total including the product name. Will this work? On which axis should the product name go and the sales fields go?
I hope your dataset uses SQL.
I would "unpivot" the data by writing a 5-part UNION SELECT, e.g.
SELECT Product_Name , 1 AS Month , SalesMonth1 AS Sales
UNION ALL
SELECT Product_Name , 2 AS Month , SalesMonth2 AS Sales
UNION ALL
...
SELECT Product_Name , 5 AS Month , SalesMonth5 AS Sales
Then in the SSRS Chart definition:
Month = Category Group
Product Name = Series Group
Sales = Values
But the real real answer is that your data is probably not in a useful shape for reporting. Will there only ever be 5 months? I suspect not...

Calculated value displays as hash marks (####) in Access query

I need to determine the GST amount from the retail price. I originally had the retail price plus GST (retail price * 1.1) however i the GST is included in the retail price so i need to use (retail price / 1.1)
SELECT Groups.GroupID, Groups.GroupName, Groups.CountryOfOrigin, CDs.CDName,
CDs.YearOfRelease, CDs.CDType, CDs.RetailPrice,
([CDs]![RetailPrice]*1.1) AS GST_Inc
FROM Groups INNER JOIN CDs ON Groups.GroupID = CDs.GroupID
WHERE (((Groups.CountryOfOrigin)="australia") AND ((CDs.CDType)="album"))
ORDER BY Groups.GroupName, CDs.CDName;
When i change it to divide for example,
SELECT Groups.GroupID, Groups.GroupName, Groups.CountryOfOrigin, CDs.CDName,
CDs.YearOfRelease, CDs.CDType, CDs.RetailPrice, ([CDs]![RetailPrice]/1.1) AS GST
FROM Groups INNER JOIN CDs ON Groups.GroupID = CDs.GroupID
WHERE (((Groups.CountryOfOrigin)="australia") AND ((CDs.CDType)="album"))
ORDER BY Groups.GroupName, CDs.CDName;
It does not return any value just hashtags in the field.
When Using ROUND(GST,1) for example,
SELECT Groups.GroupID, Groups.GroupName, Groups.CountryOfOrigin, CDs.CDName,
CDs.YearOfRelease, CDs.CDType, CDs.RetailPrice, ROUND (GST,1), [CDs]![RetailPrice]-
[RetailPrice] /1.1 AS GST
FROM Groups INNER JOIN CDs ON Groups.GroupID = CDs.GroupID
WHERE (((Groups.CountryOfOrigin)="australia") AND ((CDs.CDType)="album"))
ORDER BY Groups.GroupName, CDs.CDName;
I get two returns one as GST which is the one with like 10 decimal points, and a new field EXPR1007 with the rounded GST. How do i make the rounded GST the entry for the GST field? I have tried moving the round statement however it will not accept it.
In Datasheet View, Access will display hash marks (#) if numeric or date values are wider than the column will display.
Just drag the right side of the column to make it wider and the numbers should appear.
Hash tags are a sign that the column width is too narrow to show the entire value, so rather than truncate and possibly mislead you, access shows hashes. Either make the column wider, which will show you the number possibly with lots of decimal places, or, because you're dealing with money (and only need two decimal places, wrap the division part in a Round() function (http://www.w3schools.com/sql/sql_func_round.asp for more information).
For anyone searching this all these years later, there is an option to handle this. I'm not sure how long the option has been there but it is available now, and makes Access behave more like Excel (fits as many decimal digits as will fit in the column and rounds as needed).
Under File | Options | Current Database | Check for truncated number fields, UNcheck the option for this truncate/round behavior.