Flask-SQLAlchemy - Greater than or equal to - sqlalchemy

I'm having trouble figuring out how to do a "greater than or equal to" comparison in a query.
I have a model field:
invoicedate = db.Column(db.Date(), nullable=True, key='InvoiceDate')
And i'm trying to do the following filter:
Invoice.query.filter_by(invoicedate >= date.today()).count()
When I run the view, it keeps throwing the following error:
NameError: global name 'invoicedate' is not defined
What is the correct syntax for a greater than or equal filter in sqlalchemy or flask-sqlalchemy?

You want filter, not filter_by:
Invoice.query.filter(Invoice.invoicedate >= date.today())
See this answer for more on filter vs filter_by

Related

data type mismatch error in criteria expression-ms access

I am using the following query:
select Containers.SalesOrderNumber,
GetWeightInMT(Sum([NetWeight]), WeightUOM) as Expr1,
CInt(Round(GetWeightInMT(Sum([NetWeight]), WeightUOM), 0)) as WtMt,
SalesOrders.CustomerID,
SalesOrders.SalesOrderID,
SalesOrders.UnitPrice,
SalesOrders.Quantity,
SalesOrders.SalesCommission,
SalesOrders.LatestShipDate,
SalesOrders.PortOfDischarge,
SalesOrders.PlaceOfDelivery,
SalesOrders.LowerTolerancePct,
SalesOrders.UpperTolerancePct,
SalesOrders.Grade,
(SalesOrders.Quantity - WtMt) as OpenQty
from Containers
inner join SalesOrders on Containers.SalesOrderNumber = SalesOrders.SalesOrderNumber
group by Containers.SalesOrderNumber,
Containers.WeightUOM,
SalesOrders.CustomerID,
SalesOrders.SalesOrderID,
SalesOrders.UnitPrice,
SalesOrders.Quantity,
SalesOrders.SalesCommission,
SalesOrders.LatestShipDate,
SalesOrders.PortOfDischarge,
SalesOrders.PlaceOfDelivery,
SalesOrders.LowerTolerancePct,
SalesOrders.UpperTolerancePct,
SalesOrders.Grade;
and then calculate the sum of "OpenQty" in my textbox.
However i get error "Data type mismatch in criteria expression".
Please help.
Thankyou
You cannot use a calculated field (in this case WtMt) as part of the calculation of another field. You have to re-use the entire calculation.
(SalesOrders.Quantity - WtMt) as OpenQty
to
(SalesOrders.Quantity - CInt(Round(GetWeightInMT(Sum([NetWeight]), WeightUOM), 0))) as OpenQty
That however may not be the only problem. I recommend running our query for one calculated field at a time to see which one actually gives you the error.

How to implement Having Clause in SSRS

I'm New to SSRS.
Im trying to create a report where i need to group by [DATA Flag] column which is working fine ,but once the data is grouped i need to set the DATA FLAG ="TotalCancellations" and there is another column CancellDays which i need to set it as <120 .
I tried
Option 1:-
So to achieve this i have added TWO filters one with
Expression : DATA FLAG
Operator =
and Value as TotalCancellations
and the other filter as follows
Expression : Cancelldays
Operator =
and Value as < 120
But its not working and giving empty result,i have records with Cancelldays <120
Option 2 :-
Right click on Group and in General Tab ,Group on Expression as below ]
Fields!DFlag.Value = "TotalCancellations" AND Fields!DFlag.Value <120
which didnt work :(
this is similar to writing having clause in SQL i believe but im not getting how to implement that here in SSRS.
i can add in SQL Query but its already a huge query with lot of unions so please suggest me if there is any way i can implement here in SSRS
Im using Matrix in SSRS 2008
Adjusting the syntax for your option 1 is probably the easiest solution. In the Group Properties, under the Filters section, enter these two filters:
Expression: [DFlag] (Text)
Operator: =
Value = TotalCancellations
Expression: =Sum(Fields!CancelDays.Value) [enter this in the expression builder] (Integer)
Operator: <
Value: 120
Putting all the filters in a single expression, like your option 2, can be useful if you need to filter by one criteria OR another.
Below are the filer expressions
and this is how group by is
The filter should be implemented at the tablix level, not at the group level.

error : nested aggregation that specifies a dataset scope

I have been working on ssrs and tried to do the following statement. I am using data from 2 datasets and there is no way I can merge them into one (I tried that when I first had this error). Code:
=Sum(iif(Fields!Year.Value = Max(Fields!Year.Value) - 1 , Sum(Fields!PersonCount.Value, "RetCust"), 0))
This code gives the error: The Value expression for the text box has a nested aggregate that specifies a dataset scope. Inner aggregates cannot specify a dataset scope.
I have also tried a different variation of this code which is as follows:
=(iif(Fields!Year.Value = Max(Fields!Year.Value) - 1 , Sum(Fields!PersonCount.Value, "RetCust"), 0))
I just got rid of the sum at the beginning. But this time I had a different error: Report item expressions can only refer to fields within the current dataset scope or, if inside an aggregate, the specified dataset scope. Letters in the names of fields must use the correct case
Hope someone can help :)
Cheers
Your expression is a bit wrong.
=Sum(iif(Fields!Year.Value = Max(Fields!Year.Value) - 1 , Sum(Fields!PersonCount.Value, "RetCust"), 0))
Your Sum is outside and is covering the two fields which is not on the same dataset. You should assign Sum for each of them since they are from different dataset.
Try this:
=iif(Sum(Fields!Year.Value) = Max(Sum(Fields!Year.Value)) - 1 , Sum(Fields!PersonCount.Value, "RetCust"), 0)
Note: I haven't tested the Max(Sum(.... if that's correct.

SSRS Count or Sum expression

I cannot work out why these Total expressions don't work...
I am trying to add any cells that have a date later than today, with any cells that have "Not Reqd", and then divide that by the number of rows, to get a percentage.
All I'm getting is #Error.
These are the expressions I've tried:
=SUM(IIf(Fields!Jetter_Trng.Value >Today OR
Fields!Jetter_Trng.Value = "Not Reqd",1,0)))/(Count(Fields!Jetter_Trng.Value)
and
=Count(IIf(Fields!Jetter_Trng.Value >Today OR
Fields!Jetter_Trng.Value = "Not Reqd",1,Nothing)))/(Count(Fields!Jetter_Trng.Value)
The "Not Reqd" string has come from an expression that changes a date (01/01/1950) to "Not Reqd". Maybe this is messing things up:
=iif(Fields!Jetter_Trng.Value = "01/01/1950", "Not Reqd", Fields!Jetter_Trng.Value)
The current working expression (not looking for "Not Reqd") is:
=COUNT(IIF(Fields!Jetter_Trng.Value>Today,1,Nothing)))/(Count(Fields!Name.Value))
I'm a bit lost...
A couple of notes on your expression as it stands at present
Jetter_Trng appears to be a string representing either a date or “Not Reqd”. You can’t compare strings to dates without casting them to a date type first using CDATE()
The number of braces (( and )) do not match
The root of your problem though is that you are using Jetter_Trng to return either a Date, or the value “Not Reqd”.
When SSRS attempts to evaluate an expression it does it all at the same time. It doesn’t follow a path to find the answer, and ignore other paths. Therefore, when you are attempting to compare
Fields!Jetter_Trng.Value >Today
This is comparing a string to a date, and throwing the error, as this mean nothing
"Not Reqd" > Today
You won’t be able to do all that you want to using only one Field of type string.
Your options are to
Use two fields – the date and a flag indicating not required, or
Use one field – but have an “invalid date” (01/01/2100 perhaps) that you could then treat as the “Not Reqd” value, and check if the current date is less than that (which it always will be)
Using the second option here you could then use the following expression to create the desired calculation
=sum(iif(CDate(Fields!Jetter_Trng.Value) > Today, 1, 0)) /
Count(Fields!Jetter_Trng.Value)
Which would evaluate this dataset as follows

Change Case in SSRS 2013

I am using a parameter to change the background color of my field when the field string contains the parameter string .
I have used IndexOf, Contains, and instr. All three work, however they are all case sensitive. (i.e. when I search 'Dol' Dollar Tree and Doldrum are highlighted but not Sandolski etc.)
It is not the stored procedure, the correct records display, however the SSRS functions are what is my challenge.
I have tried toLowerInvariant but was receiving an error Help please.
As I was writing this I checked my error and learned the issue..
I assumed the syntax was to pass my string as a parameter:
toLowerInvariant(Parameters!Param1.Value)
However the correct toLowerInvariant syntax is (in ssrs):
Parameters!Param1.Value.toLowerInvariant()
An explaination on toLowerInvariant can be found here: string.ToLower() and string.ToLowerInvariant()
And also I have found that this comparison is best done with a Switch (if you are comparing multiple parameters to the field).
I have not noticed a performance impact between Field.IndexOf(#Param), Field.Contains(#Param), or Field.Instr(#Param)
My final code:
=switch(
instr(Fields!Client.Value.toLowerInvariant(),Parameters!Client_Firm.Value.toLowerInvariant()) >= 1, "Cornsilk",
instr(Fields!Client.Value.toLowerInvariant(),Parameters!KeyWord.Value.toLowerInvariant()) >= 1, "Cornsilk"
)