Computed Column Formula and how it can work base on condition - sql-server-2008

I have this challenge. I want to achieve a formula in a computed column. Currently my formula is: ((((([sponsorID]+' ')+substring(CONVERT([varchar],datepart(year,[DateCreated]),(0)),(3),(4)))+'-')+'0')+CONVERT([varchar],[ID],(0)))
Where: sponsorID, DateCreated, ID are columns in my table PatientInfo.
A sample of what the formular produce is PVT 11 10567
My ID column auto increment
Now The total no of record in my table called PatientInfo is getting to 500,000. and the formula i was given is [SPONSORCODE YEAR - 4 DIGIT]. It must not exceed 4 digit!
How do i achieve this formula in such a way that the id stops when a year ends and start afresh in a new year. i.e. patient folder no can be PVT 10 0128 ....... and PVT 11 0128. this is because in a year the total no of new patients will not exceed 4 digits. aside from the existing patients.
PLEASE Kindly assist. i hope i have provide a clear picture of my challenge. if there is a better way to achieve this, please assist.
awaiting your reply.

Would it work to use the last 4 digits of ID? For the ID part of the expression, you could use:
RIGHT('000' + CONVERT(varchar,[ID]),4)
Of course, you could get duplicates if you ever inserted more than 9999 rows in a particular year.

You can't using a computed column based on ID
You'd need a separate incrementing column per year and per sponsor that allows only 0000-9999. Then, use this in your computed column.
In the next version of SQL Server, you can use a sequence. Until then, you could use a sequence emulator: http://blogs.msdn.com/b/sqlcat/archive/2006/04/10/sql-server-sequence-number.aspx

Related

Calculate difference between multiple group columns in SSRS Report

First post in stackoverflow - so forgive me if I don't get this right. I have an SSRS report with a matrix which groups Quantity, Total Spend and Average Cost by part number for each year. Part Number and description down the left and the the years along the top. I want to add a delta column in each year after the first year which shows the change in price from the previous year. I have seen examples of how to get the difference between first and last year - but not the difference between each year and the previous one. I have hidden the delta column for the first year - no would love some guidance on how to calculate the difference in cost.
So from the image below the first row in the delta column under FY2019 would show -€0.01 as price has reduced from 0.59 to 0.58.
You haven't explained how if Avg. Cost is calculated in your report but I'm assumming it's just something like =SUM(Fields!Spend.Value) / SUM(Fields!Qty.Value)
You've not shown your report design so I can't tell what your row/column groups are called but I'll assume you have rowgroup by Part and one by Year. Let's assume your column group name is PeriodID so it's the same as my demo report.
In this case you can use the PREVIOUS() function to get the previous values to compare against.
I set up a similar report to demonstrate. As you can see in my case the year column is grouped using a columngroup called PeriodID
The expressions highlighted are as follows.
Price. This is just Cost/Qty =SUM(Fields!Cost.Value)/SUM(Fields!Qty.Value)
lastprice. This is just for illustration and debugging. It uses the PREVIOUS() fucntion to get the previous year's data
=Previous(Sum(Fields!Cost.Value), "PeriodID") / Previous(Sum(Fields!Qty.Value), "PeriodID")
Note that "PeriodID" is the name of the column group (case sensitive). This is scope for the previous function so it know to get data from the previous column group.
delta. This is the final output. It's just the current "Price" expression minus the "lastprice" expression.
=(Sum(Fields!Cost.Value) / Sum(Fields!Qty.Value)) - (Previous(Sum(Fields!Cost.Value), "PeriodID") / Previous(Sum(Fields!Qty.Value), "PeriodID"))
The final output looks like this (I've not hidden the column for the first column group or hidden last price, just so you can see it working). Also, if you see slight differences in the delta column compared to what you might expect, it's just down to the fact that the numbers are formatted to 2 decimals but my test data is up to 14 decimals.

How do I replace values in a column in KNIME?

I have a column of countries with 50 different values that I want to reduce to United States and Other.
Can someone help me with that?
Another example is Age which has 48 values that I'd like to reduce to only 4 like 1 to 18 = youth, 18-27 = starting, etc.
I've actually got about 5 columns that I want to reduce the values of. So would I need to repeat the process multiple times in KNIME or can I accomplish multiple column value replacements at once?
The latter on can easily be achieved with the Rule Engine
$Col0$ > 1 AND $Col0$ <18 => "youth"
For the First problem I'd use a String Replace (Dictionary).
I don't think you replace all at once but you can loop over columns.
For the second case I would use Numeric Binner:
For each column a number of intervals - known as bins - can be
defined. Each of these bins is given a unique name (for this column),
a defined range, and open or closed interval borders. They
automatically ensure that the ranges are defined in descending order
and that interval borders are consistent. In addition, each column is
either replaced with the binned, string-type column, or a new binned,
string-type column is appended.

SSRS percentage of a total with column groups

I apologize in advance if this is not enough info or if it is confusing (This is my first post to the forum and it's a little hard to explain what i'm trying to do). I have been researching this for a couple days now. This article comes really close to what I need, but this person did not have column groups, just a a single column with an output: SSRS percentage of a total.
Consider this layout and groups:
The idea is to provide the percentage of the total records per PrimaryPayor per Company and then break it out by Year-Month.
This expression:
=CountDistinct(Fields!ID.Value, "Primary_Payor") / CountDistinct(Fields!ID.Value, "Company")
Results in this output (The first image is my output, the second is the desired output):
As you can see, the percentages do NOT group by Year-Month, instead just provides the total percentage of IDs per Payor per Company and repeats it for every Year-Month column. How can I get the percent of Total IDs per payor, company, and month as it shows in the second result image?
After editing my question, I thought of something I hadn't tried.
If I just use =CountDistinct(Fields!ID.Value), this gives the total count for each payor, company, month, plus, I added a Total row.
Then, if I reference those textbox values (instead of trying to calculate the percentage on the fly) divided by the total row, I get my results. I just had to add a column inside of the column group. I should then be able to just Hide my "total" column and display only the percentages.
=ReportItems!Textbox50.Value / ReportItems!Textbox35.Value
Here is the output (with the total counts and then after hiding the "count" column):
Final Results
Try:
=CountDistinct(Fields!ID.Value) / CountDistinct(Fields!ID.Value, "Company")
I removed the Primary_Payor scope leaving the function calculate the count in the cell scope Primary_Payor and DOS_YrMo.
UPDATE:
=CountDistinct(Fields!ID.Value) / CountDistinct(Fields!ID.Value, "DOS_YrMo")
Let me know if this helps.

How can I sum "Expr" in bottom of the table in SSRS?

I'm doing such a thing;
=Sum(Fields!TOTAL.Value) - Sum(Fields!IND.Value)
In cells, so this is end up with expresion "<<'Expr'>>", that means I'm finding net worth price - discount for each saled products, after that I want to sum all of those product's sales on bottom, like
=Sum(Sum(Fields!TOTAL.Value) - Sum(Fields!IND.Value) )
Any chance for that? I also tried like;
=Sum(ReportItems!TOTALBOX.Value)
But not work either.
Edit;
I'm doing something like that but on final, I want to sum those values which "<>" created.
Thx.
Since your data is in a Column Group (by [DEFINITION_] - the 3 columns with the line above them) you can create a total column using the same formula. The Total will SUM all the different DEFINITION groups for each [NAME] group.
You can add a column Outside the group and create your total column with the same formula
=Sum(Fields!TOTAL.Value) - Sum(Fields!IND.Value)
If you created everything correctly (and you're lucky), SSRS might be able to create the Total for you by right clicking on the cell and Add Total.

How can I add an aggregate percentage to an SSRS report based on two specific columns in a matrix?

I am somewhat new to SSRS and I have built a matrix report that looks fairly simple:
Supplier [AccDate]
[SupplierName] Sum(PurchasingCredit)
Total <<Expr>>
The actual report shows month-by-month sales data for a year in addition to Current YTD and Previous YTD columns (joined via UNION clause in base query) in addition to a column showing Year-over-Year % as difference between the Current YTD and Previous YTD columns (also joined via union).
I was able to apply a dynamic format to the Year-over-Year column using the following expression in the Format property to show the YOY column as a percentage and the others as dollar amounts:
=IIF(Fields!AccDate.Value <> "YOY", "'$' #,0.00;'$' -#,0.00", "#,0.00 %;-#,0.00 %")
The last thing I need to do is to show the column totals, which are working for each column with the exception of the year-over-year percentages using the following:
=IIF(Fields!AccDate.Value <> "YOY", SUM(Fields!PurchasingCredit.Value), 0)
In the year-over-year (YOY) column, a sum of percentages doesn't make sense: I need it to recalculate this value based on the sums of the "CYTD" and "PYTD" columns, but I can't seem to figure it out. Any help would be greatly appreciated. I am guessing it is probably an application of scope, but the MS articles that attempt to explain this are very confusing.