How to remove records from datatable with condition? - c#

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

Related

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 apply group by in sql

Can the same logic be applied in sql stored procedure itself to get the same result as done in the following link post instead of using LINQ?
Linq query having group by
This is my stored procedure
CREATE PROC [dbo].[WF_SPScanningReport_S]
(
#COMPANYID VARCHAR(20),
#FROMDATE DATETIME,
#TODATE DATETIME,
#FromRecord int,
#ToRecord int,
#SortString varchar(200)
)
AS
BEGIN
declare #Query nvarchar(Max)
if(ISNULL(LTRIM(RTRIM(#SortString)),'') <> '')
set #SortString='order by '+#SortString
else
set #SortString='order by IMAGEID'
SET NOCOUNT ON
set #Query=' CREATE TABLE #TABLE
(IMAGEID INT ,
SCANDATE DATETIME,
COUNT INT
)
INSERT INTO #TABLE(IMAGEID , SCANDATE)
SELECT IMAGEID , CAST(CONVERT(VARCHAR(10), SCAN_DATE,101) AS DATETIME) SCANDATE FROM WF_IMAGES
WHERE COMPANYID='''+#COMPANYID+''' AND SCAN_DATE BETWEEN '''+cast(#FromDate as varchar)+''' AND '''+cast(#ToDate as varchar)+''' AND IMAGE_NAME <> ''BLANK.PDF'' AND ISNULL(''Reviewed'','''') <> ''X''
UPDATE #TABLE
SET COUNT = S.COUNT1
FROM #TABLE T, (SELECT COUNT(SCANDATE) AS COUNT1 ,SCANDATE FROM #TABLE GROUP BY SCANDATE) S
WHERE T.SCANDATE= S.SCANDATE
SELECT * FROM (SELECT Distinct row_number() over ('+#SortString+') as RowNum,
IMAGEID, SCANDATE, COUNT FROM #TABLE )AS RowConstrainedResult
Where RowNum >'+cast(#FromRecord as varchar)+' AND RowNum <= '+cast (#ToRecord as varchar)+'
DROP TABLE #TABLE '
exec (#Query)
PRINT #Query
SET NOCOUNT OFF
END
This is my SP result
RowNum IMAGEID SCANDATE COUNT
1 10000131 2012-07-04 00:00:00.000 1
2 10002626 2012-08-03 00:00:00.000 1
3 10003348 2012-09-06 00:00:00.000 1
4 10003589 2012-09-15 00:00:00.000 8
5 10003590 2012-09-15 00:00:00.000 8
6 10003591 2012-09-15 00:00:00.000 8
7 10003592 2012-09-15 00:00:00.000 8
8 10003595 2012-09-15 00:00:00.000 8
9 10003596 2012-09-15 00:00:00.000 8
10 10003598 2012-09-15 00:00:00.000 8
11 10003599 2012-09-15 00:00:00.000 8
12 10004808 2012-09-20 00:00:00.000 14
13 10004809 2012-09-20 00:00:00.000 14
14 10004810 2012-09-20 00:00:00.000 14
15 10004811 2012-09-20 00:00:00.000 14
16 10004812 2012-09-20 00:00:00.000 14
17 10004813 2012-09-20 00:00:00.000 14
18 10004814 2012-09-20 00:00:00.000 14
19 10004815 2012-09-20 00:00:00.000 14
20 10004816 2012-09-20 00:00:00.000 14
21 10004817 2012-09-20 00:00:00.000 14
22 10004818 2012-09-20 00:00:00.000 14
23 10004819 2012-09-20 00:00:00.000 14
24 10004820 2012-09-20 00:00:00.000 14
25 10004823 2012-09-20 00:00:00.000 14
26 10006686 2012-09-21 00:00:00.000 3
27 10006687 2012-09-21 00:00:00.000 3
28 10006689 2012-09-21 00:00:00.000 3
29 10006806 2012-09-27 00:00:00.000 1
30 10006816 2012-09-28 00:00:00.000 1
31 10006840 2012-10-02 00:00:00.000 1
32 10006877 2012-10-03 00:00:00.000 1
33 10006923 2012-10-05 00:00:00.000 3
34 10006936 2012-10-05 00:00:00.000 3
35 10006937 2012-10-05 00:00:00.000 3
36 10620408 2013-03-11 00:00:00.000 1
37 10622755 2013-07-01 00:00:00.000 8
38 10622756 2013-07-01 00:00:00.000 8
39 10622761 2013-07-01 00:00:00.000 8
40 10622765 2013-07-01 00:00:00.000 8
41 10622771 2013-07-01 00:00:00.000 8
42 10622772 2013-07-01 00:00:00.000 8
43 10622773 2013-07-01 00:00:00.000 8
44 10622774 2013-07-01 00:00:00.000 8
45 10622891 2013-07-02 00:00:00.000 14
46 10622892 2013-07-02 00:00:00.000 14
47 10622893 2013-07-02 00:00:00.000 14
48 10622895 2013-07-02 00:00:00.000 14
49 10622896 2013-07-02 00:00:00.000 14
50 10622897 2013-07-02 00:00:00.000 14
51 10622898 2013-07-02 00:00:00.000 14
52 10622899 2013-07-02 00:00:00.000 14
53 10622900 2013-07-02 00:00:00.000 14
54 10622901 2013-07-02 00:00:00.000 14
55 10622902 2013-07-02 00:00:00.000 14
56 10622903 2013-07-02 00:00:00.000 14
57 10622904 2013-07-02 00:00:00.000 14
58 10622905 2013-07-02 00:00:00.000 14
59 10622931 2013-07-03 00:00:00.000 10
60 10622932 2013-07-03 00:00:00.000 10
61 10622933 2013-07-03 00:00:00.000 10
62 10622946 2013-07-03 00:00:00.000 10
63 10622950 2013-07-03 00:00:00.000 10
64 10622951 2013-07-03 00:00:00.000 10
65 10622952 2013-07-03 00:00:00.000 10
66 10622953 2013-07-03 00:00:00.000 10
67 10622954 2013-07-03 00:00:00.000 10
68 10622956 2013-07-03 00:00:00.000 10
69 10622979 2013-07-04 00:00:00.000 5
70 10622980 2013-07-04 00:00:00.000 5
71 10622981 2013-07-04 00:00:00.000 5
72 10623017 2013-07-04 00:00:00.000 5
73 10623018 2013-07-04 00:00:00.000 5
74 10623026 2013-07-05 00:00:00.000 9
75 10623027 2013-07-05 00:00:00.000 9
76 10623028 2013-07-05 00:00:00.000 9
77 10623029 2013-07-05 00:00:00.000 9
78 10623030 2013-07-05 00:00:00.000 9
79 10623031 2013-07-05 00:00:00.000 9
80 10623032 2013-07-05 00:00:00.000 9
81 10623033 2013-07-05 00:00:00.000 9
82 10623034 2013-07-05 00:00:00.000 9
83 10623059 2013-07-08 00:00:00.000 7
84 10623060 2013-07-08 00:00:00.000 7
85 10623061 2013-07-08 00:00:00.000 7
86 10623062 2013-07-08 00:00:00.000 7
87 10623063 2013-07-08 00:00:00.000 7
88 10623064 2013-07-08 00:00:00.000 7
89 10623065 2013-07-08 00:00:00.000 7
90 10623107 2013-07-09 00:00:00.000 1
91 10623112 2013-07-10 00:00:00.000 5
92 10623113 2013-07-10 00:00:00.000 5
93 10623122 2013-07-10 00:00:00.000 5
94 10623123 2013-07-10 00:00:00.000 5
95 10623124 2013-07-10 00:00:00.000 5
96 10623142 2013-07-12 00:00:00.000 2
97 10623143 2013-07-12 00:00:00.000 2
98 10623172 2013-07-15 00:00:00.000 17
99 10623173 2013-07-15 00:00:00.000 17
100 10623174 2013-07-15 00:00:00.000 17
101 10623175 2013-07-15 00:00:00.000 17
102 10623176 2013-07-15 00:00:00.000 17
103 10623177 2013-07-15 00:00:00.000 17
104 10623178 2013-07-15 00:00:00.000 17
105 10623179 2013-07-15 00:00:00.000 17
106 10623180 2013-07-15 00:00:00.000 17
107 10623181 2013-07-15 00:00:00.000 17
108 10623182 2013-07-15 00:00:00.000 17
109 10623183 2013-07-15 00:00:00.000 17
110 10623184 2013-07-15 00:00:00.000 17
111 10623185 2013-07-15 00:00:00.000 17
112 10623186 2013-07-15 00:00:00.000 17
113 10623187 2013-07-15 00:00:00.000 17
114 10623188 2013-07-15 00:00:00.000 17
115 10623195 2013-07-16 00:00:00.000 27
116 10623196 2013-07-16 00:00:00.000 27
117 10623199 2013-07-16 00:00:00.000 27
118 10623200 2013-07-16 00:00:00.000 27
119 10623203 2013-07-16 00:00:00.000 27
120 10623204 2013-07-16 00:00:00.000 27
121 10623206 2013-07-16 00:00:00.000 27
122 10623207 2013-07-16 00:00:00.000 27
123 10623212 2013-07-16 00:00:00.000 27
124 10623213 2013-07-16 00:00:00.000 27
125 10623214 2013-07-16 00:00:00.000 27
126 10623215 2013-07-16 00:00:00.000 27
127 10623216 2013-07-16 00:00:00.000 27
128 10623217 2013-07-16 00:00:00.000 27
129 10623218 2013-07-16 00:00:00.000 27
130 10623223 2013-07-16 00:00:00.000 27
131 10623225 2013-07-16 00:00:00.000 27
132 10623227 2013-07-16 00:00:00.000 27
133 10623228 2013-07-16 00:00:00.000 27
134 10623229 2013-07-16 00:00:00.000 27
135 10623230 2013-07-16 00:00:00.000 27
136 10623231 2013-07-16 00:00:00.000 27
137 10623232 2013-07-16 00:00:00.000 27
138 10623234 2013-07-16 00:00:00.000 27
139 10623236 2013-07-16 00:00:00.000 27
140 10623237 2013-07-16 00:00:00.000 27
141 10623238 2013-07-16 00:00:00.000 27
142 10623240 2013-07-17 00:00:00.000 6
143 10623241 2013-07-17 00:00:00.000 6
144 10623242 2013-07-17 00:00:00.000 6
145 10623244 2013-07-17 00:00:00.000 6
146 10623247 2013-07-17 00:00:00.000 6
147 10623248 2013-07-17 00:00:00.000 6
148 10623841 2013-09-11 00:00:00.000 2
149 10623848 2013-09-11 00:00:00.000 2
150 10623921 2013-09-20 00:00:00.000 2
151 10623923 2013-09-20 00:00:00.000 2
152 10624106 2013-10-09 00:00:00.000 3
153 10624108 2013-10-09 00:00:00.000 3
154 10624116 2013-10-09 00:00:00.000 3
155 10624521 2013-11-23 00:00:00.000 2
156 10624523 2013-11-23 00:00:00.000 2
157 10625218 2013-12-31 00:00:00.000 2
158 10625219 2013-12-31 00:00:00.000 2
159 10625220 2014-01-02 00:00:00.000 1
160 10625245 2014-01-07 00:00:00.000 5
161 10625247 2014-01-07 00:00:00.000 5
162 10625250 2014-01-07 00:00:00.000 5
163 10625251 2014-01-07 00:00:00.000 5
164 10625252 2014-01-07 00:00:00.000 5
165 10625266 2014-01-09 00:00:00.000 1
And I need something like this
11/27/2007 12:00:00 AM 1001527,1001528,1001529,1001530,1001531,1001532,1001533,1001534,1001535,1001536,1001537,1001538,1001539,1001540,1001541,1001542 16
11/20/2008 12:00:00 AM 1002501,1002502,1002503,1002504,1002505,1002506,1002507,1002508,1002509,1002510,1002511,1002512,1002513,1002514,1002515,1002516,1002517,1002518,1002519,1002520 20
7/5/2011 12:00:00 AM 1015237,1015238 2
7/6/2011 12:00:00 AM 1015248,1015249,1015259,1015260,1015286,1015287,1015288,1015289,1015290,1015291,1015292,1015293 12
First column has scandate on which Group By is done, Second has image ids having same scan date in single cell - comma seperated, and lastly count colimn with count of image ids in single cell
The querstion comma delimited list as a single string, T-SQL provides an illustration of how you can get a comma-delimited list grouped by a particular field.
The following code-snippet shows how you could take your resultant dataset and pivot it as illustrated in question above.
select distinct t1.scantime,
STUFF(
(SELECT ',' + convert(varchar(10), t2.ImageId, 120)
FROM yourtable t2
where t1.scantime = t2.scantime
FOR XML PATH (''))
, 1, 1, '') AS imageids
from yourtable t1
However, you will want to refactor your existing stored proc, so that you are not doing unnecessary intermediate queries to get to this result.

Oracle timeframe conversion

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>

Linq query with List<T>

I have list List<Employee> it contains data like
Name Year Salary
John 2001 10000
Warn 2001 11000
George 2001 12000
Nick 2001 13000
Warn 2002 14000
John 2002 15000
Adam 2002 16000
Kile 2003 17000
John 2003 18000
Kile 2004 19000
Warn 2004 20000
I need to convert like that
Year John Warn George Nick Adam Kile
----------------------------------------------------------
2001 10000 11000 12000 13000 0 0
2002 15000 14000 0 0 16000 0
2003 18000 0 0 0 0 17000
2004 0 20000 0 0 0 19000
Is it possible with LINQ?
Would GroupBy() be sufficient?
employees.GroupBy(x => x.Year);
var annualGroups =
from e in List<Employee>
group e by e.Year into y
select new { Year = y.Key, Employees = y };

How to merge mutiple tables into one, with same schema and column names

I have 3 collection datatable that shows in this below
Month: January
ID NAME Absent
001 John 3
002 Travis 0
Month: February
ID NAME Absent
001 John 18
002 Travis 14
Month: March
ID NAME Absent
001 John 10
002 Travis 5
Before, I have used to merge DataTable
dataTable.merge(dataTable2);
The result is like this:
ID NAME Absent
001 John 3
002 Travis 0
001 John 18
002 Travis 14
001 John 10
002 Travis 5
dataTable.Merge(dataTableTemp);
GridView.DataSource = dataTable;
GridView.DataBind();
But, I want to create like this (we say: custom GridView):
Jan Feb Mar Apr
ID NAME Absent Absent Absent Absent
001 John 3 18 10
002 Travis 0 14 5
How to add/merge every month in the right side of table before, so if I add next month it will appear in the right side of table before..
How to create this:??? Is there any method of function or tools that I can use??
Thanks for any help..
Try this:
void Merge(DataTable masterDataTable, DataTable[] dataTables, int columnIndex)
{
foreach (DataTable dt in dataTables)
{
DataColumn newColumn = masterDataTable.Columns.Add(dt.TableName, typeof(int));
int newColumnIndex = masterDataTable.Columns.IndexOf(newColumn);
for (int i = 0; i < dt.Rows.Count; i++)
{
masterDataTable.Rows[i][newColumnIndex] = dt.Rows[i][columnIndex];
}
}
}
and this is how you use it:
Merge( janDataTable, new DataTable[]{ febDataTable, marDataTable, aprDataTable}, 2);
You need to pivot your table. Take a look at this:
http://weblogs.sqlteam.com/jeffs/articles/5091.aspx

Categories

Resources