How to Sum and Mulitply row of Specific Column in SQl Server - sql

I have database with table units with column name Transformertype and Transformeramount and section.
I want to first multiply Transformertype and Transformeramount and then add them but only where Section column contain specific type of value like 'Pattika', 'Baheri' and 'Kahori'. and then display it into label.
My Query is
select
SUM(TransformerType*TransformerAmount)
from
units
where
(Section LIKE'%Pattika%')
And
(Section LIKE '%Baheri%' )
And
(Section LIKE '%Kahori%')
But this query not showing any result into my label. Help Require

SUM is an aggregate function. It will sum the total for each group.
I don't know if I understand 100% your query, but please comment here and I will glad to update my answer:
select
SUM(TransformerType*TransformerAmount),
section,
COUNT(*) as TotalSection
from
units
where
(Section LIKE'%Pattika%')
OR
(Section LIKE '%Baheri%' )
OR
(Section LIKE '%Kahori%')
GROUP BY Section

Related

Summing the value of the aggregate function Last()

enter image description hereI am trying to design a report in ssrs that returns the last opening stock for each product in the dataset. To achieve this I used
=Last(Fields!CustProdAdj_new_openingstockValue.Value).
This works fine. But where I encountered a problem is in getting the sum of all the opening stock for each product. I tried using
=sum(Last(Fields!CustProdAdj_new_openingstockValue.Value))
but I got the error message
[Error on Preview]
Please is there another way to go about this
I have tried using aggregate(), runningValue(), to no avail
This is the dataset
This is the report layout
On previewing having used max()
This is probably easier to do directly in the dataset query but assuming you cannot change that, then this should work...
This assumes your data is ordered by the CustProdAdj_createdon column and that this is a date, datetime or some other ordered value, change this bit if required.
=SUM(
IIF(Fields!CustProdAdj_createdon.Value = Max(Fields!CustProdAdj_createdon.Value, "MyRowGroupNameHere"),
CustProdAdj_new_openingstockValue,
0)
)
Change the MyRowGroupNameHere to be the name of the rowgroup spelled exactly as it is in the rowgroup panel below the main design panel. Case sensitive and include quotes.
What this does is, for each row within the rowgroup "MyRowGroupNameHere", compare the CustProdAdj_createdon date to the max CustProdAdj_createdon across the rowgroup. . If it is the same then return the CustProdAdj_new_openingstockValue else return 0.
This will return the value required on only 1 record within the group.
For example, if you had 1 row per day then only on the last day of the month would a value be returned other than 0 because only the date of the last record would match the maximum date within the group.
Then it simply sums the results of this up.

How to display group SL number in group header section on Crystal Report

I have a report where I need to add a serial number for Group Title.
I require to generate this number in the report; let us consider it as Group Number (as we do Row Number in the detail section)
There's a built-in function that returns the current group's number. So you could just create a formula field with it:
GroupNumber
and place the formula field in your group header.

SQL custom grouping expression based on the field value string Microsoft Visual Studio

I am having a table in BIDS with three columns (e.g. Team, Product Name, Count).
For the Column Product name, I want to apply grouping, that will check for particular string "laptop" and sum all the Product Names containing string "laptop" under Laptops. Values in Product Name may vary, but there is a certain pattern like XX.Laptop.1, YY.Laptop.2 and mix of combinations to laptop.
I need something that will render full list, but add a group over Tablix Group with use of custom expression =IIF(Fields!Product_Name.Value)LIKE "laptop", "Laptops")
Please kindly advice. Thank you
Much thanks Alejandro! here is the result I got after applying your suggestion.
RESULT
I need two additional questions:
If there is a second group I would like to use for grouping - how to add it? (eg. desktops)
How to add a sumary value for the group? Shall it be done over exprerssion?
Thank you
Try using this expression for setting the group by Product Name and to show the Product Name in the respective cell.
=IIF(InStr(Fields!Product_Name.Value,"laptop") > 0,"Laptops",Fields!Product_Name.Value)
If for a specific row the Product Name contains laptop it will group on "Laptops" otherwise it will group by the Product Name value for that row.
UPDATE: If you want to check for another word to create an additional group you can use:
=Switch(
InStr(Fields!Product_Name.Value,"laptop") > 0,"Laptop",
InStr(Fields!Product_Name.Value,"desktop") > 0,"Desktop",
true,Fields!Product_Name.Value
)
Let me know if this helps.

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.

Percent of Group, not total

It seems like there are a lot of answers out there but I can't seem to relate it to my specific issue. I want to get the breakdown of yes/no for the specific Group. Not get the percent of the yes for the entire population of data.
I have tried the following code in the "What I'm Getting" % of Total cell =
=FormatPercent(Count(Fields!SessionID.Value)/Count((Fields!SessionID.Value), "Tablix1"),)
=FormatPercent(Count(Fields!Value.Value)/Count((Fields!SessionID.Value), "Value"),)
It should just be a case of changing the Scope in your expression to make sure the denominator is the total for the group, not the entire Dataset or Tablix, i.e. something like:
=Count(Fields!SessionID.Value) / Count(Fields!SessionID.Value, "MyGroup")
Where MyGroup is the name of the group, i.e. something like:
If this is still not clear, your best option would be to add a few sample rows, and your desired result for these, to the question so we can replicate your exact issue.
Edit after more info added
Thanks for adding more details. I have created a Dataset based on your example:
And I've created a table based on this:
The group is based on the Group field:
The Group % expression is:
=Fields!YesNoCount.Value / Sum(Fields!YesNoCount.Value, "MyGroup")
This is taking the YesNoCount value of each row and comparing it to the total YesNoCount value in that particular group (i.e. the MyGroup scope).
Note that I'm using Sum here, not Count as in your example expression - that seems to be the appropriate aggregate for your data and the required value.
Results look OK to me: