Sorting records based on multiple columns - postgresql

I have a dealers tables with following columns
dealer_name is_iso is_gst is_approved
& need to display dealers records in following order
dealers with all columns set should appear first,
then dealers with iso & gst,
then with iso & verified,
then with gst & verified,
then with iso,
then with gst
data in dealers table is like
dealer_name is_iso is_gst Is_approved
A 1 1 1
B 1 0 1
C 1 0 0
D 0 1 0
E 1 1 0
F 0 1 1
G 1 0 0
G 1 1 1
Currently, I am using CASE WHEN ( answer below ) to achieve this & need to know if there is better way?

SELECT
*,
CASE
WHEN (typeA = 1 and typeB = 1 and isISO = 1) THEN 6
WHEN (typeA = 1 and typeB = 1 and isISO = 0) THEN 5
WHEN (typeA = 1 and typeB = 0 and isISO = 1) THEN 4
WHEN (typeA = 0 and typeB = 1 and isISO = 1) THEN 3
WHEN (typeA = 1 and typeB = 0 and isISO = 0) THEN 2
WHEN (typeA = 0 and typeB = 1 and isISO = 0) THEN 1
ELSE 0
END as dealer_order
FROM
dealers
ORDER BY
dealer_order desc

Related

Number of blocks of linked elements in a matrix

How can one find the number of separated linked blocks using a symmetric matrix of 0s and 1s in Matlab?
For example in matrix A, if A(n,m)=1, member n and m are connected. Connected elements make blocks. In below matrix, members 2,3,4,5,6,8,9 are connected and make a block. Also, there are two clusters of size equal to 2 and one block of size 7.
A = [1 0 0 0 0 0 0 0 0 0
0 1 1 1 1 1 0 1 1 0
0 1 1 1 1 1 0 1 1 0
0 1 1 1 1 1 0 1 1 0
0 1 1 1 1 1 0 1 1 0
0 1 1 1 1 1 0 1 1 0
0 0 0 0 0 0 1 0 0 0
0 1 1 1 1 1 0 1 1 0
0 1 1 1 1 1 0 1 1 0
0 0 0 0 0 0 0 0 0 1]
following your previous question (link) you can use labeling instead of binary indications and then sqrt the number of members in each block:
A = false(10);
% direct connections
A(2,3) = 1;
A(3,4) = 1;
A(5,6) = 1;
A(4,9) = 1;
A = A | A';
B = double(A | diag(ones(1,10))); % each node is connected to it self
B(B == 1) = 1:nnz(B); % set initial unique labels
while true
B_old = B;
% connect indirect connected nodes
for node = 1:size(B,1)
row = B(node,:);
col = row';
row = row > 0;
col(col > 0) = max(col);
cols = repmat(col,[1 nnz(row)]);
% set the same label for each group of connected nodes
B(:,row) = max(B(:,row) , cols);
end
if isequal(B,B_old)
break
end
end
% get block size
u = unique(B);
counts = hist(B(:),u);
% remove non connections
counts(u == 0) = [];
u(u == 0) = [];
% remove self connections
u(counts == 1) = [];
counts(counts == 1) = [];
% block size
blocksize = sqrt(counts);

Finding a critical point in matrix

I'm attempting to find a critical point in a matrix. The value at index (i,j) should be greater than or equal to all elements in its row, and less than or equal to all elements in its column.
Here is what I have (it's off but I'm close):
function C = critical(A)
[nrow ncol] = size(A);
C = [];
for i = 1:nrow
for j = 1:ncol
if (A(i,j) >= A(i,1:end)) && (A(i,j) <= A(1:end,j))
C = [C ; A(i,j)]
end
end
end
You can use logical indexing.
minI = min(A,[],1);
maxI = max(A,[],2);
[row,col] = find(((A.'==maxI.').' & A==minI) ==1)
Details
Remember that Matlab is column major. We therefore transpose A and maxI.
A = [
3 4 1 1 2
2 4 2 1 4
4 3 2 1 2
3 3 1 1 1
2 3 0 2 1];
A.'==maxI.'
ans =
0 0 1 1 0
1 1 0 1 1
0 0 0 0 0
0 0 0 0 0
0 1 0 0 0
Then do the minimum
A==minI
ans =
0 0 0 1 0
1 0 0 1 0
0 1 0 1 0
0 1 0 1 1
1 1 1 0 1
And then multiply the two
((A.'==maxI.').' & A==minI)
ans =
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 1 0 0 0
0 1 0 0 0
Then find the rows and cols
[row,col] = find(((A.'==maxI.').' & A==minI) ==1)
row =
4
5
col =
2
2
Try this vectorised solution using bsxfun
function [ r,c,criP ] = critical( A )
%// finding the min and max values of each col & row resptly
minI = min(A,[],1);
maxI = max(A,[],2);
%// matching all the values of min & max for each col and row resptly
%// getting the indexes of the elements satisfying both the conditions
idx = find(bsxfun(#eq,A,maxI) & bsxfun(#eq,A,minI));
%// getting the corresponding values from the indexes
criP = A(idx);
%// Also getting corresponding row and col sub
[r,c] = ind2sub(size(A),idx);
end
Sample Run:
r,c should be a vector of equal length which represents the row and column subs of each Critical point. While val is a vector of same length giving the value of the critical point itself
>> A
A =
3 4 1 1 2
2 4 2 1 4
4 3 2 1 2
3 3 1 1 1
2 3 0 2 1
>> [r,c,val] = critical(A)
r =
4
5
c =
2
2
val =
3
3
I think there is a simpler way with intersect:
>> [~, row, col] = intersect(max(A,[],2), min(A));
row =
4
col =
2
UPDATE:
With intersect, in case you have multiple critical points, it will only give you the first one. To have all the indicies, there is also another simple way:
>> B
B =
3 4 1 4 2 5
2 5 2 4 4 4
4 4 2 4 2 4
3 4 1 4 1 4
2 5 4 4 4 5
>> row = find(ismember(max(B,[],2),min(B)))
row =
3
4
>> col = find(ismember(min(B),max(B,[],2)))
col =
2 4 6
Note that the set of critical points now should be the combination of row and col, means you have total 6 critical points in this example: (3,2),(4,2),(3,4),(4,4),(3,6),(4,6).
Here you can find how to export such combination.

chisquare vector different length

I have 3 data vectors representing the gender (0=male, 1=female) of 3 groups A,B,C.
for example
A = [0 0 0 0 1 1 1 1 0 0];
B = [1 1 1 1 1 1 1 0];
C = [1 0 0 1 0 1 1 0 1 1 1 1 1];
and relative number of male and female
n_maleA =6;
n_femaleA =4;
n_maleB = 1;
n_femaleB = 7;
n_maleC = 4;
n_femaleC = 9;
I would like to know if there are significant differences in gender between the 3 groups.
To do this I read that is possible to use
[tbl,chi2stat,pval] = crosstab(x1,x2)
How can I use this with more than 2 groups of data and with data that have different length?
Is there any other way to perform the chi-squared test in matlab that suits with my case?
Thanks in advance

Using matrix rows as index range on a vector?

I have this matrix:
A = [1 3
5 7
9 10];
And this vector:
B = zeros(1,10);
Now I want to change the elements in the ranges of [1:3],[5:7] and [9:10] to 1 .
So, to get this:
C = [1 1 1 0 1 1 1 0 1 1];
I tried:
B(A(:,1):A(:,2)) = 1;
but it just changes the zeros in the first range.
Can it be done without a for loop?
Thanks.
The first column of A are starting positions and the second one are ending positions of each sequence of 1s. To denote a beginning use 1 and for the end -1, then cumsum().
% Preallocate
N = 10;
B = zeros(1,N);
B(A(:,1)) = 1
B =
1 0 0 0 1 0 0 0 1 0
B(A(:,2)+1) = -1
B =
1 0 0 -1 1 0 0 -1 1 0 -1
B = cumsum(B)
B =
1 1 1 0 1 1 1 0 1 1 0
B(1:N)
ans =
1 1 1 0 1 1 1 0 1 1
Would something like this be appropriate?
>> f = #(x)(any(A(:,1)<=x & x<=A(:,2)));
>> i = 1:length(B)
i =
1 2 3 4 5 6 7 8 9 10
>> arrayfun(f,i)
ans =
1 1 1 0 1 1 1 0 1 1
Hello you can try this:
B([A(1,1):A(1,2) A(2,1):A(2,2) A(3,1):A(3,2)]) = 1;

Combination, Subset, MATLAB

I use combnk to generate a list of combinations. But, the result shape is not my required data. I want for example for combnk(1:3,2):
1 1 0
0 1 1
1 0 1
not
1 2
1 3
2 3
How can i do it? How can i change the combnk in optimal way to give results?
Don't you mean you want
1 1 0
1 0 1
0 1 1
instead of
1 2
1 3
2 3
So that each row is a logical selection vector for the original vector v?
You can get this with the following:
v = 1:3;
k = 2;
tmp = combnk(v,k);
M = size(tmp,1);
output = false(M,numel(v));
output(sub2ind(size(output),repmat((1:M)',1,k),tmp))=true;
result:
output =
1 1 0
1 0 1
0 1 1
Another solution:
c = combnk(1:3,2);
r = repmat(1:size(c,1), [1 size(c,2)]);
output = full(sparse(r,c(:),1))
result:
output =
1 1 0
1 0 1
0 1 1