Table alias and datatables - crystal-reports

I am triying to fill a report with datatables. The report has two tables which are
he same but with diferent alias. In c# I create a datatable for one of them with the records that must be shown and another datatable with the other table info. Crystal only gets info from the first datatable to fill the two tables so the shown data is wrong. Can I fill the two tables that are the same but diferent alias with two diferent datatables?
This is the same problem that I wrote yesterday but simplier.
Thanks for all,

Crystal Reports does support duplicate tables with aliases.
The problem may be the link. If your trying to display the two tables on the report both there's no JOIN or WHERE statement.
The table would need a link with itself. Here's a link to a tutorial on join a table to itself
select * from tbl1 as child join tbl1 as parent on parent.primarykey = child.foreignkey;

Related

How to join the tables in Tableau as they are joined in ER diagram

I want to join tables in tableau but, when I am adding any table to join it is getting joined with the 1st table I added irrespective of keys. Even if tables do not have same keys table is getting joined.
Example: Sales to be joined with OnlineSales and Customer to be joined with OnlineSales but Customer is getting joined with Sales.
I tried using custom SQL as well but it is not working.
Click on the Venn diagram icon for the join and set the left and right keys as desired. You can choose different tables for each join key; you aren't limited to the ones listed first. After you save your join key choices, the diagram will update to show which tables are involved.
No need for custom SQL. In fact, save custom SQL for the rare situation where Tableau gives you no other choice because Custom SQL prevents useful query optimizations.

T-SQL, Get distinct values from column in source, check target, insert if not exist

I've seen several somewhat similar questions, but nothing exactly like mine. A T-SQL god should be able to answer this is a flash.
Source table (feeder) of employees and department codes from HRMS system feed. I have an employees table and a departments table in my SQL Server database. I need to have a stored proc that will first get a distinct list of department codes in the feeder table, then check those codes to see if they exist in the departments table. If they don't then insert into the departments table from the feeder table. Then, do the insert into the employee table.
Right now I have found that one of the business analysts has been getting separate list of departments in Excel and adding them manually. Seems crazy when the data is already coming into the feeder table from HRMS. I can do the inserts, but I don't know how to loop through feeder table to see if the department code in each row exists in the departments table already. Thanks for any assistance.
J.
You can use the merge keyword in SQL 2008 or greater. I just tested this snippet:
merge department as d
using feeder as f on f.code = d.code
when not matched then
insert (code)
values (f.code);
Merge will work. Since we're just doing inserts though, this will too:
INSERT
department
(
departmentCode
)
SELECT departmentcode
FROM
(
SELECT
departmentcode
FROM
feeder
EXCEPT
SELECT
departmentcode
FROM
department
) c;
With indexes on departmentcode in both tables, this pattern usually performs well.

Crystal Reports selecting data from two different databases

I have a two databases that contain the exact same tables and are on the same server. I want to be able to create a report that will allow me to "merge" these databases so that when a user queries they will query BOTH databases at the same time. Is that even possible?
The simplest way to achieve this would be to create database views that UNION ALL the values from the same tables in both databases - something like:
CREATE VIEW CombinedSalesTable AS
SELECT * FROM database1.SalesTable
UNION ALL
SELECT * FROM database2.SalesTable
- and design the reports to query the views.
You may want to add an additional column to the views to show which database each record comes from, as a key value that is unique in one table may have a "duplicate" in the equivalent table in the other database.

crystal reports, populating a column

I have a crystal report which fetches data from a table. It is then displayed in a tablular format. Now I want to add a new column in the report. The values for the new column will be based on the values of an already existing column which is getting populated from the table. The relation is based between the tables.
For eg:
Currently crystal report fetches data from XYZ table. The values of columns column1 and column2 are displayed in the report. Now I am adding a new field in crystal report to be displayed. The values for this field is obtained from a column of another table, say column3 of table ABC. Table ABC has column3 and column4. The relation will be like, i have to select ABC.column3 where XYZ.column2=ABC.column4.
How to achieve this in crystal report.
Thanks in advance
In the report creation wizard, select both your XYZ and ABC tables, similar to this
Then in the next step, setup the relation [XYZ.column2=ABC.column4], similar to this
Now in the next step, select all the columns you want to display from both tables, similar to this
That should take care of your requirement.

How do I create a report with multiple 1-M relationships?

I need to create a report that shows, for each master row, two sets of child (detail) rows. In database terms you can think of this as table 'A' having a 1:M relationship to table 'B' and also a 1:M relationship to table 'C'. So, for each row from table 'A', I want to display a list of child rows from 'B' (preferably in one section) and a list of child rows from 'C' (preferably in another section). I would also prefer to avoid the use of sub-reports, if possible. How can I best accomplish this?
Thanks.
I think I understand your question correctly, ie for a given row in Table A, you want the details section(s) to show all connected rows in Table B and then all connected rows in Table C (for any number of rows in B or C, including zero). I only know of two solutions to this, neither of which is straightforward.
The first is, as you've guessed, the disliked subreport option.
The second involves some additional work in the database; specifically, creating a view that combines the entries in Table B and Table C into one table, which can then be used in the main report as a linkable object to report on and you can group on Table A as desired. How straightforward this is will depend on how similar the structures of B and C are. If they were effectively identical, the view could contain something simple like
SELECT 'B' AS DetailType, Field1, Field2, FieldLinkedToTableA
FROM TableB
UNION ALL
SELECT 'C' AS DetailType, Field1, Field2, FieldLinkedToTableA
FROM TAbleC
However, neither option will scale well to reports with lots of results (unless your server supports indexing the view).
This is exactly what Crystal was made for :)
Make a blank .rpt and connect to your data sources as you normally would.
Go to Report->Group Expert and choose your grouping field (aka Index field).
You will now see a Group Header section in your design view. This is your "Master row". The Details sections will be your "Child rows".
In the example image below, this file is grouped by {Client}. For client "ZZZZ", there are 2 records, so there are 2 details sections while all the other clients only have 1 details section.
Edit
Based on your response, how about this:
In your datasource (or perhaps using some kind of intermediary like MS Access), start SQLing as follows.
Make a subquery left joining the primary key of TblA and the foreign key of TblB. Add a third column containing a constant, e.g. "TblB"
Make a subquery left joining the primary key of TblA and the foreign key of TblC. Add a third column containing a different constant, e.g. "TblC"
Union those 2 queries together. That'll be your "index table" of your crystal report.
In Crystal, you can have multiple grouping levels. So group first by your constant column, then by the TblA primary key, then by the foreign key.
This way, all the results from TblB will be displayed first, then TblC. And with a little work, tables B & C won't even have to have the same field definitions.
You can use or create columns that are used for grouping, then group on the table A column, then the table B column, then C. (Crystal's group is not the same as t-sql "group by". In Crystal, it's more of a sorting than a grouping.)