SQL ignore all blank fields in calculation - sql

I am using the following query in SQL within PhpRunner:
SELECT
[Date],
(MondayStrengthEnd1Sets*MondayStrengthEnd1Reps*MondayStrengthEnd1Distance) + (MondayStrengthEnd2Sets*MondayStrengthEnd2Reps*MondayStrengthEnd2Distance) AS Total
FROM Running
When I run the query I get a blank for an answer. Some of the fields will not necessarily be filled in for every record. The example above is just a snippet of all the fields that is in the table and in the complete calculation, there will be almost 90 fields in total. All the fields are from the same table.
What can I add to the query to treat the blank fields as blanks and not as zeros in order to still calculate the total despite some fields not being filled in? If there is anything that will do it automatically for all the fields it would be great.
I am aiming for something like this:

It looks like you need something like:
SELECT
[Date],
isnull(MondayStrengthEnd1Sets*MondayStrengthEnd1Reps*MondayStrengthEnd1Distance, 0)
+ isnull(MondayStrengthEnd2Sets*MondayStrengthEnd2Reps*MondayStrengthEnd2Distance, 0)
AS Total
FROM Running

Related

VBA - Running Total sum in dynamic range keeps disappearing

So I have 4 columns of values in a table, Income, Expenditure, AIncome and AExpenditure.
I'm trying to get a running total in the final column Total. This the formula I have so far, which works:
=SUM(INDEX([Income],1):[#Income], INDEX([Expenditure],1):[#Expenditure], INDEX([AIncome],1):[#AIncome], INDEX([AExpenditure],1):[#AExpenditure])
My problem is that I don't know how to keep the sum in the Total column whenever I reload the table in Excel. The number of rows can vary from 1 to a few hundred.
Does anyone have any advice?
Thanks
An image of what the table looks like. THis is just a part with values in each column. J,K,L,M are Income, Expenditure, AIncome and AExpenditure. N is the total column. It's normally blank until I manually put in the formula.
Screenshot of part of the table
I can reload this table to get different values from the query I'm using, but that's not important. the problem is when I fetch the query again, there's a new table, always the same amount of columns, but not the same amount of rows. And then I need to manually put in my formula again in the total column
I would store the formula in a variable and after the query is loaded, copy the formula to the rows in the rnning total column.
Something like:
Const StoredFormula as string = "your formula"
Worksheets(x).range("range of the query table").formula = StoredFormula

Access 2016 - SQL Sum Query Doesn't Provide Sum, No errors

Goal / Problem
What I am trying to do is fairly simple. I have two fields, InvoiceNbr and TransAmt. In the table, there may be multiple transactions for each invoice. So I'm simply trying to sum all transactions for each invoice number so I'll have one transaction sum for each invoice number (which I can later subtract from a balance to get the remaining balance). Sounds simple, and typically is... but I'm having an issue (or bug? that DOES NOT result in an error)!
My Data
Here is a query that will give a snippet of my data:
SELECT [_MasterLog_Transactions].InvoiceNbr, [_MasterLog_Transactions].TransAmt
FROM _MasterLog_Transactions
GROUP BY [_MasterLog_Transactions].InvoiceNbr, [_MasterLog_Transactions].TransAmt
HAVING ((([_MasterLog_Transactions].InvoiceNbr)="16081702014741")
AND (([_MasterLog_Transactions].TransAmt)>0));
This results in:
InvoiceNbr TransAmt
16081702014741 $1,096.73
16081702014741 $3,668.25
If I add a 'totals' row using the Access GUI, and choose "SUM", it correctly calculates "$4,764.98". And for other random reference, in the _MasterLog_Transactions table, TransAmt is a 'number' field, field size 'double', and format 'currency' -- so it should be capable of being used with the SUM function.
My Query
So working from the above data, I created the query:
SELECT [_MasterLog_Transactions].InvoiceNbr,
Sum([_MasterLog_Transactions].TransAmt) AS MyTest
FROM _MasterLog_Transactions
GROUP BY [_MasterLog_Transactions].InvoiceNbr
HAVING ((([_MasterLog_Transactions].InvoiceNbr)="16081702014741"));
My Query Results
InvoiceNbr MyTest
16081702014741 $1,096.73
I would have expected:
InvoiceNbr MyTest
16081702014741 $4,764.98
No errors occur. It runs and Access seems to think it is outputting the proper result.
Am I overlooking something really simple here, or is this an Access bug I've never run into? Any help would be greatly appreciated! Thanks.
Please check for space in the InvoiceNbr.
You can verify that this is an InvoiceNbr issue by adding COUNT(*) to your query.
I think the equivalent query would look more like this:
SELECT t.InvoiceNbr,
Sum(t.TransAmt) AS MyTest
FROM _MasterLog_Transactions as t
WHERE t.InvoiceNbr = "16081702014741" AND
t.TransAmt > 0
GROUP BY t.InvoiceNbr;
I'm guessing your real summarized data looks like:
InvoiceNbr TransAmt
16081702014741 $1,096.73
16081702014741 $3,668.25
16081702014741 -$3,668.25
That is the condition [_MasterLog_Transactions].TransAmt > 0 is filtering out key values.

How to retrieve a part of a value in a column

Correction - I only need to Pick the WORK value every result set in the column will contain comma seperated values like below..
"SICK 0.08, WORK 0.08" or "SICK 0.08,WORK 0.08"
I only need to pick WORK 0.08 from this.
I am quite new to SQL
I am using the following script to get some results;
select Work.Work_summary, Work.emp_id
from Work
all work fine. but the first column has values like the following :
WORK 08.57, SICK 08.56 (Some columns)
SICK 07.80, WORK 06.80 , OT 02.00 (Some columns)
How can i only retrieve the column with only the WORK% value, if there is no WORK value the results shall be empty.
select Work_summary, emp_id
from Work
where Work_summary like '%WORK%'
This will return the rows in the Work table where Work_summary column contains the word WORK. See the documentation for more details.
Contains is faster than like.
SELECT Work_summary, emp_id FROM Work WHERE CONTAINS(Work_summary, 'WORK');
then use: this will give only the result where work summary contains work content.
select Work.Work_summary, Work.emp_id
from Work where contains(work.Work_summary ,'work');
select replace(Work_summary,",","") as work_summary
from Work
where upper(Work_summary) like '%WORK%'

SQL - How to insert a subquery into a query in order to retrieve unique values

I am writing reports using Report Builder 3, and I need some help with an sql query to get unique values.
Using the following sample data:
I need to be able to get one single value for feeBudRec returned for each feeRef. The value of each feeBudRec is always the same for each individual feeRef (eg for every data row for feeRef LR01 will have a feeBudRec of 1177).
The reason why I need to get a single feeBudRec value for each feeRef is that I need to be able to total the feeBudRec value for each feeRef in a feePin (eg for feePin LEE, I need to total the feeBudRec values for LR01 and PS01, which should be 1177 + 1957 to get a total of 3134; but if I don't have unique values for feeBudRec, it will add the values for each row, which would bring back a total of 11756 for the 8 LEE rows).
My experience with writing SQL queries is very limited, but from searching the internet, it looks like I'll need to put in a subquery into my SQL query in order to get a single unique feeBudRec figure for each feeRef, and that a subquery that gets a minimum feeBudRec value for each feeRef should work for me.
Based on examples I've found, I think the following subquery should work:
SELECT a.feeRef, a.feeBudRec
FROM (
SELECT uvw_EarnerInfo.feeRef, Min(uvw_EarnerInfo.feeBudRec) as AvailableTime
FROM uvw_EarnerInfo
GROUP BY
uvw_EarnerInfo.feeRef
) as x INNER JOIN uvw_EarnerInfo as a ON a.feeRef = x.feeRef AND a.feeBudRec = x.AvailableTime;
The problem is that I have no idea how to insert that subquery into the query I'm using to produce the report (as follows):
SELECT
uvw_EarnerInfo.feeRef
,uvw_EarnerInfo.PersonName
,uvw_EarnerInfo.PersonSurname
,uvw_EarnerInfo.feePin
,uvw_RB_TimeLedger.TimeDate
,uvw_RB_TimeLedger.matRef
,uvw_RB_TimeLedger.TimeTypeCode
,uvw_RB_TimeLedger.TimeCharge
,uvw_RB_TimeLedger.TimeElapsed
,uvw_WoffTimeByTime.WoffMins
,uvw_WoffTimeByTime.WoffCharge
,uvw_EarnerInfo.feeBudRec
,uvw_EarnerInfo.personOccupation
FROM
uvw_RB_TimeLedger
LEFT OUTER JOIN uvw_WoffTimeByTime
ON uvw_RB_TimeLedger.TimeId = uvw_WoffTimeByTime.TimeId
RIGHT OUTER JOIN uvw_EarnerInfo
ON uvw_EarnerInfo.feeRef = uvw_RB_TimeLedger.feeRef
WHERE
uvw_RB_TimeLedger.TimeDate >= #TimeDate
AND uvw_RB_TimeLedger.TimeDate <= #TimeDate2
If that subquery will get the correct results, can anyone please help me with inserting it into my report query. Otherwise, can anyone let me know what I will need to do to get a unique feeBudRec value for each feeRef?
Depends on the exact schema, but assuming the uvw_EarnerInfo lists the Pin, Ref, and Rec without duplicates, try adding an extra column (after personOccupation) on the end of your query such as :
feeBudRecSum = (Select SUM(FeeBudRec) From uvw_EarnerInfo x
where x.feePin = uvw_EarnerInfo.feePin
Group By x.FeePin)
Note that you would not Sum these values in your report. This column should have the total you are looking for.
The key to Report Builder is to get your query correct from the offset and let the wizard then structure your report for you. It takes all the hard work out of structuring your report manually.
I haven't used Report Builder for a while now but in the query builder of the report displaying the graphical representation of your query you should be able to drag and drop columns in and out of the query set. Dragging a column upwards and out of the box (showing your columns) would have the effect of causing your report to break on this column.
If you restructure this way you will probably have to run the report generator again to regenerate the report and restructure it.
Once you are happy with the structure you can then begin to add the summary columns.

Sum of distinct values in field SSRS 2005

I'm working on SSRS report builder that is using a dataset calling a SQL Server 2000 database.
The query is getting sums of a few different fields and is also pulling out all records that have to do with that client number. I want to get the sum of the sum but it is way over because of the detail rows. Basically what I want is the sum of the distinct sum column values.
=Sum(Fields!tot.Value, "table1_Group3")
I saw that you can get sums by the groups and I tried the expression above but it comes back with an error:
The Value expression for the textbox 'tot' has a scope parameter that is not
valid for an aggregate function...
table1_Group3 is the name of the group that holds the sum value in the report.
Any suggestions on how to get the distinct values to sum them in this report.
=Sum(Fields!tot.Value, "table1_Group3")
The code above will give you the sum of "tot" for all rows in the current "table1_Group3." This means that this expression only makes sense somewhere within table1_Group3. Otherwise, SSRS doesn't know which is the current instance of that group.
Sounds like you would like to sum this value across multiple groups, but only take one "tot" from each instance of the group. (Are you sure that all rows in that group will have the same "Tot?")
If tot is the total of other fields in your returned data, then simply add those up in your formula. This may have the added benefit of simplifying your SQL query as well.
Some other options that could work:
- Change your SQL query so that only one row per group gets the Tot field set.
- Use Embedded code in the report to keep a running total which is added to only once per group, such as in the group header.
(If upgrading to 2008R2 SSRS is an option, then the Lookup function could be used here, maybe even to look back at the same dataset.)
change the query/ dataset to sum(distinct tot) using the temp table on the sql server
I suppose you need to write sum(distinct columnName).