Oracle timeframe conversion - c#

I have the following configuration: a table called source (bid, valid_from, valid_to, qty) and destination (bid, jan, feb, mar, apr, ...., dec).
I would like to insert the data from source into origin, (spliting the qty equally to the valid_from-valid_to months and the remaining rest to the last month in the timeframe) like this : if the record in source is
00001 01.02.2001 31.06.2001 132
this would be translated into destination :
00001 0 26 26 26 26 28 0 0 0 0 0 0
How could I do this?
Thank you!

I'm assuming valid_from/valid_to never spans multiple years; i.e., TO_DATE(valid_from,'YYYY') always = TO_DATE(valid_to,'YYYY').
SQL> CREATE TABLE source (
2 bid VARCHAR2(5)
3 , valid_from DATE
4 , valid_to DATE
5 , qty NUMBER
6 );
Table created.
SQL> INSERT INTO source VALUES ('00001',TO_DATE('20010201','YYYYMMDD'),TO_DATE('20010630','YYYYMMDD'),132);
1 row created.
SQL> INSERT INTO source VALUES ('00002',TO_DATE('20020301','YYYYMMDD'),TO_DATE('20021231','YYYYMMDD'),59);
1 row created.
SQL> CREATE TABLE destination (
2 bid VARCHAR2(5)
3 , jan NUMBER
4 , feb NUMBER
5 , mar NUMBER
6 , apr NUMBER
7 , may NUMBER
8 , jun NUMBER
9 , jul NUMBER
10 , aug NUMBER
11 , sep NUMBER
12 , oct NUMBER
13 , nov NUMBER
14 , dec NUMBER
15 );
Table created.
SQL> COLUMN jan FORMAT 999
SQL> COLUMN feb FORMAT 999
SQL> COLUMN mar FORMAT 999
SQL> COLUMN apr FORMAT 999
SQL> COLUMN may FORMAT 999
SQL> COLUMN jun FORMAT 999
SQL> COLUMN jul FORMAT 999
SQL> COLUMN aug FORMAT 999
SQL> COLUMN sep FORMAT 999
SQL> COLUMN oct FORMAT 999
SQL> COLUMN nov FORMAT 999
SQL> COLUMN dec FORMAT 999
SQL> INSERT INTO destination
2 SELECT bid
3 , NVL(MAX(DECODE(r,01,split_qty)),0) jan
4 , NVL(MAX(DECODE(r,02,split_qty)),0) feb
5 , NVL(MAX(DECODE(r,03,split_qty)),0) mar
6 , NVL(MAX(DECODE(r,04,split_qty)),0) apr
7 , NVL(MAX(DECODE(r,05,split_qty)),0) may
8 , NVL(MAX(DECODE(r,06,split_qty)),0) jun
9 , NVL(MAX(DECODE(r,07,split_qty)),0) jul
10 , NVL(MAX(DECODE(r,08,split_qty)),0) aug
11 , NVL(MAX(DECODE(r,09,split_qty)),0) sep
12 , NVL(MAX(DECODE(r,10,split_qty)),0) oct
13 , NVL(MAX(DECODE(r,11,split_qty)),0) nov
14 , NVL(MAX(DECODE(r,12,split_qty)),0) dec
15 FROM
16 (
17 SELECT x.bid
18 , x.month_abbr
19 , x.r
20 , x.rn
21 , x.total_months
22 , x.qty
23 , FLOOR(x.qty / x.total_months)
24 + DECODE(x.rn
25 , x.total_months, MOD(x.qty, x.total_months)
26 , 0) split_qty
27 FROM (SELECT s.bid
28 , months.r
29 , ROW_NUMBER()
30 OVER (PARTITION BY s.bid
31 ORDER BY months.r) rn
32 , COUNT(*)
33 OVER (PARTITION BY s.bid) total_months
34 , s.qty
35 FROM (SELECT ROWNUM r
36 FROM DUAL
37 CONNECT BY LEVEL <= 12) months
38 , source s
39 WHERE TO_CHAR(s.valid_from,'YYYY') = TO_CHAR(s.valid_to,'YYYY')
40 AND months.r BETWEEN TO_NUMBER(TO_CHAR(s.valid_from,'MM'))
41 AND TO_NUMBER(TO_CHAR(s.valid_to,'MM'))) x
42 )
43 GROUP BY bid
44 ;
2 rows created.
SQL> SELECT *
2 FROM destination
3 ;
BID JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC
----- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
00001 0 26 26 26 26 28 0 0 0 0 0 0
00002 0 0 5 5 5 5 5 5 5 5 5 14
SQL>

Related

Convert rows values of FieldName and TableName to ClassName and its properties and save to DB using Entity Framework

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,

C# console application reading user input [duplicate]

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);

How to remove records from datatable with condition?

I have a datatable like below,
ID--- Month---- Salary
10 Mar 15000
10 Apr 15000
11 Mar 10000
11 Apr 25000
12 Mar 15000
How to make this table to
ID-----Month-----Salary
11 Mar 10000
11 Apr 25000
12 Mar 15000
select distinct salary group by employee id

month wise reports by date

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

convert rows to columns in Access

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

Categories

Resources