PostgreSQL distinct and grouping - postgresql

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

Related

how to reverse the element of matrix?

I am trying to reverse the elements of the matrix such that for given matrix order of the elements get reversed.
my code is as shown for the 3x3 matrix is working.
X = [ 1 2 3 ; 4 5 6 ; 7 8 9 ];
B = [fliplr(X(3,:));fliplr(X(2,:));fliplr(X(1,:))];
input X =
1 2 3
4 5 6
7 8 9
output:
B =
9 8 7
6 5 4
3 2 1
the above code I am trying to generalize for any matrix with the following code
[a,b]=size(X);
for i=0:a-1
A = [fliplr(X(a-i,:))];
end
but get only last row as output.
output A =
3 2 1
please help me to concatenate all the rows of the matrix one above to each other after it got reversed.
rot90 is the function made for this purpose.
B = rot90(A,2);
Your code doesn't work because you overwrite A in every loop iteration. Instead, you should index into A to save each of your rows.
However, fliplr can flip a whole matrix. You want to flip left/right and up/down:
B = flipud(fliplr(X));
This is the same as rotating the matrix (as Sardar posted while I was writing this):
B = rot90(X,2);
A totally different approach would work for arrays of any dimensionality:
X(:) = flipud(X(:));

Table comparison cell content reference from non cell array object

I have two tables with same variables. One table contain one row while other table contain more than one rows.
a=[1 2; 2 3],b=[2 3; 1 2]
S1=table(a,b)
a=[1 1],b=[1 1]
S2=table(a,b)
if all(S2{:,:}<S1{:,:}) & any(S2{:,:}<=S1{:,:})
S1=[S1;S2]
end
Where is the mistake in specifying table or cell? Even the conversion table2cell, table2struct, table2array did not work (getvar error was shown).
Table values are fixed. There is no addition, no replacement, but the appending only when the condition is satisfied. Final output is table with values as shown.
S1 = 3×2 table
a b
______ ______
1 2 2 3
2 3 1 2
1 1 1 1
The error is due to the fact that you are trying to compare two set of data (S1 and S2) that have different size.
S2{:,:}
1 1 1 1
S1{:,:}
1 2 2 3
2 3 1 2
If you want to compare each row of S1 against S2 you can use the function bsxfun:
to check S2 < S1
bsxfun(#lt,S2{:,:},S1{:,:})
to check S2 <= S1
bsxfun(#le,S2{:,:},S1{:,:})
This will lead to:
if all(bsxfun(#lt,S2{:,:},S1{:,:})) & any(bsxfun(#le,S2{:,:},S1{:,:}))
S1=[S1;S2]
end

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

return all columns except one matrix

Suppose we have matrix A like this:
10 5 8 6 2
A= 9 3 5 4 1
12 5 7 2 6
How can I choose a subset of A where there is no third column(for example)??
like this:
10 5 6 2
B = 9 3 4 1
12 5 2 6
I know I can say:
B = A(:,[1 2 4 5]);
but I need a better way
You can use
B = A(:,1:size(A,2)~=n);
with n as selected column (see answer of #freude).
His solution also works fine in any case, but locial indexing (as here) should be faster than his approach.
If we assume that the column to remove is n, it reads:
B = A(:,[1:n-1 n+1:end]);
An alternative, though not a single line, is to copy over and then remove the bit you don't want by setting it to empty:
B = A;
B(:,n) = [];
I mention this because in the case that you don't need B as a new matrix but just want to take one column out of A, this is the simplest/easiest to read way of doing it.

Crystal Report - First record or second record of a group

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.