I have an SSRS dataset query that has a SUM on a column of values, which represent decimal time of a persons absence, which the admin staff enter every time an absence is recorded for a student. In the uni database the staff seem to add absence records that also show hours that should be removed from a record, and they do this by simply putting a minus in front of the number. Is it possible to get this column to recognise the minus as a subtraction and remove it from the total? You can see at the bottom of the list a record was entered for 187.50 hours absent, but then another record added for that same absence period which noted that 150.00 of those hours should be subtracted (they fulfilled the shortfall in placement hours another way). Hope I've explained that query, have attached a screenshot of the dataset query. I'm not sure if I added the screenshot correctly. Thanks.
I think you can just add a CASE statement to check for the minus sign in the SUM and use 0 if it is a negative number.
CAST(dbo.srs_enl.enl_udfb AS DECIMAL(27, 2))
would become
CAST(CASE WHEN LEFT(dbo.srs_enl.enl_udfb, 1) = '-' THEN 0 ELSE dbo.srs_enl.enl_udfb END AS DECIMAL(27, 2))
or you could use Boolean logic, multiplying the value by 0 if it's less than 0:
CAST(dbo.srs_enl.enl_udfb AS DECIMAL(27, 2)) * CASE WHEN CAST(dbo.srs_enl.enl_udfb AS INT) < 0 THEN 0 ELSE 1 END
I assume you would want to see the final two records appearing as a single record with 37.50 in the final column? If that's correct then it looks like your grouping by too many columns.
You seem to be grouping by
GROUP BY
clause GROUP BY
clause GROUP BY
clause If you remove the 3 columns from the group by clause the sum should 'just work' as expected.
One more thing... Try to alias table names, it will make you SQL much easier to read.
So something like
SELECT
dbo.TableA.columnA, dbo.TableB.columnB
FROM dbo.TableA
JOIN dbo.TableB on dbo.TableA.myKeyColumn = dbo.TableB.myKeyColumn
ORDER BY dbo.TableA.columnA
using aliases would look like this
SELECT
a.columnA, b.columnB
FROM dbo.TableA a
JOIN dbo.TableB b on a.myKeyColumn = b.myKeyColumn
ORDER BY a.columnA
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.