SQL Server - Mapping table to identify fields to sum - sql-server

I have a large database of fields around 400, that need to be summed in various different ways.
Currently I do this in Excel with a look up table to identify which field to sum and then use a sum if formula to then sum those columns. How would I replicate this in SQL?
I've seen some examples where you manually type out each field to calculate but this seems very impractical if you are summing up to 300 fields and if there are any changes to that mapping table, then we would have to redo that.
So far my only solution is to copy and paste all the fields to be calculated in excel, and add the correct summation syntax and paste back in to SQL.
Thanks

Lots of ways of doing this;
create a case statement which acts like your sumif and pass the conditions in the case e.g. CASE WHEN amount >=100 THEN SUM(Amount) END as 'New Column'
Create a function and store all your logic in the function similar to above but easy to maintain should your logic change.
if you post your sumif's with a sample I can help you.

Related

Dynamic SQL SSRS and Dynamic Row Grouping

I have a dynamic SQL query that have dynamic column names. ( The resultset is then pushed into a tablix very similar to this setup(https://www.youtube.com/watch?v=BB9wvvkDY7Y ).It works fine. However, I need to establish a grouping on the row in SSRS. Due to the dynamic nature, the fieldname is not available before run time and therefore I cannot set a group on the row. I have tried several workarounds, using parameters like :
=JOIN(Parameters!DynamicGroup.Value, ", ")
(it is a multivalued field), but I cannot seem to get this working. At best, I get 1 row returned.
I may not be clear on this, but what I need is to be able to use the grouping when exporting to excel to have each group create a new sheet in the workbook.
Any help would be appreciated.
Thanks for the replies. I figured out what why I am only getting back a single row. It is because of the way the dynamic query is pivoting the resultset and then grouping it to display all columns(marked with number) as a row. So all fields with the same number becomes a row. When I add another grouping it is taking this qrouping, hence there will only be one row because it is unique(pivoted).
This leads me to think that I cannot get this done this way. Is there a way that I can have dynamic column names (based on Dates)? There will always be the same number of columns returned, but the content will different. I googled this, but I have not found anything that really solves this issue.

Can I Fix the a lookup field in Access database based upon a calculation or another field?

I am trying to create a database with field descriptions for a very large excel file that I have at work. I have created 3 tables- List of sheets, list of variables(including a lookup field pointing at the List of sheets table, so that I can select the sheet to which the variable belongs), and a third table which specify some validation rule.
In this third table, I want to see two lookup fields, one specifying the sheet in which the rule applies(say, 'Select Sheet'), and another specifying the variable(say, 'Select Variable'). I can point to the two different tables, but I want to do something a bit more nuanced than that. When I give a particular sheet name to 'Select Sheet', I want the lookup field for the variable('Select Variable') to show me only those variables which exists in that sheet.
I know that there probably will be solutions using forms, but this database is going to be very detailed and there are things to do afterwards, so I do not want to get into queries and forms before all data has been recorded in tables in a neat manner.
I have a good grasp of VBA in the context of excel and I am given to understand that I can extend the applications of Access using VBA. I am ready to do that, but I want to see before that whether this is some functionality of access that I am missing. Had anyone done anything similar before, and if so, did it take VBA to do it?

Finding difference between columns in column group

I am using report builder to create a report showing a budget for a project. The dataset includes line items for both budget and projected. See below for example rows. I am using a matrix with column group to display budget and projected side by side as well as a row group to show section, category, etc. I need to have a variance column that subtracts projected from budget.
I have scoured the interwebs for solutions but nothing that has worked so far. I feel like there has to be simple solution to this given it is something that could be done in a sql query with zero effort. Most solutions are assuming I have two separate fields, but these are dynamic fields pull out with the column group.
Dataset Row Samples
Type Section Cateogry Phase Task Total
Budget Building Kitchen Pre-Construction Cabinet Hardware $100
Projected Building Kitchen Pre-Construction Cabinet Hardware $220
Report sample
COL GROUP This is the column i want
Budget Projected Variance
+Buidling $100 $220 -$120
+Kitchen
+Pre-Con
EDIT: I tried the below solution without success and have already visited every link provided in the second answer. Maybe there is something I am missing, but I ended up just doing everything in the SQL query and not use Column groups. This is 100% the simplest solution. I am very surprised there is no easy way to reference individual columns in a column group. The below may work for others, but I just could not get them to work for me. Not sure why.
You could add an additional column inside the “Type” group (provided that this is the name of your column group). Set the Column Visibility to hide the column by an expression like
= IsNothing(Previous(Field!Type.Value, “Type”)
Calculate the values for that column as
= Previous(Sum(Fields!Total.Value), “Type”) – Sum(Fields!Total.Value)
That should calculate the difference between the values of the previous type and the current type, and
only show that column for the "Projected" type (when there is a previous type).
On the matrix, you can use the group subtotals to achieve this, you only have to overwrite the SUM operation with an expression that subtract to values. There are many link mentioning how to do that or that can helps you:
How to add calculated column from dynamic columns to a matrix
Adding subtotals to SSRS report tablix
How to write Expression to subtract row Group SubTotals
Reporting in SQL Server – Using calculated Expressions within reports

Creating a Table to cater and calculate SUMPRODUCT equivalent

I have the following table in sql as per below. Users capture for each KPIID an actual an budget for each period.
From this table i need to calculate a similar result as which i have done in Excel using SUMPRODUCT, but i dont know what the equivalent sql query would look like to obtain these results.
I would like to obtain a result like this which i have made an example in excel

Query SQL database from Excel

I'm attempting to create a MS Query to return data from a SQL database based on a value from a cell in Excel. I have actually successful accomplished this, but only for 1 row. I cant figure out how to get it to copy-down to other rows.
I've created a connection as follows:
Notice that the SQL statement includes a parameter. The parameter is set to point to a specific cell:
I guess this makes sense as I'm only looking to return 1 value per row:The problem is that I have multiple lines to return values for. How do I return a value per row for multiple rows?
I've tried changing the cell reference in the Parameters dialog box, but this does not work as the Excel Table is designed to grow dynamically.
Excel data connections works in a way that every connection has only one SQL Query. So in order to do what you'r looking for, you will need to have many connections, and that's not the "best practice".
However, there are two ways you can solve this situation:
1. Make a single connection with all of the data and create a pivot table based on it. Then use VLOOKUP/INDEX to gather the data to your requested cells.
2. If the data is too big, you can use VBA code to create a smaller Query based on the cells you mentioned and then continue as described on the first option.
Good luck.

Resources