I have a table Sync_Transaction which has a column FieldName which stores column names of another table DDR_Transaction and NewValues which are values I want to insert/update.
Now I want build className from TableName and properties of that class from FieldName and insert into table using Entity Framework.
Sync_Transaction
RecordId DMLType TableName PKId FieldName OldValue NewValue
1 I DDR_Transaction 1 DDRId NULL 1
2 I DDR_Transaction 1 VesselID NULL 31
3 I DDR_Transaction 1 TransactionID NULL 0
4 I DDR_Transaction 1 GeneratedDate NULL Aug 1 2019 12:07PM
5 I DDR_Transaction 1 MasterName NULL Master Name
6 I DDR_Transaction 1 ChiefEngineerName NULL Chief Engineer Name
7 I DDR_Transaction 1 Title NULL FIRST DDR
8 I DDR_Transaction 1 ComponentNo NULL 13463
9 I DDR_Transaction 1 TypeOfDefect NULL NON-ESSENTIAL
10 I DDR_Transaction 1 Severity NULL 1
11 I DDR_Transaction 1 VIQChapterId NULL 1
12 I DDR_Transaction 1 VIQRefId NULL 1
13 I DDR_Transaction 1 Responsibility NULL 6
14 I DDR_Transaction 1 IdentifiedDate NULL Jul 28 2019 12:00AM
15 I DDR_Transaction 1 RepairedBy NULL 1
16 I DDR_Transaction 1 PlannedFor NULL 4
17 I DDR_Transaction 1 Priority NULL 9
18 I DDR_Transaction 1 DueDate NULL Aug 10 2019 12:00AM
19 I DDR_Transaction 1 EstStartDate NULL Aug 3 2019 12:00AM
20 I DDR_Transaction 1 EstCompletionDate NULL Aug 5 2019 12:00AM
21 I DDR_Transaction 1 IsSMSRequired NULL 0
22 I DDR_Transaction 1 IsRARequired NULL 0
23 I DDR_Transaction 1 IsMOCRequired NULL 0
24 I DDR_Transaction 1 IsPTWRequired NULL 0
25 I DDR_Transaction 1 IsReliability NULL 0
26 I DDR_Transaction 1 DescOfDefectDamage NULL TEST FIRST
27 I DDR_Transaction 1 CauseOfDamage NULL TEST FIRST CAUSE
28 I DDR_Transaction 1 IsJobCardAmended NULL 0
29 I DDR_Transaction 1 IsAdditionalJobs NULL 0
30 I DDR_Transaction 1 ISDraft NULL 0
31 I DDR_Transaction 1 CreatedBy NULL 17
32 I DDR_Transaction 1 CreatedDate NULL Aug 1 2019 12:07PM
33 I DDR_Transaction 1 IsDeleted NULL 0
34 U DDR_Transaction 1 GeneratedDate Aug 1 2019 12:07PM Aug 1 2019 12:07PM
35 U DDR_Transaction 1 ComponentNo 13463 13409
36 U DDR_Transaction 1 DescOfRepairPlanned NULL repiared
37 U DDR_Transaction 1 IsJobCardAmended 0 1
38 U DDR_Transaction 1 IsAdditionalJobs 0 1
39 U DDR_Transaction 1 RepairDate NULL Aug 14 2019 12:00AM
40 U DDR_Transaction 1 CreatedDate Aug 1 2019 12:07PM Aug 1 2019 12:07PM
41 U DDR_Transaction 1 ModifiedBy NULL 17
42 U DDR_Transaction 1 ModifiedDate NULL Aug 1 2019 12:09PM
DDR_Transaction
DDRId int
VesselID int
TransactionID int
DDRNo varchar
GeneratedDate datetime
MasterName nvarchar
ChiefEngineerName nvarchar
Title nvarchar
ComponentNo int
TypeOfDefect nvarchar
Severity int
VIQChapterId int
VIQRefId int
Responsibility int
IdentifiedDate datetime
RepairedBy nvarchar
PlannedFor nvarchar
Priority nvarchar
DueDate datetime
EstStartDate datetime
EstCompletionDate datetime
IsSMSRequired bit
IsRARequired bit
IsMOCRequired bit
IsPTWRequired bit
SMSTransID int
RATransID int
MOCTransID int
PTWTransID int
DockPlanJobNo int
IsReliability bit
ReliabilityType int
OffHireTimeInMinutes int
OffHireTimeInHours decimal
Comments nvarchar
DescOfDefectDamage nvarchar
CauseOfDamage nvarchar
DescOfRepairPlanned nvarchar
IsJobCardAmended bit
PMSChangeRequestTransID int
IsAdditionalJobs bit
RepairDate datetime
ISDraft bit
CreatedBy int
CreatedDate datetime
ModifiedBy int
ModifiedDate datetime
IsDeleted bit
DeletedBy int
DeletedDate datetime
Unfortunately, You cannot create a class and use it to map model using EF at run time for 2 main reasons:
Entity must be set in a DbSet if you mapping directly or must
Implement IEntityTypeConfiguration if you are explicitly mapping
entities
Considering DesignTimeDbContextFactory Run for your application
where it runs your mapping and chekcs for your connection and
migrations etc so its quite impossible to do what you have mentioned
with Entity Framework.
I recommend you using a trigger on your reference table, sort of each time data is updated, you run a stored procedure that reads the values from it and modifies the target table structure such as needed.
You can call that stored procedure from code with Entity Framework aswell.
Hope it helps,
This question already has answers here:
Console.ReadLine() max length?
(10 answers)
Closed 5 years ago.
I have c# console application. I want to read user input separated by space but my console application stops reading space separated input after count 87. what is the reason.
for example below is the count 87.I want to read 90 inputs separated by space. and it stops reading inputs after count 87.
1 1 1 1 1 1 1 12 22 22 22 22 22 222 222 222 222 222 222 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 31 22 22 22 22 22 22 22 22 22 2 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 2 2 2 2 22 22 22 22 22.
My console application code is;
string[] temp = Console.ReadLine().Split(' ');
int[] height = Array.ConvertAll(height_temp, Int32.Parse);
above as accoding to above input 90. height should be 90 but it gives me height 87. infact it stops reading inputs after count 87.
There is a character limit for Console.ReadLine(). It should be 256 characters if I am not mistaken. So, you can increase the limit like that;
Console.SetIn(new StreamReader(Console.OpenStandardInput(),
Console.InputEncoding,
false,
bufferSize: 1024));
string[] temp = Console.ReadLine().Split(' ');
int[] height = Array.ConvertAll(temp, Int32.Parse);
Is it possible to get this report format in sql. I tried various ways but no luck..any light on this would help me a lot.. I can get name, tickets from table but how can date wise report in sql. I dont want use any reports
NAME 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 (date)
smrithi 10 20 34 45 55 55 66 77 33 44 55 56 44 66 77 88 55 22 33 11 44 99 77 88 (tickets)
XYZ 10 20 34 45 55 55 66 77 33 44 55 56 44 66 77 88 55 22 33 11 44 99 77 88
this is what I tried ..
SELECT *
FROM
( SELECT CAST(DAY(t_date_time_issued) AS VARCHAR(4)) AS SaleDay,
CAST(MONTH(t_date_time_issued) AS VARCHAR(4)) AS SaleYear
FROM dbo.tickets) as ts
PIVOT
(
count(t_reference)
FOR SaleDay IN ( [1],[2],[3],[4],[5],[6],[7],[8],[9],
[10],[11],[12],[13],[14],[15],[16],[17],[18],[19],[20],[21],
[22],[23],[24] ) ) AS pvt
Try something like this:
SELECT Name, [1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12],[13],[14],[15],[16],[17],[18],[19],[20],[21],[22],[23],[24]
FROM
(
SELECT Name,
CAST(DAY(t_date_time_issued) AS VARCHAR(4)) AS SaleDay,
t_reference
FROM dbo.tickets) AS ts
PIVOT
(
count(t_reference)
FOR SaleDay IN ( [1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12],[13],[14],[15],[16],[17],[18],[19],[20],[21],[22],[23],[24] )
) AS pvt
http://sqlfiddle.com/#!6/052ff/5
I have read many question on Stack Overflow related to my problem, but I don't think they quite address my problem. Basically I download a XML dataset with lots of data, and inserted that data into my MS Access database. What I want to do is convert the data so that some specific rows become columns.
Now I can probably do this manually in code before inserting the data to database, but that would require lots of time and change in code, so I'm wondering if its possible to do this with MS Access.
Here's how my table basically looks, and how I want to convert it.
The index is not so relevant in my case
[Table1] => [Table1_converted]
[Index] [Name] [Data] [NameID] [NameID] [AA] [BB] [CC] [DD]
1 AA 14 1 1 14 date1 64 61
2 BB(date) 42 1 2 15+19 date2 67+21 63+12
3 CC 64 1 3 9 10
4 DD 61 1 4 date4 1 87
5 AA 15 2
6 BB(date) 35 2
7 CC 67 2
8 DD 63 2
9 AA 9 3
10 CC 10 3
11 AA 19 2
12 BB(date) 20 2
13 CC 21 2
14 DD 12 2
15 BB(date) 83 4
16 CC 1 4
17 DD 87 4
Forgot to mention that, the Values under the column [Name] are not really AA BB CC.
They are more complex then that. AA is actually like "01 - NameAA", without the quotation mark.
Forgot to mention one important element in my question, if the [Name] ex. AA with same [NameID] exists in table, then the [Data] should SUM up those two values. I have edited the tables, on the converted table i have written ex. 15+19 or 35+20 which only illustrates which values are summed up.
One more edit, hopefully the last. One of the [Name] BB has a Datetime type in [Data].
The NameID can be whichever, does not matter. So i need a query which does an exception on [Name] BB when its summing up, so that it does not sum it up like it does to every other [Name]s [Data]. Places where date is written multiple times for same [Name] and [NameID], it is always the same.
To accomplish this in Access, all you need to do is
TRANSFORM Sum([Data]) AS SumOfData
SELECT [NameID]
FROM [Table1]
GROUP BY [NameID]
PIVOT [Name]
edit re: revised question
To handle some [Name]s differently we would need to assemble the results (Sum()s, etc.) first, and then crosstab the results
For test data in [Table1]:
Index Name Data NameID
----- ---- ---------- ------
1 AA 14 1
2 BB 2013-12-01 1
3 CC 64 1
4 DD 61 1
5 AA 15 2
6 BB 2013-12-02 2
7 CC 67 2
8 DD 63 2
9 AA 9 3
10 CC 10 3
11 AA 19 2
12 BB 2013-12-02 2
13 CC 21 2
14 DD 12 2
15 BB 2013-12-04 4
16 CC 1 4
17 DD 87 4
the query
TRANSFORM First(columnData) AS whatever
SELECT [NameID]
FROM
(
SELECT [NameID], [Name], Sum([Data]) AS columnData
FROM [Table1]
WHERE [Name] <> 'BB'
GROUP BY [NameID], [Name]
UNION ALL
SELECT DISTINCT [NameID], [Name], [Data]
FROM [Table1]
WHERE [Name] = 'BB'
)
GROUP BY [NameID]
PIVOT [Name]
produces
NameID AA BB CC DD
------ -- ---------- -- --
1 14 2013-12-01 64 61
2 34 2013-12-02 88 75
3 9 10
4 2013-12-04 1 87
Try this...in sql query may be it is your answer
SELECT NameID , [AA] as AA,[BB] as BB,[CC] as CC,[DD] as DD
FROM
(
SELECT Name,Data,NameID FROM Table1
)PivotData
PIVOT
(
max(Data) for Name in ([AA],[BB],[CC],[DD])
) AS Pivoting
I think you need to this
1) Take all your Table1 as it is in SQL Server
2) Then run following query
DECLARE #cols AS NVARCHAR(MAX),
#query AS NVARCHAR(MAX)
select #cols = STUFF((SELECT distinct ',' + QUOTENAME(Name)
from [Table1]
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
set #query = 'SELECT countryid,' + #cols + '
from
(
select NameID, Name
from Table1 cc
) T
pivot
(
max (Name)
for languagename in (' + #cols + ')
) p '
execute sp_executesql #query;
DECLARE #Table1 TABLE ([Index] INT,[Name] CHAR(2),[Data] INT,[NameID] INT)
INSERT INTO #Table1
VALUES
(1,'AA',14,1),
(2,'BB',42,1),
(3,'CC',64,1),
(4,'DD',61,1),
(5,'AA',15,2),
(6,'BB',35,2),
(7,'CC',67,2),
(8,'DD',63,2),
(9,'AA',9,3),
(10,'CC',10,3),
(11,'BB',83,4),
(12,'CC',1,4),
(13,'DD',87,4)
SELECT [NameID] , ISNULL([AA], '') AS [AA], ISNULL([BB], '') AS [BB]
, ISNULL([CC], '') AS [CC], ISNULL([DD], '') AS [DD]
FROM
(
SELECT NAME, DATA, NAMEID
FROM #Table1
)q
PIVOT
(
SUM(DATA)
FOR NAME
IN ([AA], [BB], [CC], [DD])
)P
Result Set
NameID AA BB CC DD
1 14 42 64 61
2 15 35 67 63
3 9 10
4 83 1 87