Modifying existing Crystal Reports to include additional filters - crystal-reports

I have an existing report that I didn't make. It is pretty complicated. It does not use the command function, so I cannot directly change the filters in the SQL code. I have reviewed several links that basically say that there is no way around this and that I will need to create a new command and redo the report. I would like to find a solution that doesn't involve me redoing the report.
My logic that I need is to only select subcontracts that only have one instance (regardless if approved or not), if they have two instances associated only choose the one that has been approved
This is the current backend Crystal SQL
SELECT columns
from several tables joins etc
WHERE subcontract = '12345'--parameter entry
AND company=1 --parameter entry
I would like to add this logic which works in a normal sql statement but since I can't modify Crystal's SQL I am stuck...
AND ((subcontract in (select subcontract from table
group by subcontract having count(*) = 1))
OR (subcontract in (select subcontract from table group by subcontract having count(*) > 1)
AND "approved = 'Y'))
I have looked at you the Select and Group Expert but I am just not getting there. Has anyone found some simple work-arounds?
Thanks!!!
edit: I already have a select expert that involves subcontract but I don't know how to add what I want to it since it is validation based and I am wanting to add a filter component to it..
(
if (IsNull({?BeginSub}) or Trim({?BeginSub})="") then
true
else
{subcontract} >= Trim({?BeginSub})
) and
(
if (IsNull({?BeginSub}) or Left(Trim({?BeginSub}),3) in ["","zzz"]) then
true
else
{subcontract} <= Trim({?EndSub})
)
Edit Update: I have found a way to get what I want in crystal reports but it throws an error when I run the report from my application. I create a command with the conditions that I want and then link to the other tables where command.subcontract=othertables.subcontract.

I found what I needed.. I added the following sql expression field,
(Select count(subcontracts) from subcontract table)
then I added the following to the select expert
if(({%SubContractCount} <>2) or ({%SubContractCount}>1 AND
approved = Y)) then true else false
Thank you!!

Related

Do ampersands work for user input in reports?

I have a working query that goes something like this:
Base table ACCT, link to others (listagg/subqueries) and ensure matching year("rollyear") field. Then the final step is to say which year ACCT table is in.
SELECT
FROM table ACCT
FULL OUTER JOIN table TABLE2 on ACCT.id = TABLE2.id and ACCT.rollyear= TABLE2.rollyear
...
FULL JOIN table TABLE7 on ACCT.id = TABLE7.id and ACCT.rollyear= TABLE7.rollyear
where ACCT.rollyear = extract (year from sysdate) +1
I typically use the calendar year plus one. I've been playing with using ACCT.rollyear = &rollyear to get user input... The limitation/issue I have is getting that running using the "User Defined Reports" feature.
Am I using this feature correctly? Does that only work for SQL Queries and not reports? Ive seen videos/comments about Stored Procedures and using "Accept...." but my attempts to mimic give errors and I'm not sure I'm on the right track. Thanks for any advice.
Change your query to use a bind variable:
where ACCT.rollyear = :rollyear
Then in the 'Binds' section you will see a variable with that name:
When you run your query you'll then be prompted to supply the value:

SSRS subquery based on other query

I'd like to make an overview of projects.
This contains some fixed info; table projects joined with some other tables.
Now the report needs to have some subqueries: how many sales per salesman, how many is in transit, inventory, ... and so on.
I have a tablix with grouping on project (as to create an excel with one worksheet per projet).
How would I go about executing a new subquery per project (f.e. select owner, sum(totalprice) from opportunities where project=xxx group by owner)
I know I could achieve this with subreports; but as I will have about 10 subreports, I was hoping I could solve this with extra datasets and some filtering (and thus keep all logic in one file).
What's the best way to achieve this?
I would create a dataset with this query:
select owner, project, sum(totalprice) as totalprice from opportunities group by owner, project
Next, in your tablix where you want to display owner and totalprice info, you will have an expression like this:
=LOOKUP(Fields!<FirstDataSetProjectFieldName>.Value, Fields!project.Value, Fields!owner.Value, "<NewDatasetName>")
The above code will send the value of the project you are searching for, match it with a the same field in your new dataset, then return the requested value from the new dataset. You can obviously do this for totalprice as well.
Check out the documentation for LOOKUP to get a better handle on it but I think this is the solution you are looking for.

Prevent Crystal Reports to select related fields to get only distinct items

I'm using Crystal Reports 14 with an Oracle DB. If I have related tables Authors and Books and I selects all the authors that have book with 'code' in their title Crystal will put Books.title in the SQL SELECT clause, even if I don't use any Books fields anywhere in the report (except for the record selection). The query looks like this:
SELECT "AUTHORS"."NAME", "BOOKS"."TITLE"
FROM "AUTHORS" INNER JOIN "BOOKS" ON "AUTHORS"."ID"="BOOKS"."AUTHOR"
WHERE "BOOKS"."TITLE" LIKE '%code%'
This causes a problem because, even if I use the "Distinct" option, I will get many entries for each author (if they have more then one book with 'code' in the title). But what I want is only one entry by author. "Distinct" do not work here because each entry is really distinct, their book title are different.
How can I avoid getting "BOOKS"."TITLE" in the SELECT clause like this:
SELECT DISTINCT "AUTHORS"."NAME"
FROM "AUTHORS" INNER JOIN "BOOKS" ON "AUTHORS"."ID"="BOOKS"."AUTHOR"
WHERE "BOOKS"."TITLE" LIKE '%code%'
If that's not possible, what's the best workaround to get distinct authors in my scenario?
UPDATE
I should add that currently I'm using this formula in the Suppress details section:
{AUTHORS.ID} = previous({AUTHORS.ID})
The problem with this solution and also the group solution proposed by #Beth is that the query is the same and the DB return too much data for no reason. In both solutions the data is filtered/regrouped in Crystal Reports, not in the DB.
can you group by authors in your report and suppress the books.title details?
to group by author, in your report, under the Insert menu, select 'Group...' then select author.id in the first combo box and click OK. You'll notice your report has changed to introduce Group Header and Footer sections above and below the detail section. You can suppress the detail section and only the authors will print.
If you want only the authors to come across, you'll need to use a command sent to the data source with the SQL you want used, instead of using the tables directly like you are now.
You can create a command as your back-end with your parameter in it. You'll use that parameterized command instead of the tables you're using now. Try creating the command without the parameter first, then after you get that working, you can add your existing parameter into the command SQL.

Can't remap fields - map fields window is missing new table

I have a Crystal Report with a database command:
The command has a join clause that can be removed and read from a table in the database, because it represents static data. I add this table (called _System) to the database expert:
Now I edit the command to remove the join and columns that reference this table. Since the report fields that depended on these columns are no longer mapped, this causes the Map Fields window to appear:
...which does not have the new table in it. If I cancel out of this I am back to where I originally was. If I hit OK without mapping, all of the unmapped fields on the report are deleted (suffice it to say... I was not expecting this >:( )
I have tried adding links between the command and the new table, and refreshing report parameters, but these have had no effect.
One workaround is to manually replace every field in the report, but this is very labour intensive.
Here is the outline of the command before:
SELECT ACT.Account_Code, ACT.Company, ACT.FName, --etc
STM.CompanyName AS 'DLRName', STM.Address_1 AS 'DLRAddress', STM.City AS 'DlrCity' --etc
FROM Accounts AS ACT
JOIN _System AS STM ON 1 = 1
GROUP BY ACT.Account_Code, ACT.Company, ACT.FName, --etc
STM.CompanyName, STM.Address_1, STM.City --etc
And after:
SELECT ACT.Account_Code, ACT.Company, ACT.FName, --etc
FROM Accounts AS ACT
GROUP BY ACT.Account_Code, ACT.Company, ACT.FName --etc
I have removed the JOIN on the _System table, and all referenced columns.
It appears to not be recognizing your _system table as a new source.
I would :
1) leave your command object SQL unchanged & get the issue worked out with the _System table, then
2) ensure that you are able to establish a join between the command object fields and the _System table fields, and lastly
3) then remap the fields.
Step two I suspect is the source of the problem, as your join condition is "ON 1 = 1" which I assume to mean that you may not have a common key field in both tables.
Note that your original command SQL selects STM.Companyname AS 'DLRName'.
Hence, crystal now know of a field called DLRName, but does not know of a field called CompanyName, hence it cannot make the association between DLRName in the old source, and CompanyName in the new source...
Likewise with the rest of the fields that are being moved from the command object to an attached table. if no name match exists...Crystal cant make the connection. However...it would list all unmatched fields that are on the report, and all unused fields in the recognized data sources, and allow you to specify the matches yourself.
But it does not...which tells me that something has gone wrong with the attempt to attach/open the _System table. Hence..you need to get that worked out first, then make the field adjustments.
If this doesnt get you thru...then show some sample data so I can see how the two tables are relating ( ensure some examples exists where there is a row match from both tables ).
I had the same problem a while ago.
Unfortunately I can't find anything online that helps, or maybe wasn't looking hard enough. I just noticed that in my case, that particular field that isn't showing in the map field dialogue box has nvarchar(max) as its datatype (in view).
I tried to force the datatype with CAST(missingfieldname as nvarchar(20)) as missingfieldname (I did this in the view), and voila, it magically appears in the map field dialogue box.
It seems that field mapping dialogue box aren't showing fields with blob texts.
I know this question was asked 4 years ago. But hopefully, this comment could help future solution seekers regarding this absurd and weird problem. I just got lucky seeing what's unique about that particular missing field.

SSRS - Data Driven Subscription -

What I'm trying to do:
I have a report already created that looks for something existing in one database and not in another. 99% of the time the report comes up empty. We do not need to know when there are no results to show. I only want to know when the query returns a result.
What I've done so far:
I have a the Data Source created and a table (view) created to where I can query for Subscriber information.
What I hope can be answered:
Is it all possible to have this report run and email my selected Subscribers only when there is data in the output?
I see you've already looked into Data-Driven subscriptions. You should be able to write your query in the data-driven subscription to test if the report should return results, and if not, send it to a dummy address, and only send it to your subscriber list if there will be data in it.
If you put the dummy address in your table with an IsDummy flag column, you could do something like this:
SELECT [EmailTo]
FROM SubscriptionTable
WHERE IsDummy=0
AND (SELECT COUNT(*) FROM SomeTable)>0 --report should have results
UNION ALL
SELECT [EmailTo]
FROM SubscriptionTable
WHERE IsDummy=1
AND (SELECT COUNT(*) FROM SomeTable)=0 --report should not have results
And that's only one way, there are probably lots of other ways that might suit your needs as well or better.