How to create a single query with multiple where columns and value are optional - filter

for example,
select * from a where id=#id and date between #date1 and #date2
when i passed #id=1 #date1='09/29/17'and #date2='09/30/17'
then an output is correct
When I passed #id=0 (means no id found in a table) #date1='09/29/17'and #date2='09/30/17' then output comes between date and ignore id column
means I want the filters like a shopping website

The way that you would accomplish this would be to use an OR on the ID column, to leave your SQL looking like:
SELECT *
FROM a
WHERE (#id=0 OR id=#id)
AND date BETWEEN #date1 AND #date2
The thing you need to be careful and aware of using this type of approach is that depending on your DBMS you will likely end up in a scenario where the execution is optimized for whatever the first execution path took. If it's a very big table, this could end up causing problems.

Related

How to make groups in an input and select a specific row in each of them in Talend?

I am working on a Talend transformation process (we are using Talend 6.4).
, and I don't know how to implement the current requirement.
I have an input consisting in :
Two columns that are my group keys (Account and Product), but are not unique (the same Account x Product couple can happen in multiple rows)
A criterion column (Contract end date), which will help me decide which row I want to keep for each group
Some "tail" data that need to be passed to the following step of the processing (the contract number)
The rule to implement is:
Keep only one record per group
The selected record must be one with no end date or, if all have end date, with the biggest end date
The selected record can be random in case there is a tie
See the transformation applying those rules on some dummy data:
I thought first to do the following:
sort by Account, Product, End_date (nulls first)
"select first" in each group
but I am not skilled enough to know whether the second transformation exists in Talend.
Regards,
Pierre
Very interesting Talend question.
You need to create something like this job.
here a link to the zip file to import in your Talend
The answer from #MBDIA seem to be working, however I would like to share what we did to fulfill our requirement.
See our Talend process here:
The first tMap (tMap_3) acts like a tReplicate and a tMap, and sends:
in the upper branch only the Account and Product references, that are then deduplicated by the tAggregateRow_1.
in the lower branch all data and computed fields that enables us to take care of the case where the date is missing (instead of defaulting to 31/12/9999, we compute a flag (0 or 1) that we use in the sort step afterwards).
In the second part of the process, we first apply the sort to the whole data on Account, Product, Empty date flag (computed before), End date (desc) and use a second tMap to make a join on both branches (on Account x Product), only keeping First Match in order to keep the first record as per our requirement.

Number filed is not processed correctly

I use Oracle Apex, I want that my 'display only field' is updated automatically. Well, when I use dynamic actions like this select 5 * price from ... or, for instance, random values, it works absolutely correctly, the field is filled with the value 5 * price (or set new random value). But when I use select :P4_COUNT * price from, the filed is empty. I think that the problem in :P4_COUNT (it is a number field) but I do not know what to do.
In the Dynamic Action, look for "Items to Submit" (usually under the SQL or PL/SQL code). Put the names of items that need to be submitted to session state prior to running the code. Also, note that currently, all values in session state are strings. So it's probably best to use to_number if you need a number.

SSRS Dropdown 'Any' Value not working

new to this world so looking for help with what I think wold be a simple thing to fix, however me and the guy who is training me on all that is SQL and SSRS cannot figure this out
I have a report within SSRS and SQL which is working perfectly bar one thing
I have a drop down list parameter which has all our customers names, and the report shows volumes of what that customer has obtained so far etc, and for an individual customer, this works perfectly. However, when trying to see the total volumes by choosing 'Any' from the drop down list, it returns no data, rather than returning everything
Can anyone please advise what I could be missing here, or what I need to show you to help resolve this issue
Cheers
Liam
Assuming you Stored Proc parameter is varchar and represents either customer names or an 'Any' value then the following should work.
SELECT myField1, myField2 -- etc
FROM myTable t
WHERE (t.ClientName = #myParameterName OR #myParameterName = 'Any')
Optionally Please Note: Personally I don't use SPs and usually just put the code to grab the data in the dataset. Some companies don't like you doing this but if you are able to do this I think this makes life easier.
If you can put the stored proc code directly in your dataset query then you can make the report more flexible. You can change your parameter to be MultiValue, you don't need and 'Any' value added to your parameter list either and then you can simply do something like
SELECT myField1, myField2 -- etc
FROM myTable t
WHERE t.ClientName IN(#myParameterName)
SSRS will take all the selected parameter values and inject them into the dataset query correctly, so there is nothing else you need to do. SSRS will also add a 'Select All' option to your parameter in case you want to gran data for everything. The report will work for 1, 2, 10 or all client names.

Crystal Reports - Sorting two different date fields chronologically

I've got two date fields from two tables and I'm trying to show receipts of POs in line with work order consumption sorted chronologically.
Is there any way to sort two date fields together?
For instance:
1/1/14 work order date
1/5/14 work order date
1/7/14 PO receipt date
1/9/14 work order date
1/20/14 work order date
The two fields are 'duedate' from table 'porel' and 'reqdate' from table 'jobmtl'
Usually the simplest solution in such cases is to perform the ordering at the server side (e.g. using SQL Server stored procedure, Access query, etc.), and then use the stored procedure or query as the source for the data.
An alternative that I read about is to create global variables in the report, assign your dates values to these variables using 'WhilePrintingRecords;' in formula fields, and using these variables that then does the actual reporting for you.
Slightly complicated.
Another solution which I am not sure if applies to you is :
Click on the main menu > Report > Record Sort Expert
Select your date field in the box on the left and add it to the box on the right
Check the Ascending checkbox and click Ok
Let us know how it goes.
you should create a formula saying
if (table1.duedate = null) then
{table2.duedate}
else
{table1.duedate}
Then sort on this formula. Check the syntax yourself.

Generate reports in birt using user input. When input is null, everything should be fetched otherwise corresponding data should be shown

I have to create a birt report with user input parameters. It is something like when the para
meter is left blank it should fetch all values from a table otherwise when the user inputs the students roll no.,the corresponding data should be fetched. Can this be done through Birt report? If yes, then please suggest a way.
Thanks!
Yes, you can do that. If the parameter is optional you can't use the Dataset Parameter (with a ? in your query), because it will be null. Instead you have to modify your query using JavaScript.
Create a Report Parameter like usual, in this case 'stud_no'. Then add a comment in your SQL that you are reasonably sure is unique, I use something like --$stud_no$, wherever you want your clause inserted.
Then add a script like this to your Data Set, in beforeOpen:
if (params["stud_no"].value){
this.queryText = this.queryText.replace("--$stud_no$", "and stud_no = " + params["stud_no"]);
}
This replaces the comment with the clause when the parameter has a value. You can use regex in the search string, and then you can also insert it multiple places if you want.
Create your paremater using a like statement
where students_roll_no like ?
Create your report paramater using as a text box, with a defualt value of %
Because the percent '%' is the SQL wildcard, it returns all values. If the user enters a Student Roll number, it returns only that record. Additionally the user can enter 0500% and get all the records that begin 0500.

Resources