1

So I have the below case in crystal reports

Main Report which has a query that shows the batch details of an invoice Sub-report which shows the details of the batches included in the main report but are not included in that document. So for example

  • Invoice A has 10 items all with the same batch (this will show in the main report)
  • Invoice B, C, an D has 3 more items in the same batch (so total 13 items for that batch in 4 different documents) and the 3 items that are not in document A is what appears in the sub-report.

Now it's easy to achieve that in Crystal Reports because you can link sub-reports with fields to filter directly without linking the queries, however since CR is not a database engine the filtration is super slow and to solve that I need a solution in an SQL query:

I tried converting it to UNION ALL so it's

Main Report
Union ALL
Sub Report

The problem is I am unable to replicate the sub-report case since the grouping needs to be by Document then Batch and the 3 items from the sub-report are not on the same document so they are not grouped by the same dimension now and that ruins the report.

I know it's not logical but after trying all the solutions that came to my mind I thought maybe someone more experienced would have a different opinion, basically for every invoice in the main report I need to show all items that are not in that specific invoice so right now I have no links because the Item is not the same, the document is not the same, and only the batch is the same but that's a many to many link.

EDIT: For simplicity let's say this is the main query

SELECT 

T0.DocumentNumber,
T2.ItemCode,
T2.ItemName,
T3.BatchNumber,
T3.Quantity 

FROM DocHeader T0 

JOIN DocRows T1 ON T0.DocumentEntry = T1.DocumentEntry
JOIN ItemMaster T2 ON T1.ItemCode = T2.ItemCode
JOIN BatchTransaction T3 ON T1.Document Entry = T3.DocumentEntry and
T3.DocumentType = T1.DocumentType and T3.DocumentLine = T1.DocumentLine 

The report is grouped by Document Number, so what i need is the same query but for every other document number that is not in the main query joined on the main query. The 2 problems are 1. They can leave the document number parameter blank and select all the documents in the table so this process needs to happen for each document seperately. 2. the document number in the sub query will never be the same so the grouping is an issue because the report needs to be grouped on the main query document number.

Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
  • https://meta.stackoverflow.com/questions/271055/tips-for-asking-a-good-structured-query-language-sql-question – Digvijay S Mar 18 '20 at 07:21

1 Answers1

0

It would help if you wrote out your main query and some of the secondary queries you've tried, but assuming I understand your issue correctly, you should be able to just create an additional JOIN on the Batches table. This will have your SQL query list off ALL the Item rows for the Batch, then you can try to use conditional formatting and/or grouping in Crystal to to show/hide the rows as needed.

Alternatively, using the query described above, you may be able to use the linked Invoice Numbers you're now getting from your main report query to drive your Sub-Report, as opposed to going by the Batch # -- this may result in better performance.

EDIT: Based on your comment and update, I think the right approach here is to create a Stored Procedure. Doing a single query is probably not going to work, I took a crack at it and it's not worth the hassle.

Using a Stored Procedure you can create a table variable in memory, add the main invoice records to the table, then add the records for the secondary Invoices linked by the Batch number. Use some kind of column/flag in your table that indicates whether it's a 'Main Invoice' record or not, and use this flag to drive the Crystal display logic for how you want to show the main vs secondary/linked invoices.

It's doable!

Overhed
  • 1,289
  • 1
  • 13
  • 41
  • added in the main post. – user3338096 Mar 19 '20 at 07:41
  • Batch to Batch is many to many because i could have multiple lines in the main invoice and multiple lines in the sub. A many to many link would work for grouping meaning i can group them all by the main document if i link m2m but i still won't be able to flag sub and main because all data will come against each other. – user3338096 Mar 23 '20 at 06:51