SQL and iReport - Aggregate functions in the Where clause and Parameters - sql

Well basically I would like to be able to use a parameter on iReport with an aggregate function.
If you type "yes" it will show you the values greater than 0, if you type "no" it will show the values that are less than 0. However, the aggregate function first adds up all the values related to an id and then it subtracts the result from another value, the result of that is the one I want to show.
How would I be able to do this? I'm clueless as I don't know how to use it with HAVING.

I don't understand what 'it' refers to in "I don't know how to use it with HAVING." The question will be much clearer with some SQL. But I guess you're looking for this:
SELECT id, sum(values) as the_agg
FROM table1
GROUP BY id
HAVING sum(values) $P!{BiggerOrSmaller} 0
The default value for the parameter BiggerOrSmaller should be like this:
$P{MyParam}.equals("yes") ? ">" : "<"
This assumes you have a parmeter called MyParam which can take the value "yes". Based on that value it sets the parameter BiggerOrSmaller appropriately.

Related

Which one of these two SELECT statment are correct?

I am a little bit confusing and have no idea which one of these two SELECT statments are correct
SELECT Value FROM visibility WHERE site_info LIKE '%site_is_down%';
OR
SELECT Value FROM visibility WHERE site_info = 'site_is_down';
SInce I run both of these I get same result, but I am interesting which one is correct since Value column is VARCHAR data type OR both of these SELECT are incorect ?
Result set running first SELECT
Value
1. 0
Result set running second SELECT
Value
1. 0
The two statements do not do the same thing.
The first statement filters on rows whose site_infos contain string 'site_is_down'. The surrounding '%' are wildcards. So it would match on something like 'It looks like site_is_down right now'.
The second query, with the equality condition, filters on site_info whose content is exactly 'site_is_dow'.
Everything that the second query is also returned by the first query - but the opposite is not true.
Which statement is "correct" depends on your actual requirement.
If both queries are useful for you, I'd use the second query, as it is the simplest, and runs faster.

Replace empty date values with a matching value from the same column?

I have a small query that looks like this:
SELECT
CS.ID,
CS.Import_Date,
CS.Secondary_Date
FROM
Center_Summary CS
ORDER BY CS.Import_Date
Which returns values like this:
And I want to replace these "empty" values which are pulling as 01/01/1900 with the value of 05/01/2019. In this case, it's because the ID and Import_Date match, so the Secondary_Date should match as well. I've thought to use REPLACE() (REPLACE(CS.Secondary_Date, '01/01/1900', ???), but I'm not sure how to write logic to pull in a matching value from the column Secondary_Date based on ID and Import_Date - what function should I be looking to use here?
How it's currently pulling (the dates in red I want to replace):
What my expected result is:
Why not just use a UPDATE with a WHERE?
UPDATE dbo.YourTable
SET SecondaryDate = ImportDate
WHERE SecondaryDate = '19000101';
"Empty" values are best represented by NULL. I would recommend converting them to NULL:
nullif(secondary_date, '1900-01-01')
If you really want another value, then you can use coalesce() or a case expression:
coalesce(nullif(secondary_date, '1900-01-01'), '2019-05-01')
However, I'm not generally a fan of such magic values in the code.

SSRS - Need to hide column inside a group

I have a requirement to hide a column, if no value exits in that column. But i have a grouping(Parent : Employee Number, Child: Category) in the report. In some of the group result may have value, but some of then does not have.
Example screen shot attached.
If you check the above image, second employee(Shiju) does not have Category. So for the second employee(Shiju) - need to hide Category column.
I tried with "Column Visibility" option and Column Groups > select Column > write expression in "Hidden" property. Following is the expr.
=iif(CountDistinct(Fields!Cat.Value) = 0,True,False)
These two options did not work.
Please give any solution for this.
Thanks in advance.
Your expression is almost correct, you just need to format the 0 else it will treat it as nothing:
=IIF(CountDistinct(Fields!cat.Value)=cint(0),true,false)
The other thing to check is that the field is actually null or nothing rather than blank strings or spaces.

Assign a minimum value for a column in an MS-Access query?

That is, if the returned value is below a set value, give the assigned minimum value. If this isn't possible without a data-modifying query, does anyone know of any VB function that I could compile and use?
Use the Immediate If function, which takes the following format:
IIf(expression, truepart, falsepart)
For example:
SELECT IIf(columnName < setValue, assignedMinimumValue, columnName) AS columnAlias
FROM tableName;

SQL Select to keep out fields that are NULL

I am trying to connect a Filemaker DB to Firebird SQL DB in both ways import to FM and export back to Firebird DB.
So far it works using the MBS Plug-in but FM 13 Pro canot handle NULL.
That means that for example Timestamp fields that are empty (NULL) produce a "0" value.
Thats means in Time something like 01.01.1889 00:00:00.
So my idea was to simply ignore fields containing NULL.
But here my poor knowlege stops.
First I thought I can do this with WHERE, but this is ignoring whole records sets:
SELECT * FROM TABLE WHERE FIELD IS NOT NULL
Also I tried to filter it later on like this:
If (IsEmpty (MBS("SQL.GetFieldAsDateTime"; $command; "FIELD") ) = 0 ; MBS("SQL.GetFieldAsDateTime"; $command; "FIELD"))
With no result either.
This is a direct answer to halfbit's suggestion, which is correct but not for this SQL dialect. In a query to provide a replacement value when a field is NULL you need to use COALESCE(x,y). Where if X is null, Y will be used, and if Y is null then the field is NULL. Thats why it is common for me to use it like COALESCE(table.field,'') such that a constant is always outputted if table.field happens to be NULL.
select COALESCE(null,'Hello') as stackoverflow from rdb$database
You can use COALESCE() for more than two arguments, I just used two for conciseness.
I dont know the special SQL dialect, but
SELECT field1, field2, value(field, 0), ...FROM TABLE
should help you:
value gives the first argument, ie, your field if it is NOT NULL or the second argument if it is.