Generate String Value Table Automatically in EF Core - database

I have a table called Customer that has several columns called National Code and Name. It also has a number of other features called Contact Numbers and Recommenders, since the number of Contact Numbers and Recommenders is more than one, so you need some other table to store them.
Also suppose I have other tables like the Customer, each of which has a number of attributes greater than one.
What is your suggestion for storing these values?
In one source, it was suggested that for each table, a table called StringValue be used for storage. Does EF core have a way to implement StringValue without writing additional code?
Example:
Customer Table:
CustomerId Name NationalCode
------------------------------------------------------------------------
1 David xxxx
------------------------------------------------------------------------
StringValue Table:
StringId CustomerId StringName Value
------------------------------------------------------------------------
10 1 PhoneNumber 915245
11 1 PhoneNumber 985452
12 1 PhoneNumber 935446
13 1 Recommenders Mr Jhon
14 1 Recommenders Mr bb
------------------------------------------------------------------------

I think it is more intutive create a new table for the field which has more than one records, then configure a one-to-many relationship between the two tables. Take your case as an example, you can divide the customer table into three tables, they can be linked by foreignkey:
1.Customer Table:
CustomerId Name NationalCode
---------------------------------------------
1 David xxxx
2.Contact Table:
Id CustomerId PhoneNumber
---------------------------------------------
1 1 915245
2 1 985452
3 1 935446
3.Recommender Table:
Id CustomerId RecommenderName
---------------------------------------------
1 1 Mr Jhon
2 1 Mr bb

Related

How can I build a query to view the same value in 20 or more tables in SQL?

Scenarios:
Table 1
Name ID dept
John 112 Fin
Mary 113 Act
Table 2
Name email
John John#gmail.com
Mary Max#gmail.com
Table 3
Name Supervisor
John Kelly
Mary Adam
Table 4
Namevalue Salary
John 1000
Mary 1000
Expected Output:
I would like to know the occurrence of John and Mary in all these tables. There are more than 20 tables in difference database.
Firstly, specify the master table according to data quality, use this table as base and then use left joins to concat with other tables.
But, I think, there is a design problem in the database so maybe creating keys for each user can be useful.
For example, name + surname + User Something to make records unique then use joins otherwise all records will duplicate.

SQL Server 2014 Set values to lookup table ID

If I have an address table and a cities lookup table and the address table has the cities in text instead of a foreign key, how would I replace the text with its counterpart in the lookup table?
Example:
Table luCities:
ID Name
--------------------
1 New Orleans
2 Portland
3 Seattle
Table Addresses:
ID Street City State Zip
--------------------------------------------------
1 123 main st. New Orleans OR 96556
should become
ID Street CityId State Zip
--------------------------------------------------
1 123 main st. 1 OR 96556
I have roughly 250 rows to match multiple cities with, so I'm hoping that a single UPDATE/SET statement can be used to match and modify them.
I believe you are looking for an update-join. If you add CityID to addresses as a FK to luCities, you can run this:
UPDATE Addresses SET
CityID = c.ID
FROM Addresses A
inner join luCities C on A.City = C.Name
Once everything looks correct, you can drop the old City column if you like.

SSIS: Lookup data and merge again

Somehow I have a feeling that this should be an easy one - but I cannot see how to fix this in a SSIS package.
I have two tables: Order and CustomerMapping. My CustomerMapping table is not containing all my customers, but only customers that have changed Id and CustomerName.
The definitions of the tables are:
Order:
Id (int)
CustomerId (int)
CustomerName (nvarchar(50))
CustomerMapping:
Id (int)
ObsoleteId (int)
CustomerName (nvarchar(50))
ObsoleteCustomerName (nvarchar(50))
Data in Order table is:
Id CustomerId CustomerName
1 100 Customer 1
2 101 Customer 2
3 102 Customer 3
Data in CustomerMapping:
Id ObsoleteId CustomerName ObsoleteCustomerName
20 100 New Customer 1 Customer 1
21 101 New Customer 2 Customer 2
I want to use the tools provided by the SSIS package to create what I would do like this in SQL:
SELECT o.Id,
CustomerId = ISNULL(cm.Id, o.CustomerId),
CustomerName = ISNULL(cm.CustomerName, o.CustomerName)
FROM Order AS o
LEFT JOIN CustomerMapping AS cm ON o.CustomerId = cm.ObsoleteId
The result of above query is
Id CustomerId CustomerName
1 20 New Customer 1
2 21 New Customer 2
3 102 Customer 3
I now want to take this result-set and save into a new table.
I know that I can just take the above SQL and do what I want (which might be what I end up doing), but somehow I believe I can make a lookup, merge and/or merge join... But I must admit I cannot really see how I do this without splitting the lookup into two and then having to save from each new "thread".
The above is rather simplified... I have 4 columns which I have to compare and then do some other stuff with the whole lot, before saving it into a new table again, which is why I want to keep a single "thread".
Edit: image added:

SQL Server aggregate function with sum

I want to use an aggregate function in SQL Server to sum the number of seats:
The table is like that (it's all the same software just version changes)
OrderID | CustomerID | ProductID | Product Name | No of Seats
1 | 11 | 351-0212-4 | soft v601,Download | 3
2 | 11 | 361-0313-5 | soft v701,Upgrade | 2
3 | 12 | 341-1210-4 | soft v501,Download | 5
4 | 12 | 351-0212-5 | soft v601,Upgrade | 2
...
And I want a result like
Sum(no of seats)
8
So If a customer already bought the software but have upgraded keep number of seats for the customer.
e.g.:
Customer 11 bought 3 licences of our soft and then he bought two upgrades of a newer vesion so the sum for him should be 3 instead of 5.
Is that something possible to do in SQL ?
I hope I've been clear if not let me know.
Thanks in advance.
something like
select CustomerID, sum([No of Seats])
from <your table>
where [Product Name] not like '%upgrade%'
group by CustomerID
But in general - filter out those you don't want to see in the results and then sum. And if you want total number (not per customer):
select sum([No of Seats])
from <your table>
where [Product Name] not like '%upgrade%'
you should add boolean column, like 'isActive', then your select can be like this
select customerid, sum(numberOfSeats) from table
where isActive = 1
group by customerid
You have some normalization problems. The Product Name column is (presumably) redundant with the ProductID column, plus Product Name
apparently carries two logically distinct pieces of information: the name itself, and whether that product is an upgrade.
It would be better to split this into two tables, say Products and Orders. The Products table would have columns ProductID (primary key), Product_Name, and Is_Upgrade, and the Orders table would have columns OrderID (primary key), CustomerID (foreign key), ProductID (foreign key), and NumberOfSeats.
Given what you now have, however, and assuming that you want to avoid counting seats where the product name ends in 'Upgrade', you seem to want a query along these lines:
SELECT SUM("No of seats")
FROM Orders
WHERE CustomerID = 11 AND "Product Name" NOT LIKE '%Upgrade'

Entity relation

Is the following entity relation correct? I am trying to link employee, timesheet and approver table.
There are few other tables and fields but for this question my main concern is following three tables. One employee can have more than timesheet approver.
Employee
--------
EmpID pk
Name
TimeSheet
------------
TSHEET_ID PK
FK_EmpID FK
Approved_By
Timesheet_Approver (one employee can have more than one approver
but only one will be approving the timesheet)
------------------
EmpID
Approver_EmpID
Employee Table Data:
EmpID Name
----- -----
1 john
2 david
3 mark
Timesheet Approver Data:
EmpID Approver
----- --------
1 2
1 3
2 3
Timesheet
TSID EMPID APPROVED_BY
---- ----- -----------
101 1 2
102 1 3
103 2 3
OR this looks okay?
My Suggestion
Why do you want Approver as a seperate table? Can you keep a Bit Field like IsApprover in Employee table? Otherwise you are creating redundancy
I feel something wrong in the design. This is my idea.
Employee
EmpID pk
Name
TimeSheet
TSHEET_ID PK
FK_EmpID
Approved_By
Third table is unnecessary.
If there are several approves we can't add a column called Approved_by to the TimeSheet table.
Then we can add two more relations. One is for approvers list.
Approvers
ApproverID
Department
Name
TimeSheetApproves
TimeSheetID
Department1ApproverID
Department2ApproverID
..........................so on
Don't forgot to remove Approved_by to the TimeSheet realtion.
Employee
EmpID - Pk
Name
TimeSheet
TSHEET_ID - PK
EmpID - FK
ApproverID - FK
Approvers
ApproverID -PK
Department
OK this is my final answer. If it doesn't suit you can try your own one. I don't have time to read all those comments. And I was trying to help you.
Using the Approvers relation you can get approvers in a department and other things. Check sql queries to get those data.

Resources