Personel Table
Id
Name
1
ABC
2
DEF
Car Table
Id
Name
1
X Car
2
Y Car
Image&Document Table
Id
Path
EntityId
EntityName
1
Car-1.Jpg
1
Car
2
Person1.Jpg
1
Personal
3
Car-3.Jpg
3
Car
I want to create a sustainable and evolving common document table for all my assets.
Would it be appropriate to set up the structure as above in EF?
Does it make it harder for me to use the Include(Join) construct?
Related
I have this data:
Table A
pk name
1 Peter
2 Jon
Table B
pk trait fk_A
1 funny 1
2 generous 1
3 rude 2
I want to get this result set:
pk name traits
1 Peter funny,generous
2 Jon rude
How can I aggregate the trait column of table B and attach it as a CSV field 'traits' to table A?
I have a table filled with cars, another table that has the car IDs and the feature IDs and another table with the feature IDs and feature names.
A car can have multiple features in that table. The result of the query is, that i get multiple instances of the same car, each having a different feature.
I would like to have only one instance of the car (by ID) and combine all features into one column (list or array). I could do it in code after the query but i would like to do it in a query instead.
Cars table Car property table Property table
ID Name Car_ID property_ID property_id Property_name
1 Audi 1 1 1 Aircon
2 BMW 1 2 2 Autopilot
3 Mercedes 2 1
3 2
Result is:
1 Audi Aircon
1 Audi Autopilot
2 BMW Autopilot
3 Mercedes None
Result should be:
1 Audi [Aircon, Autopilot]
2 BMW Autopilot
3 Mercedes None
This seems like a basic aggregation query with joins:
select c.name, group_concat(Property_name) as properties
from cars c left join
car_properties cp
on c.car_id = cp.car_id left join
properties p
on p.property_id = cp.property_id
group by c.name
I have a junction table, say for People and Locations
PersonLocations
PersonId | LocationId
---------------------
1 3
2 5
Now, Locations can belong to each other i.e. one location can sit inside another, which can sit inside another etc., so I have this defined by the Location table referencing itself:
Locations
LocationId | ArbitraryName | ParentLocationId
--------------------------------------------
1 a country null
2 region a 1
3 village 1 2
4 village 2 2
5 region b 1
So you can see village 1 and village 2 belong to region a, which in turn belongs to a country
Now, I want it to be known that if person 1 visited Location 3 (village 1) as shown in the first table, they also visited Location 2 and 1 - which can be inferred by the Location table self-referencing.
But what I've done is written rules (triggers) so that if an entry occurs on the PersonLocations table it automatically inserts the ParentLocationId (which recursively works until ParentLocationId is null)
so inserting
PersonId | LocationId
---------------------
1 3
actually results in
PersonId | LocationId
---------------------
1 3
1 2
1 1
And vice versa if I remove.
What I really want to know is - is this safe? It makes my queries and views much easier but am I missing something that is later going to bite me in the backside? I feel like as long as those triggers are in place it would be fine, although its taking up more space - the payoff justifies it? But at the same time I also feel like I might be violating some principle I don't know about...
I have to get “suggested” groupings of records based off of existing data.
Table A has a composite key of Akey+Bkey. Table B has a primary key of Bkey.
Akey is generated from a SQL 2012 Sequence object and there is a one to many relationship between table A and table and Table B on Bkey.
The structure and sample data are listed below.
Table A
Akey Bkey ItemSequence
---- ---- ------------
1 1 1
1 5 2
1 7 3
2 7 1
3 2 1
3 3 2
Table B
Bkey GroupValue Data HashString
---- --------------- -------------- ----------
1 Ford Festiva AIR BAG HASH1
2 Ford Festiva RADIATOR CAP HASH2
3 Ford Festiva FUEL PUMP HASH3
4 Ford Mustang AIR FILTER HASH4
5 Ford Explorer AIR FILTER HASH5
6 Ford Edge RADIATOR CAP HASH2
7 Ford Edge FUEL PUMP HASH3
The query has to insert new groups into Table A that match existing sets of Bkeys where the HASH value matches in Table B. To depict the point I use the example of a car model. The hash values of the items within the model of the car could match exactly ( but they might not ). So if all items exist within of OVER the GroupValue then I want to insert a new set of records into Table A with the values where the exact matches were found within that group.
In the sample data you can see that AKey 3 has BKeys 2 and 3 and Bkeys 6 and 7 are a match so these 2 records would be inserted into Table A and a new sequence # generated for the AKey.
I am not sure I understand the part about the groups and GroupValue field.
According to what I understand, you want to insert into Table A any rows from Table B that match the HashString for the Bkey of each AKey and don't already exist in Table A?
If I understand correctly: for Akey1, row TableB.Bkey 7 should be added to Table A. Is that right?
If the logic is good, the following query should select those rows.
SELECT
a.Akey, a.Bkey, b3.*
FROM b
INNER JOIN a ON a.Bkey=b.Bkey
INNER JOIN b b2 ON b2.HashString=b.HashString and b2.Bkey=a.Bkey
INNER JOIN b b3 ON b3.HashString=b2.HashString AND b3.Bkey <> b.Bkey
WHERE a.Akey=3 --Comment this line for the full matches
I have a scenario as follows:
ManufacturerID Name
1 XXX
2 YYY
DeptID Name ManufacturerID
1 abc 1
2 bcd 1
3 efg 2
ProductID name deptid
1 dfdfg 1
2 dfdg 2
3 sdfsd 2
PartsID name productid
1 sdfs 1
2 sfdfs 2
3 sdd 1
I want the above table structure to be made as hierarchical using levels. How do I design the table?
I cannot understand what hierarchy you need to make, but if you're using sql server 2008 please take a look at hierachyId datatype.
And please provide more desdcription about you process. Now it looks straightforward:
Manufacturer has many departments, department has many products, product has many parts.