I'm getting error when I use this formula in SSRS 2017:
Operator '/' is not defined for types 'integer' and 'System.TimeSpan'
Operator '*' is not defined for types 'System.TimeSpan' and 'System.TimeSpan'
=IIF(
100 / (DateAdd("d", -(Day(Today)), Today) -
DateSerial(Year(DateAdd("m", -1, Now())), 1, 1)
)
* ((Today - DateSerial(Year(DateAdd("m", -1, Now())), 1, 1)) / 100) > 1,
1,
100 / (DateAdd("d",-(Day(Today)), Today) -
DateSerial(Year(DateAdd("m", -1, Now())), 1, 1)
)
* ((Today - DateSerial(Year(DateAdd("m", -1, Now())), 1, 1)) / 100)
)
Use DateDiff() instead of -:
=IIF(
100 / DateDiff("d", DateAdd("d", -Day(Today), Today),
DateSerial(Year(DateAdd("m", -1, Now())), 1, 1)
)
* (DateDiff("d", Today, DateSerial(Year(DateAdd("m", -1, Now())), 1, 1)) / 100) > 1,
1,
100 / DateDiff("d", DateAdd("d",-(Day(Today)), Today),
DateSerial(Year(DateAdd("m", -1, Now())), 1, 1)
)
* DateDiff("d", Today, DateSerial(Year(DateAdd("m", -1, Now())), 1, 1)) / 100
)
Related
I am not an MySQL Professinal, but my query works fine
SELECT r.id,
/* Total repurchase price per Ton
* (Quantitiy/t * repurchase price/dry) + Extra Costs
*/
(
SUM((rc.menge / 1000) * (rw.preis / (IF (rw.is_zement = 1, 1.25 , 1)) / (1 - (IFNULL(rw.h2o, 0) / 100)))) +
SUM((rc.menge / 1000) * pr.preis) +
IF ((r.extra_1_type > 0), IF ((r.extra_1_type = 1), extra_1 , extra_1 * SUM(rc.menge / 1000)) , 0) +
IF ((r.extra_2_type > 0), IF ((r.extra_2_type = 1), extra_2 , extra_2 * SUM(rc.menge / 1000)) , 0) +
IF ((r.finanzierung_satz > 0) AND (r.finanzierung_monate > 0) AND SUM(rc.menge / 1000),
((r.finanzierung_satz / 100) / 12) * r.finanzierung_monate * (
SUM((rc.menge / 1000) * (rw.preis / (IF (rw.is_zement = 1, 1.25 , 1)) / (1 - (IFNULL(rw.h2o, 0) / 100)))) +
SUM((rc.menge / 1000) * pr.preis) +
IF ((r.extra_1_type > 0), IF ((r.extra_1_type = 1), extra_1 , extra_1 * SUM(rc.menge / 1000)) , 0) +
IF ((r.extra_2_type > 0), IF ((r.extra_2_type = 1), extra_2 , extra_2 * SUM(rc.menge / 1000)) , 0)
)
, 0) ) as repurchase_price,
/* Profit
* (Quantitiy/t * Price) - repurchase_price
*/
(SUM(rc.menge) / 1000 * r.vk) - (
SUM((rc.menge / 1000) * (rw.preis / (IF (rw.is_zement = 1, 1.25 , 1)) / (1 - (IFNULL(rw.h2o, 0) / 100)))) +
SUM((rc.menge / 1000) * pr.preis) +
IF ((r.extra_1_type > 0), IF ((r.extra_1_type = 1), extra_1 , extra_1 * SUM(rc.menge / 1000)) , 0) +
IF ((r.extra_2_type > 0), IF ((r.extra_2_type = 1), extra_2 , extra_2 * SUM(rc.menge / 1000)) , 0) +
IF ((r.finanzierung_satz > 0) AND (r.finanzierung_monate > 0) AND SUM(rc.menge / 1000),
((r.finanzierung_satz / 100) / 12) * r.finanzierung_monate * (
SUM((rc.menge / 1000) * (rw.preis / (IF (rw.is_zement = 1, 1.25 , 1)) / (1 - (IFNULL(rw.h2o, 0) / 100)))) +
SUM((rc.menge / 1000) * pr.preis) +
IF ((r.extra_1_type > 0), IF ((r.extra_1_type = 1), extra_1 , extra_1 * SUM(rc.menge / 1000)) , 0) +
IF ((r.extra_2_type > 0), IF ((r.extra_2_type = 1), extra_2 , extra_2 * SUM(rc.menge / 1000)) , 0)
)
, 0)
) as profit,
/* Profit Percentage
* Profit / (repurchase_price / 100)
*/
((SUM(rc.menge) / 1000 * r.vk) - (
SUM((rc.menge / 1000) * (rw.preis / (IF (rw.is_zement = 1, 1.25 , 1)) / (1 - (IFNULL(rw.h2o, 0) / 100)))) +
SUM((rc.menge / 1000) * pr.preis) +
IF ((r.extra_1_type > 0), IF ((r.extra_1_type = 1), extra_1 , extra_1 * SUM(rc.menge / 1000)) , 0) +
IF ((r.extra_2_type > 0), IF ((r.extra_2_type = 1), extra_2 , extra_2 * SUM(rc.menge / 1000)) , 0) +
IF ((r.finanzierung_satz > 0) AND (r.finanzierung_monate > 0) AND SUM(rc.menge / 1000),
((r.finanzierung_satz / 100) / 12) * r.finanzierung_monate * (
SUM((rc.menge / 1000) * (rw.preis / (IF (rw.is_zement = 1, 1.25 , 1)) / (1 - (IFNULL(rw.h2o, 0) / 100)))) +
SUM((rc.menge / 1000) * pr.preis) +
IF ((r.extra_1_type > 0), IF ((r.extra_1_type = 1), extra_1 , extra_1 * SUM(rc.menge / 1000)) , 0) +
IF ((r.extra_2_type > 0), IF ((r.extra_2_type = 1), extra_2 , extra_2 * SUM(rc.menge / 1000)) , 0)
)
, 0)
))
/
((
SUM((rc.menge / 1000) * (rw.preis / (IF (rw.is_zement = 1, 1.25 , 1)) / (1 - (IFNULL(rw.h2o, 0) / 100)))) +
SUM((rc.menge / 1000) * pr.preis) +
IF ((r.extra_1_type > 0), IF ((r.extra_1_type = 1), extra_1 , extra_1 * SUM(rc.menge / 1000)) , 0) +
IF ((r.extra_2_type > 0), IF ((r.extra_2_type = 1), extra_2 , extra_2 * SUM(rc.menge / 1000)) , 0) +
IF ((r.finanzierung_satz > 0) AND (r.finanzierung_monate > 0) AND SUM(rc.menge / 1000),
((r.finanzierung_satz / 100) / 12) * r.finanzierung_monate * (
SUM((rc.menge / 1000) * (rw.preis / (IF (rw.is_zement = 1, 1.25 , 1)) / (1 - (IFNULL(rw.h2o, 0) / 100)))) +
SUM((rc.menge / 1000) * pr.preis) +
IF ((r.extra_1_type > 0), IF ((r.extra_1_type = 1), extra_1 , extra_1 * SUM(rc.menge / 1000)) , 0) +
IF ((r.extra_2_type > 0), IF ((r.extra_2_type = 1), extra_2 , extra_2 * SUM(rc.menge / 1000)) , 0)
)
, 0)
) / 100)
as profit_percentage,
FROM recipe as r
LEFT JOIN recipecomponent as rc ON r.id = rc.recipe_id
LEFT JOIN rawmaterial as rw ON rc.rawmaterial_id = rw.id
LEFT JOIN press as pr ON r.press_id = pr.id
GROUP BY r.id
ORDER BY lieferdatum desc
As you can see, to calculate price, profit, profit percentage, I use often
the same parts... Is it possible to shorten my SQL Query? To make it more
'elegant' :)
Thank you & Best Regars Simon
As a generic answer, if you repeat caculations in your query, you can use derived tables to avoid repetition:
select d.a, d.b, d.a*d.b
from (
select x+y+z as a, d+e+f as b
from yourtable
) as d;
This is probably a noob question, but i am a little stuck.
I have this long SELECT query :
SELECT
dbo.Dagplanning.GeldigOp ,
dbo.Trips_Bus.Route ,
dbo.Trips_Bus.TripStart AS BeginUur ,
dbo.Places.Naam AS BeginPlaats ,
dbo.Trips_Bus.TripEnd AS EindUur ,
dbo.Trips_Bus.Duty AS Dienst ,
dbo.Trips_Bus.Block ,
dbo.Personeel.Personeelsnummer ,
dbo.Personeel.Naam ,
dbo.Personeel.VoorNaam ,
dbo.Trips_Bus.OpDay ,
dbo.Trips_Bus.PeriodeID
FROM
dbo.Dagplanning ,
dbo.Personeel ,
dbo.Trips_Bus ,
dbo.Places
WHERE
dbo.Trips_Bus.TripStart >= DATEADD(minute, DATEDIFF(minute, -5, GETDATE()), 0)
AND dbo.Trips_Bus.TripStart <= DATEADD(minute, DATEDIFF(minute, 300, GETDATE()), 0)
AND dbo.Personeel.Personeelsnummer = dbo.Dagplanning.Personeelsnummer
AND dbo.Trips_Bus.Duty = ( dbo.Dagplanning.Rol + '+' + REPLICATE('0', 3 - LEN(dbo.Dagplanning.Dienst)) + dbo.Dagplanning.Dienst )
AND dbo.Trips_bus.Opday LIKE '%" + opdag + "%'
AND dbo.Trips_Bus.PeriodeID = '" + periodeid + "'
AND dbo.Dagplanning.GeldigOp = DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0)
AND dbo.Trips_Bus.TripFrom = dbo.Places.Place
ORDER BY
dbo.Trips_Bus.TripStart ASC
It's actually this part that I have a problem with:
WHERE
dbo.Trips_Bus.TripStart >= DATEADD(minute, DATEDIFF(minute, -5, GETDATE()), 0)
AND dbo.Trips_Bus.TripStart <= DATEADD(minute, DATEDIFF(minute, 300, GETDATE()), 0)
This returns nothing, no error but also no data. De data in the table is stored like this example : 21:50:00
Any idea what I am doing wrong? Please be gentle ;-)
Oh, I am completely new at this, so please keep that in mind!
I got a Query which cuts off domain suffixes for every row, e.g. google.com -> google or google.co.uk -> google
The query is as follows
SELECT id,domain,
CASE
WHEN LENGTH(domain) - LENGTH(REPLACE(domain, '.', '')) = 1 THEN REVERSE(SUBSTRING(REVERSE(domain), LOCATE('.', REVERSE(domain)) + 1, 1000))
WHEN LENGTH(domain) - LENGTH(REPLACE(domain, '.', '')) = 2 THEN REVERSE(SUBSTRING(REVERSE(REVERSE(SUBSTRING(REVERSE(domain), LOCATE('.', REVERSE(domain)) + 1, 1000))), LOCATE('.', REVERSE(REVERSE(SUBSTRING(REVERSE(domain), LOCATE('.', REVERSE(domain)) + 1, 1000)))) + 1, 1000))
END as Keydomain
FROM sites
Now I want to display all modified domains that occur more than once. How can I do that? Thanks for helping me out ;)
Just add
GROUP BY Keydomain
HAVING COUNT(*) > 1
to your query.
EDIT:
Could you tell me if there is a way to list the complete domains one by one with your addition?
SELECT * FROM
(
SELECT
CASE
WHEN LENGTH(domain) - LENGTH(REPLACE(domain, '.', '')) = 1 THEN REVERSE(SUBSTRING(REVERSE(domain), LOCATE('.', REVERSE(domain)) + 1, 1000))
WHEN LENGTH(domain) - LENGTH(REPLACE(domain, '.', '')) = 2 THEN REVERSE(SUBSTRING(REVERSE(REVERSE(SUBSTRING(REVERSE(domain), LOCATE('.', REVERSE(domain)) + 1, 1000))), LOCATE('.', REVERSE(REVERSE(SUBSTRING(REVERSE(domain), LOCATE('.', REVERSE(domain)) + 1, 1000)))) + 1, 1000))
END as Keydomain
FROM sites
GROUP BY Keydomain
HAVING COUNT(*) > 1
) d1
INNER JOIN
(
SELECT id, domain,
CASE
WHEN LENGTH(domain) - LENGTH(REPLACE(domain, '.', '')) = 1 THEN REVERSE(SUBSTRING(REVERSE(domain), LOCATE('.', REVERSE(domain)) + 1, 1000))
WHEN LENGTH(domain) - LENGTH(REPLACE(domain, '.', '')) = 2 THEN REVERSE(SUBSTRING(REVERSE(REVERSE(SUBSTRING(REVERSE(domain), LOCATE('.', REVERSE(domain)) + 1, 1000))), LOCATE('.', REVERSE(REVERSE(SUBSTRING(REVERSE(domain), LOCATE('.', REVERSE(domain)) + 1, 1000)))) + 1, 1000))
END as Keydomain
FROM sites
) d2
ON d1.Keydomain = d2.Keydomain
The following code returns a NaN in situations where there are no records. How do I prevent this from being displayed in the report? A 0 would be preferred.
=FormatNumber(
((
(Code.NullSafeSplit(Fields!AvgLOSC1.Value, 0) * Code.NullSafeSplit(Fields!AvgLOSC1.Value, 1)) +
(Code.NullSafeSplit(Fields!AvgLOSC2.Value, 0) * Code.NullSafeSplit(Fields!AvgLOSC2.Value, 1)) +
(Code.NullSafeSplit(Fields!AvgLOSC3.Value, 0) * Code.NullSafeSplit(Fields!AvgLOSC3.Value, 1)) +
(Code.NullSafeSplit(Fields!AvgLOSC4.Value, 0) * Code.NullSafeSplit(Fields!AvgLOSC4.Value, 1)) +
(Code.NullSafeSplit(Fields!AvgLOSC5.Value, 0) * Code.NullSafeSplit(Fields!AvgLOSC5.Value, 1))
) / (
CInt(Code.NullSafeSplit(Fields!AvgLOSC1.Value, 1)) +
CInt(Code.NullSafeSplit(Fields!AvgLOSC2.Value, 1)) +
CInt(Code.NullSafeSplit(Fields!AvgLOSC3.Value, 1)) +
CInt(Code.NullSafeSplit(Fields!AvgLOSC4.Value, 1)) +
CInt(Code.NullSafeSplit(Fields!AvgLOSC5.Value, 1))
))
, 0)
I think the formula below should get it. I haven't tested this, so I might be missing a parenthesis. The problem is likely coming from a divide by zero when all entries are null. This catches that and sets the divisor to 1 in that case.
=FormatNumber(
(
(Code.NullSafeSplit(Fields!AvgLOSC1.Value, 0) * Code.NullSafeSplit(Fields!AvgLOSC1.Value, 1)) +
(Code.NullSafeSplit(Fields!AvgLOSC2.Value, 0) * Code.NullSafeSplit(Fields!AvgLOSC2.Value, 1)) +
(Code.NullSafeSplit(Fields!AvgLOSC3.Value, 0) * Code.NullSafeSplit(Fields!AvgLOSC3.Value, 1)) +
(Code.NullSafeSplit(Fields!AvgLOSC4.Value, 0) * Code.NullSafeSplit(Fields!AvgLOSC4.Value, 1)) +
(Code.NullSafeSplit(Fields!AvgLOSC5.Value, 0) * Code.NullSafeSplit(Fields!AvgLOSC5.Value, 1))
)
/
(
IIF((
CInt(Code.NullSafeSplit(Fields!AvgLOSC1.Value, 1)) +
CInt(Code.NullSafeSplit(Fields!AvgLOSC2.Value, 1)) +
CInt(Code.NullSafeSplit(Fields!AvgLOSC3.Value, 1)) +
CInt(Code.NullSafeSplit(Fields!AvgLOSC4.Value, 1)) +
CInt(Code.NullSafeSplit(Fields!AvgLOSC5.Value, 1))
) = 0,
1,
(
CInt(Code.NullSafeSplit(Fields!AvgLOSC1.Value, 1)) +
CInt(Code.NullSafeSplit(Fields!AvgLOSC2.Value, 1)) +
CInt(Code.NullSafeSplit(Fields!AvgLOSC3.Value, 1)) +
CInt(Code.NullSafeSplit(Fields!AvgLOSC4.Value, 1)) +
CInt(Code.NullSafeSplit(Fields!AvgLOSC5.Value, 1))
)
)
, 0)
I hope this can clearly explain what I am looking for. I have searched read through a few articles on this site, but haven't found what I am looking for. I have also spent close to 3 hours trying to figure this out on my own.
I am trying to count the number of records and SUM the WorkTime. Here is my query I have been working with.
SELECT Log.User
, sum(if(hour(endtime) = 0, 1, 0)) AS Midnight
, sum(if(hour(endtime) = 1, 1, 0)) AS `1AM`
, sum(if(hour(endtime) = 2, 1, 0)) AS `2AM`
, sum(if(hour(endtime) = 3, 1, 0)) AS `3AM`
, sum(if(hour(endtime) = 4, 1, 0)) AS `4AM`
, sum(if(hour(endtime) = 5, 1, 0)) AS `5AM`
, sum(if(hour(endtime) = 6, 1, 0)) AS `6AM`
, sum(if(hour(endtime) = 7, 1, 0)) AS `7AM`
, sum(if(hour(endtime) = 8, 1, 0)) AS `8AM`
, sum(if(hour(endtime) = 9, 1, 0)) AS `9AM`
, sum(if(hour(endtime) = 10, 1, 0)) AS `10AM`
, sum(if(hour(endtime) = 11, 1, 0)) AS `11AM`
, sum(if(hour(endtime) = 12, 1, 0)) AS `12PM`
, sum(if(hour(endtime) = 13, 1, 0)) AS `1PM`
, sum(if(hour(endtime) = 14, 1, 0)) AS `2PM`
, sum(if(hour(endtime) = 15, 1, 0)) AS `3PM`
, sum(if(hour(endtime) = 16, 1, 0)) AS `4PM`
, sum(if(hour(endtime) = 17, 1, 0)) AS `5PM`
, sum(if(hour(endtime) = 18, 1, 0)) AS `6PM`
, sum(if(hour(endtime) = 19, 1, 0)) AS `7PM`
, sum(if(hour(endtime) = 20, 1, 0)) AS `8PM`
, if(hour(endtime) = 20, sec_to_time(sum(time_to_sec(endtime) - time_to_sec(starttime))), 0) AS `8PM Time`
, sum(if(hour(endtime) = 21, 1, 0)) AS `9PM`
, sum(if(hour(endtime) = 22, 1, 0)) AS `10PM`
, sum(if(hour(endtime) = 23, 1, 0)) AS `11PM`
FROM
(
SELECT user
, controlnumber
, starttime
, endtime
, timediff(endtime, starttime) AS Worktime
FROM
atrtaxcert.ordertimeworked
) AS Log
GROUP BY
Log.User;
These start and end times are only minutes apart.
Any guidance is much appreciated. This is my first post here, and was not able to provide any images to help describe.
If starttime and endtime are TIME datatypes, then use the TIME_TO_SEC function and do a subtraction. Total up the seconds, and then convert the total to a string representation.
SELECT `Log`.`User`
, ...
, SUM(HOUR(`Log`.endtime)=20) AS `8PM_count`
, SUM(IF(HOUR(`Log`.endtime)=20,work_seconds,0) AS `8PM_seconds`
, SEC_TO_TIME(SUM(IF(HOUR(`Log`.endtime)=20,`Log`.work_seconds,0) AS `8PM_hhhmmss`
, ...
FROM ( SELECT
, TIME_TO_SEC(endtime)-TIME_TO_SEC(starttime) AS work_seconds
) `Log`
GROUP
BY `Log`.`User`
NOTE: this:
SELECT HOUR(endtime)=0 AS foo
is shorthand equivalent to
SELECT IF(HOUR(endtime) = 0, 1, 0) AS foo
If starttime and endtime are DATETIME values, the you can use the TIMESTAMPDIFF function to calculate the difference in seconds:
SELECT `Log`.`User`
, ...
, SUM(HOUR(`Log`.endtime)=20) AS `8PM_count`
, SUM(IF(HOUR(endtime)=20,TIMESTAMPDIFF(SECOND,`Log`.starttime,`Log`.endtime),0) AS `8PM_seconds`
, ...
FROM (
) `Log`
GROUP
BY `Log`.`User`
(You probably want to ignore the values returned when e.g. starttime = '23:59:00' and endtime = '00:01:00', and that would require another conditional test.)