Tough Sql Query problem involving family relationsships - tsql

i have a many to many table relationship that involves 2 logical tables.
Record table that joins to a relation table on primaryID
Second instance of record table that joins to the relation table on ReciprocalID
The purpose of this is to show family relations within the database. Each primary Record table has one or more rows in the relationtable that shows everyother family relationship this person has in the database.
I have been tasked with trying to make a contact list that involves displaying the names of each of the children that attend this school along with their parents and contact information.
I have gotten to a point where I am able to show the children under each parent, but now I have to find a way to merge these together.
Since I have no control over the design of this database(its Education Edge 7) I have made a separate database that holds my queries and views for my reports. The school I am doing this work for only has access to CR 8.5.
Right now I have my top group in CR as the lastname of the recordstable, my second group is on the fullname of the recordstable. I have a subreport that pulls in all the child records.
I have used a case when statement in my primary view(the one described above) to convert 'daughter' and 'son' to child and 'mother' or 'father' to parent.
hopefully this hasnt rambled too much. If you need anymore information just ask.
SELECT dbo.vwEA7RelationshipsTableView.PRIMARYID,
dbo.vwEA7RecordsTableView.LASTNAME AS PRIMARYLASTNAME,
dbo.vwEA7RecordsTableView.FIRSTNAME AS PRIMARYFIRSTNAME,
dbo.vwEA7RecordsTableView.NAMEFORDISPLAY AS PRIMARYNAME,
CASE dbo.vwEA7RelationshipsTableView.PRIMARYDESC
WHEN 'Father' THEN 'Parent'
WHEN 'Mother' THEN 'Parent'
WHEN 'Son' THEN 'Child'
WHEN'Daughter' THEN 'Child'
ELSE dbo.vwEA7RelationshipsTableView.PRIMARYDESC
END AS PRIMARYDESC,
dbo.vwEA7RelationshipsTableView.RELATIONID,
vwEA7RecordsTableView_1.LASTNAME AS RELATIONLASTNAME,
vwEA7RecordsTableView_1.NAMEFORDISPLAY AS RELATIONNAME,
dbo.vwEA7RelationshipsTableView.RELATIONDESC
FROM dbo.vwEA7RelationshipsTableView INNER JOIN
dbo.vwEA7RecordsTableView ON
dbo.vwEA7RelationshipsTableView.PRIMARYID = dbo.vwEA7RecordsTableView.ID INNER JOIN
dbo.vwEA7RecordsTableView AS vwEA7RecordsTableView_1 ON
dbo.vwEA7RelationshipsTableView.RELATIONID = vwEA7RecordsTableView_1.ID
TableViews are really just recreation of the primary tables from the main database.

I have solved this issue. My sql code was good, it was a matter of formatting my internal paramaters for Crystal as well as some creative grouping.

Related

Inheriting Parent Table with identifier (Postgres)

Sorry if this is a relatively easy problem to solve; I read the docs on inheritance and I'm still confused on how I would do this.
Let's say I have the parent table being car_model, which has the name of the car and some of it's features as the columns (e.g. car_name, car_description, car_year, etc). Basically a list of cars.
I have the child table being car_user, which has the column user_id.
Basically, I want to link a car to the car_user, so when I call
SELECT car_name FROM car_user WHERE user_id = "name", I could retrieve the car_name. I would need a linking component that links car_user to the car.
How would I do this?
I was thinking of doing something like having car_name column in car_user, so when I create a new data row in car_user, it could link the 2 together.
What's the best way to solve this problem?
Inheritance is something completely different. You should read about foreign keys and joins.
If one user drives only one car, but many users can drive same car, you need to build one-to-many -relation. Add car_name to your user table and JOIN using that field.

Filemaker: How to fetch list of related entities

I'm new to Filemaker, but have extensive SQL experience.
How do I add a list of children to my Filemaker layout, if I have a one-to-many relationship (a tree)? I would like to see for my current node all its children. Later I want to filter them as well.
Showing the parent is easy via the related field. But for the reverse it appears that I need to use scripts?
In SQL, I would write:
SELECT * from Element WHERE parent = {current_id};
You set up a relationship between the tables in the relationship graph using a primary key and foreign key arrangement. Then you add a portal to the related table occurrence on your main table layout. You can add filtering in the relationship itself or in the portal afterwards.
I advice you to check out this info from FileMaker on the subject.

Create One-to-One relationship based on PK of both tables

I'm really new to Entity Framework (currently using EF5) and vs2012 and am having difficulty trying to figure something out.
I have an .edmx that was generated from my database. It has two tables in it: Item and 3rdPartyItem. In short, the Item table is the main table for all the company items while the 3rdPartyItem table is a table that is used to hold additional attributes for items. This 3rdPartyItem table was created by an outside company and is used with their software for the company, so I can't mess with either table. What I'm trying to do is show a list of Items in a grid but I need to show a combination of all the fields for both tables. In vs2012, if I create a relationship and make it 'zero-to-one' (because for each record in the Item table, there doesn't necessarily have to be one in the 3rdPartyItem table), vs complains about not being mapped correctly. When I set the mapping, it then complains that there's multiple relationships. I did some research and found that you can't create a relationship with 2 primary keys, so I was thinking that was the problem. But how can I change the .edmx so that in code, I can access Items and 3rdPartyItem like so:
var items = dbContext.Items;
items.3rdPartyItem.SomeField <--field from 3rdPartyItem table.
not too sure if it's even possible, but it would be very, very helpful if so. Any ideas?
What you're looking for is table-per-type (TPT) mapping inheritance. You can view an MSDN walkthrough here (although you'd want your base type to be instantiable):
http://msdn.microsoft.com/en-us/data/jj618293.aspx

Where to place auditing fields?

In our shop, when we design a database, we typically include auditing attributes for each table (LastUpdateUser, LastUpdateDate, etc). This is common practice, however, I've noticed this becoming an increasing problem when you have tables that "inherit" from other tables, especially using tools such as the entity framework.
For example, if you have tables Customers and Employees, and those tables have a foreign key to table People, then in your entity / class model when you establish the inheritance, you need to change the names for the audit fields because they exist in both tables. Perhaps they need to become PersonLastUpdatedUser and PersonLastUpdatedDate, while the ones from Employees remain as simply LastUpdatedUser and LastUpdatedDate.
When designing tables for inheritance, do you put such audit fields in both tables, or do you just have them in the parent table and update the parent table whenever an attribute changes in a child table?
If you want to use inheritance than those attributes belong to parent table because the parent with related table forms single entity and you track auditing for whole entity. If you for any reason needs those attributes in both tables it should be the first warning that those tables are not good candidates for inheritance.
If you want true auditing, you create separate audit tables that are populated by triggers (never ever by the application or you will miss items that need to be audited).
and they shouw both the old and new value as well as the date and the user or application that made the change.
If you want a last updatedcolumn in each table (which I think is better than having it only in the parenta as that doesn't tell you anything about which of the tables changes last) and you want o use inheritance then you might need to create unique names by adding the table name to lastUpdated. So PersonLastUpdated and OrderLastUpdated, etc.
Or you don't use inheritance.

Map one-to-many tables relationship to a single entity framework object

I have the following tables in the database (just a demo, of course):
Positions
PositionId
Limits
LimitId
PositionId
I want to left join them into a single entity (always have position, not always have a limit attached to it):
Position
PositionId
LimitId
I've seen articles regarding one-to-one mapping and "Table per type inheritence", and tried to implement the same method here, but with no sucess. Is that even possible?
I think what you want is an ordinary inner join where your foreign key (PositionID in the Limits table) is allowed to be null.
Yes and no...In my scenario, the 2nd option is the applicable one, since I don't have the same primary key in both tables. so, I must create an updateable view...The problem with updateable view is that I can't modify fields which are in different tables and expect the database to handle it, unless I use "Instead of" triggers, which I really don't want to get into at all...
So, I guess there's nothing out of the box for me...damn.
(Unless you have another idea...)
Anyways, I really thank you for your help, it's much appreciated.
Nir.