I have 2 DataTables that look like this:
DataTable 1:
cheie_primara cheie_secundara judet localitate
1 11 A
2 22 B
3 33 C
4 44 D
5 55 A
6 66 B
7 77 C
8 88 D
9 99 A
DataTable 2:
ID_CP BAN JUDET LOCALITATE ADRESA
1 11 A aa random
2 22 B ss random
3 33 C ee random
4 44 D xx random
5 55 A rr random
6 66 B aa random
7 77 C ss random
8 88 D ee random
9 99 A xx random
and I want to update DataTable 1 with the field["LOCALITATE"] using the maching key DataTable1["cheie_primara"] and DataTable2["ID_CP"].
Like this:
cheie_primara cheie_secundara judet localitate
1 11 A aa
2 22 B ss
3 33 C ee
4 44 D xx
5 55 A rr
6 66 B aa
7 77 C ss
8 88 D ee
9 99 A xx
Is there a LINQ methode to update DataTable1 ?
Thanks!
This is working:
DataTable1.AsEnumerable()
.Join( DataTable2.AsEnumerable(),
dt1_Row => dt1_Row.ItemArray[0],
dt2_Row => dt2_Row.ItemArray[0],
(dt1_Row, dt2_Row) => new { dt1_Row, dt2_Row })
.ToList()
.ForEach(o =>
o.dt1_Row.SetField(3, o.dt2_Row.ItemArray[3]));
If you want to use Linq, here's how I'd go about it;
var a = (from d1 in DataTable1
join d2 in DataTable2 on d1.cheie_primara equals d2.ID_CP
select new {d1, d2.LOCALITATE}).ToList();
a.ForEach(b => b.d1.localitate = b.LOCALITATE);
Related
How to write a program for multiplication table PL/SQL ask the user input a number
this is the code display just table without input
Declare
i NUMBER:=0;
x NUMBER;
Begin
loop
i := i+1;
x :=2*i;
dbms_output.put_line('2'||'x'||i||'='||x);
IF i >=10 THEN
EXIT ;
END IF;
END loop;
END;
/
A simple option (ran in SQL*Plus) is
SQL> set ver off
SQL> begin
2 for i in 1 .. 10 loop
3 dbms_output.put_line(&&par_number|| ' x ' || i ||' = '|| &&par_number * i);
4 end loop;
5 end;
6 /
Enter value for par_number: 2
2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
2 x 4 = 8
2 x 5 = 10
2 x 6 = 12
2 x 7 = 14
2 x 8 = 16
2 x 9 = 18
2 x 10 = 20
PL/SQL procedure successfully completed.
SQL>
As Koen commented, depending on a client, substitution variable (&&par_number) might need to be modified to a bind variable (:par_number), or you'd enter it into a page item, or some other option.
More info you provide, better answer you get.
You could create the complete multiplication table with just sql and then select whatever you want without using PL/SQL ...
Here is the code: (all ran in SQL Developer)
WITH
nums AS
(
Select LEVEL "N" From Dual Connect By LEVEL <= 10
),
tbl AS
(
Select COL_N * N1 "N1", COL_N * N2 "N2", COL_N * N3 "N3", COL_N * N4 "N4", COL_N * N5 "N5",
COL_N * N6 "N6", COL_N * N7 "N7", COL_N * N8 "N8", COL_N * N9 "N9", COL_N * N10 "N10"
From ( SELECT n2.N "COL_N",n1. N "N", n1.N "ROW_N"
FROM nums n1
INNER JOIN nums n2 ON(1 = 1)
)
PIVOT ( MAX(N) FOR ROW_N IN(1 "N1", 2 "N2", 3 "N3", 4 "N4", 5 "N5", 6 "N6", 7 "N7", 8 "N8", 9 "N9", 10 "N10") )
Order By COL_N
)
... the content is
SELECT N1 "1", N2 "2", N3 "3", N4 "4", N5 "5", N6 "6", N7 "7", N8 "8", N9 "9", N10 "10" FROM tbl
1 2 3 4 5 6 7 8 9 10
---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 40 48 56 64 72 80
9 18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100
... and select with variable passed to:
SELECT LPAD(N1, 2, ' ') || ' x ' || LPAD(&&M_NUM, 2, ' ') || ' = ' || LPAD(N1 * &&M_NUM, 3, ' ') "RESULTS"
FROM tbl
-- with &&M_NUM = 6 results:
RESULTS
--------------
1 x 6 = 6
2 x 6 = 12
3 x 6 = 18
4 x 6 = 24
5 x 6 = 30
6 x 6 = 36
7 x 6 = 42
8 x 6 = 48
9 x 6 = 54
10 x 6 = 60
... Or you could get the results in one row (it's 6 again)
SELECT * FROM tbl WHERE N1 = &&M_NUM
N1 N2 N3 N4 N5 N6 N7 N8 N9 N10
---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
6 12 18 24 30 36 42 48 54 60
... or in columns ...
SELECT N1, &&M_NUM "MULTIPLYED _BY",
CASE &&M_NUM WHEN 2 THEN N2
WHEN 3 THEN N3
WHEN 4 THEN N4
WHEN 5 THEN N5
WHEN 6 THEN N6
WHEN 7 THEN N7
WHEN 8 THEN N8
WHEN 9 THEN N9
WHEN 10 THEN N10
END "RESULT"
FROM tbl
N1 MULTIPLYED _BY RESULT
---------- -------------- ----------
1 6 6
2 6 12
3 6 18
4 6 24
5 6 30
6 6 36
7 6 42
8 6 48
9 6 54
10 6 60
... or anything else you like...
Code
num = int(input(“Enter the number of lines: “))
for i in range(10):
for j in range(1,i):
print(num, the end='')
num = num+1
print()
I am writing a program which is should be like this.
Enter the number of lines: 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29
30 31 32 33 34 35 36 37
38 39 40 41 42 43 44
45 46 47 48 49 50
51 52 53 54 55
56 57 58 59
60 61 62
63 64
65
I don’t have any example from the lecturer, i just following the step from website, but the output of my code is like this: i am confused where i made the mistake, don’t get any clue to wear for or while. Please help me, thank you.
10
11 12
13 14 15
16 17 18 19
20 21 22 23 24
25 26 27 28 29 30
31 32 33 34 35 36 37
38 39 40 41 42 43 44 45
Try this:
input_data = input('Enter number of lines: ')
num = int(input_data)
# how many items to print in the first line?
items_to_print = num
# what's the starting number?
print_number = 11
for i in range(0, num):
# don't decrease num
# decrease items_to_print
# each line will reduce 1 item to print
for j in range(0, items_to_print):
print(print_number, end = ' ')
print_number += 1
print()
items_to_print -= 1
Result:
Enter number of lines: 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29
30 31 32 33 34 35 36 37
38 39 40 41 42 43 44
45 46 47 48 49 50
51 52 53 54 55
56 57 58 59
60 61 62
63 64
65
Explanation
Start small and make your way up.
First just do this:
input_data = input('Enter number of lines: ')
num = int(input_data)
print(num)
That'll print 10 if you entered 10. Great.
Second, add the first for loop and test whether it will print 10 rows.
input_data = input('Enter number of lines: ')
num = int(input_data)
for i in range(0, num):
print(f'Printing line {i}')
Third, try to print a block of 10 x 10. So, you add another variable called items_to_print. Set it to num. If you enter 10 as input, you will get 10 rows and 10 columns.
input_data = input('Enter number of lines: ')
num = int(input_data)
print_number = 0
items_to_print = num
for i in range(0, num):
print(f'Printing line {i}')
for j in range(0, items_to_print):
print(print_number, end = ' ')
Fourth step is to reduce the number of zeros printed before restarting the i loop. So, you decrement items_to_print.
input_data = input('Enter number of lines: ')
num = int(input_data)
print_number = 0
items_to_print = num
for i in range(0, num):
print(f'Printing line {i}')
for j in range(0, items_to_print):
print(print_number, end = ' ')
items_to_print -= 1
Now that your printing is working great, let's set print_number to start with 11 and each time a print happens in j loop, increment print_number. Then you will have same code I published at the top of this answer.
Well you have three little problems so let's address them one at a time.
First: default range function starts at 0 so when your j starts at one you are missing one iteration of the cicle. That explains missing one column and row but not two so let's keep going.
Second: the range function is non inclusive meaning you're I goes from 0 to 9 then in the inner loop you go from 1 to a maximum of 8. There's your missing second iteration.
Third: you are looping from 1 to an encreasing value what you want is the opposite so you need a decreasing range.
This is how you're code should look like.
num = 11
for i in range(10, 0, - 1):
for j in range(i):
print(num, end = " ")
num += 1
print()
Good luck and happy coding
Context:
I have a hydraulic erosion algorithm that needs to receive an array of droplet starting positions. I also already have a pattern replicating algorithm, so I only need a good pattern to replicate.
The Requirements:
I need an algorism that produces a set of n^2 entries in a set of format (x,y) or [index] that describe cells in an nxn grid (where n = 2^i where i is any positive integer).
(as a set it means that every cell is mentioned in exactly one entry)
The pattern [created by the algorism ] should contain zero to none clustering of "visited" cells at any stage.
The cell (0,0) is as close to (n-1,n-1) as to (1,1), this relates to the definition of clustering
Note
I was/am trying to find solutions through fractal-like patterns built through recursion, but at the time of writing this, my solution is a lookup table of a checkerboard pattern(list of black cells + list of white cells) (which is bad, but yields fewer artifacts than an ordered list)
C, C++, C#, Java implementations (if any) are preferred
You can use a linear congruential generator to create an even distribution across your n×n space. For example, if you have a 64×64 grid, using a stride of 47 will create the pattern on the left below. (Run on jsbin) The cells are visited from light to dark.
That pattern does not cluster, but it is rather uniform. It uses a simple row-wide transformation where
k = (k + 47) mod (n * n)
x = k mod n
y = k div n
You can add a bit of randomness by making k the index of a space-filling curve such as the Hilbert curve. This will yield the pattern on the right. (Run on jsbin)
You can see the code in the jsbin links.
I have solved the problem myself and just sharing my solution:
here are my outputs for the i between 0 and 3:
power: 0
ordering:
0
matrix visit order:
0
power: 1
ordering:
0 3 2 1
matrix visit order:
0 3
2 1
power: 2
ordering:
0 10 8 2 5 15 13 7 4 14 12 6 1 11 9 3
matrix visit order:
0 12 3 15
8 4 11 7
2 14 1 13
10 6 9 5
power: 3
ordering:
0 36 32 4 18 54 50 22 16 52 48 20 2 38 34 6
9 45 41 13 27 63 59 31 25 61 57 29 11 47 43 15
8 44 40 12 26 62 58 30 24 60 56 28 10 46 42 14
1 37 33 5 19 55 51 23 17 53 49 21 3 39 35 7
matrix visit order:
0 48 12 60 3 51 15 63
32 16 44 28 35 19 47 31
8 56 4 52 11 59 7 55
40 24 36 20 43 27 39 23
2 50 14 62 1 49 13 61
34 18 46 30 33 17 45 29
10 58 6 54 9 57 5 53
42 26 38 22 41 25 37 21
the code:
public static int[] GetPattern(int power, int maxReturnSize = int.MaxValue)
{
int sideLength = 1 << power;
int cellsNumber = sideLength * sideLength;
int[] ret = new int[cellsNumber];
for ( int i = 0 ; i < cellsNumber && i < maxReturnSize ; i++ ) {
// this loop's body can be used for per-request computation
int x = 0;
int y = 0;
for ( int p = power - 1 ; p >= 0 ; p-- ) {
int temp = (i >> (p * 2)) % 4; //2 bits of the index starting from the begining
int a = temp % 2; // the first bit
int b = temp >> 1; // the second bit
x += a << power - 1 - p;
y += (a ^ b) << power - 1 - p;// ^ is XOR
// 00=>(0,0), 01 =>(1,1) 10 =>(0,1) 11 =>(1,0) scaled to 2^p where 0<=p
}
//to index
int index = y * sideLength + x;
ret[i] = index;
}
return ret;
}
I do admit that somewhere along the way the values got transposed, but it does not matter because of how it works.
After doing some optimization I came up with this loop body:
int x = 0;
int y = 0;
for ( int p = 0 ; p < power ; p++ ) {
int temp = ( i >> ( p * 2 ) ) & 3;
int a = temp & 1;
int b = temp >> 1;
x = ( x << 1 ) | a;
y = ( y << 1 ) | ( a ^ b );
}
int index = y * sideLength + x;
(the code assumes that c# optimizer, IL2CPP, and CPP compiler will optimize variables temp, a, b out)
I have a problem trying to group some records and trying to count the number of records returned by a query is adding an example:
07 COMERCIO 92
15 SERVICIOS OTROS 41
01 AGRICULTURA, GANADERIA Y SILVICULTURA 141
04 INDUSTRIA MANUFACTURERA 28
10 BANCA Y FINANZAS 5
12 ADMINISTRACION PUBLICA 16
03 MINERIA 3
16 HOGAR 2
08 HOTELES Y RESTAURANTES 37
11 EMPRESARIAL 21
14 SOCIAL Y SALUD 4
06 CONSTRUCCIÓN 3
09 TRANSPORTE 30
13 EDUCACION 10
This is query:
SELECT
AGRUP.VC_CODDET AS CHR_SECECO,
AGRUP.VC_NOMDET AS VC_SECECO,
0 AS INT_NROPRESTAMO,
COUNT(*) INTO_BENEFICIARIOS,
0 AS DEC_SALCON
FROM TB_JSI_PRESTAMO_DETALLE PREDET
INNER JOIN TB_JSI_PRESTAMO PRE ON
PRE.INT_IDPRESTAMO=PREDET.INT_IDPRESTAMO
INNER JOIN TB_JSI_BENEFICIARIO_IFI BENIFI ON
BENIFI.INT_IDBENEIFI=PRE.INT_IDBENEIFI
INNER JOIN TB_JSI_OPERACION OPE ON PRE.INT_IDOPE = OPE.INT_IDOPE AND
TRUNC(TO_DATE(OPE.DT_FECVEN))>TRUNC(TO_DATE('30/09/2018', 'DD/MM/YY'))
INNER JOIN TB_JSI_CIIU CIIU ON PRE.INT_IDACT = CIIU.INT_IDACT AND
PRE.INT_IDSEC=CIIU.INT_IDCLAS
INNER JOIN TB_JSI_TABLA_DET SECECO ON SECECO.INT_IDDET=CIIU.INT_IDCLAS
INNER JOIN TB_JSI_AGRUPA_SECTOR AGRSEC ON SECECO.INT_IDDET =
AGRSEC.INT_IDSEC
INNER JOIN TB_JSI_TABLA_DET AGRUP ON AGRSEC.INT_IDAGRU = AGRUP.INT_IDDET
INNER JOIN TB_JSI_TABLA_DET MON ON OPE.INT_IDMON = MON.INT_IDDET
INNER JOIN TB_JSI_IFI IFI ON OPE.INT_IDIFI = IFI.INT_IDIFI
WHERE TRUNC(PREDET.DTE_FECPRO) = (
SELECT
TRUNC(DTE_FECPRO)
FROM (SELECT
DTE_FECPRO
FROM TB_JSI_PRESTAMO_DETALLE
WHERE DTE_FECPRO<=TO_DATE('30/09/2018','DD/MM/YY')
AND DEC_SALDOL>0
ORDER BY DTE_FECPRO DESC)
WHERE ROWNUM = 1
)
AND (NULL IS NULL OR OPE.INT_IDTIPPRO =2)
AND (NULL IS NULL OR OPE.INT_IDMON = 364)
AND (NULL IS NULL OR OPE.INT_IDIFI=72)
GROUP BY AGRUP.VC_CODDET, AGRUP.VC_NOMDET
and should return this result
07 COMERCIO 92
15 SERVICIOS OTROS 41
01 AGRICULTURA, GANADERIA Y SILVICULTURA 141
04 INDUSTRIA MANUFACTURERA 28
10 BANCA Y FINANZAS 4
12 ADMINISTRACION PUBLICA 16
03 MINERIA 3
16 HOGAR 2
08 HOTELES Y RESTAURANTES 37
11 EMPRESARIAL 21
14 SOCIAL Y SALUD 4
06 CONSTRUCCIÓN 3
09 TRANSPORTE 30
13 EDUCACION 10
since I have these records and when counting I should return 4 and not 5
269516 10 BANCA Y FINANZAS 1
269558 10 BANCA Y FINANZAS 1
269592 10 BANCA Y FINANZAS 2
269611 10 BANCA Y FINANZAS 1
#Roller, I have analyzed the result and it is now clear that one of the tables used in the query has two records with the matching condition and that is why it is returning 5 and not 4.
You just need to identify the table and if you are not able to, then use the following trick.
First of all, identify the table, basis of which you are saying that there must be 4 rows and then use that table's PK in the count with distinct as follows:
COUNT(DISTINCT MY_PK)
List list => Contents:
ID Counter
1 34
5 34
3 55
2 45
4 33
3 123
1 4
5 12
5 133
2 33
I want to group by Id. And I want to pick the big one out of every group and throw it in a new list of the same type.
This is the last version of the list:
ID Counter
1 34
2 45
3 123
4 33
5 133
Use Group like this:
var groupedData = from item in list
orderby item.id
group item by item.id into idGroup
select new { Id = idGroup.Key, MaxCounter = idGroup.Max(i => i.counter) };