Crystal Report - First record or second record of a group - crystal-reports

I have a one to many records relationship and want to convert it into one to one. Here is the example:
a 1
a 2
b j
b p
b k
b 4
c
d 0
d 1
d v
d 6
and I want to pull the first record of data like the following:
a 1
b j
c
d 0
Also how can I pull the second record of the data like the following?
a 2
b p
c
d 1

you have to take a group for that.
and do grouping on the particular field which you wants.

Related

PostgreSQL distinct and grouping

Once upon a time in a table...
segment
column
a
1
a
1
a
2
a
3
b
4
b
5
b
5
c
6
c
7
...
...
And this is what I like to see as an end result:
segment
sum
a
COUNT(DISTINCT(column) in segment a
b
COUNT(DISTINCT(column) in segment b
c
COUNT(DISTINCT(column) in segment c
...
...
What would it be like with a for loop?
Or there is a simple, elegant way in postgreSQL?
I tried for loop but it still doesn't work :)

[kdb+/q]: Convert adjacency matrix to adjacency list

Given (rectangular) adjacency matrix m, how to construct adjacency list in q language?
In QIdioms wiki I've found solution in the k language which when run through q console with k) command gives me 'vs error:
m:(1 0 1;1 0 1)
k) (^m)_vs &,/m
'vs
Result should be:
0 0 1 1
0 2 0 2
This is what I was able to replicate in q:
k) &,/m
0 2 3 5
q) where raze m
0 2 3 5
k's^ a.k.a. shape verb is missing in q so I just did:
k) (^m)
000b
000b
q) 2 3#0b
000b
000b
Now, since:
q) parse "vs"
k) {x\:y}
I tried unsuccessfully both:
q) (2 3#0b) _vs where raze m
'
q) (2 3#0b) _\: where raze m
'type
Note that QIdioms wiki has q solution for inverse problem: from adj.list to adj.matrix.
You've got errors because original Q idioms are written in k2 - an old version of k which modern kdb+ version don't support. A current version of k is k4 and it's not backwards-compatible with k2.
For instance, X _vs Y where X and Y are integer atoms or lists in old k2 behaved like X vs Y will behave in kdb+ starting from 3.4t 2015.12.13: http://code.kx.com/q/ref/lists/#vs:
Since 3.4t 2015.12.13: For integer types, computes the base representation of Y in the radices X.
Another example. Indeed ^ in k2 was a shape operator, but it is not any longer. In k2 ^m would have returned 2 3 for a matrix m from your example whereas current implementation behaves like q's not null as far as I understand.
Now, back to your original question, "how to construct adjacency list in q language". One way to do it is this:
q)lm:{flip raze(til count x),''where each x}
or
k)lm:{+,/(!#x),''&:'x}
UPDATE: Here's how it works. If we were to build an adjacency list using any "verbose" language we would do something like this:
for i = 0 to <number of rows> - 1 <---- (1)
for j = 0 to <number of columns> - 1 <---- (2)
if M[i;j] <> 0 <---- (3)
print i, j
In an array language like q (1) can be "translated" into til count M because count will return the number of elements at top level, i.e. the number of rows. (2) and (3) combined can be represented by where each M. Indeed, for every row we return positions of non-zero elements. Given an original matrix m we will get:
til count m -> 0 1
where each m -> (0 2; 0 2)
All we need to do is join row and column indexes. We can't use just ,' because it will join 0 with the first 0 2 and 1 with the second 0 2 resulting in (0 0 2; 1 0 2). We need to go one level deeper, joining every element from the left with every element of every element of a nested list (0 2; 0 2) from the right, hence double apostrophes in ,''.
I hope it makes sense now.
Personally, I would not use flip (or + in k), I can't read an adjacency matrix in this form:
0 0 1 1
0 2 0 2
I think this is much more readable:
0 0
0 2
1 0
1 2
But it's up to you of course.

How to move a number of columns in a matrix to the right most in matlab

Suppose I have an mxn matrix A. Suppose I have a list (or a vector) P of i elements where each element of P indicates the number of a column in A. I need to move all the columns indicated by P to the right most of A; for instance the the columns indicated in the first and the i'th elements in P will become the (n-i)'th and the n'th column of A respectively.
Hope my statement is clear, let me know if there is any ambiguity.
thanks.
To get the columns 3,5,7 to the right, first c is constructed which indicates the new order of columns. Then the columns are indexed using c which reorders them.
>> M=magic(10);
>> c=[3,5,7];
>> c=[setdiff(1:size(M,2),c),c]
c =
Columns 1 through 9
1 2 4 6 8 9 10 3 5
Column 10
7
>> M=M(:,c);

Matrix patterns

I have two 9x9 matrices, A and B.
I would like to create a large matrix C with the following pattern
A B B B B B
B A B B B B
B B A B B B
B B B A B B
B B B B A B
B B B B B A
As you can see, the A matrices are on the diagonal, the B are everywhere else. I'm trying to create a code so that this pattern continues no matter how great the dimensions are.
E.g. 10 matrices x 10 matrices still has matrix A along the diagonal and B's everywhere else.
Best to use horzcat and vertcat or something else like blkdiag? I'd rather not convert these matrices to cells as matrix A and B already contain information.
Thank you everyone for taking the time to read.
How about (refined)
maskcell = repmat( {ones(size(A))}, 1, 10 );
maskdiag = blkdiag( maskcell{:} );
AA = repmat( {A}, 1, 10 );
AD = blkdiag( AA{:} );
BB = repmat( B, 10, 10 );
C = BB .* (maskdiag == 0) + AD
Following on from the entirely valid comments below, I added the 'mask' to ensure that the correct pieces are selected from C.
C=B(~eye(size(B)))+A(eye(size(A))) should get you what you want. Could be a faster way to combine the use of eye though...
kron(eye(10), A) + kron(~eye(10), B)

Matlab - select all rows starting with a particular number

Say I have a matrix A, and I want to construct a matrix B that contains all rows of B that start with a particular number. How?
Thanks
Select all rows of B into A, where the first colum of B has value n:
A = B(B(:,1) == n,:);
In contrast to that, the following selects all rows of B into A, starting from row index n:
A = B(n:end,:);