Mondrian: sum aggregator gives unexpected results - mondrian

I have a cube with the following measures :
<Measure name="NbrSecrets"
column="isSecret" aggregator="sum"/>
<Measure name="Montant du contrat"
column="montantcontrat" aggregator="sum"
formatString="$#,##0" />
The column 'isSecret are either 0 or 1 (of type int in postgres)
The following query returns sums of zero for the column / measure NbrSecrets,
it is behaving differently than 'Montant du contrat', which correctly sums the values
in the fact table. The result is as if the isSecret column of the fact table contained 0 for all rows, I know for a fact that many of the rows have isSecret=1.
SELECT
NON EMPTY
{Hierarchize({[Time].[Year].Members})}
ON COLUMNS,
NON EMPTY
{Hierarchize({
[Measures].[NbrSecrets],
[Measures].[Montant du contrat]})}
ON ROWS
FROM [Contrats]
Axis #0:
{}
Axis #1:
{[Time].[2000]}
{[Time].[2001]}
{[Time].[2003]}
{[Time].[2004]}
{[Time].[2005]}
{[Time].[2006]}
{[Time].[2007]}
{[Time].[2008]}
{[Time].[2009]}
{[Time].[2010]}
{[Time].[2011]}
{[Time].[2012]}
{[Time].[2013]}
Axis #2:
{[Measures].[Montant du contrat]}
{[Measures].[NbrSecrets]}
Row #0: $24,906
Row #0: $798,634
Row #0: $8,381
Row #0: $56,281
Row #0: $1,683,772
Row #0: $614,878
Row #0: $4,983,809
Row #0: $409,447,717
Row #0: $7,769,408,600
Row #0: $7,451,808,764
Row #0: $10,240,167,750
Row #0: $12,675,764,184
Row #0: $2,328,797,494
Row #1: 0
Row #1: 0
Row #1: 0
Row #1: 0
Row #1: 0
Row #1: 0
Row #1: 0
Row #1: 0
Row #1: 0
Row #1: 0
Row #1: 0
Row #1: 0
Row #1: 0
This MDX seems to contradict the previous results (isSecret returns the correct sums in this case) :
SELECT
NON EMPTY CrossJoin([Transparence (montant des contrats secrets ou non)].[Montant secrets].Members, [Time].[Year].Members) ON COLUMNS,
NON EMPTY {Hierarchize({[Measures].[NbrSecrets]})} ON ROWS
FROM [Contrats]
result :
Axis #0:
{}
Axis #1:
{[InterneOuExterne].[false], [Time].[2000]}
{[InterneOuExterne].[false], [Time].[2001]}
{[InterneOuExterne].[false], [Time].[2003]}
{[InterneOuExterne].[false], [Time].[2004]}
{[InterneOuExterne].[false], [Time].[2005]}
{[InterneOuExterne].[false], [Time].[2006]}
{[InterneOuExterne].[false], [Time].[2007]}
{[InterneOuExterne].[false], [Time].[2008]}
{[InterneOuExterne].[false], [Time].[2009]}
{[InterneOuExterne].[false], [Time].[2010]}
{[InterneOuExterne].[false], [Time].[2011]}
{[InterneOuExterne].[false], [Time].[2012]}
{[InterneOuExterne].[false], [Time].[2013]}
{[InterneOuExterne].[true], [Time].[2001]}
{[InterneOuExterne].[true], [Time].[2008]}
{[InterneOuExterne].[true], [Time].[2009]}
{[InterneOuExterne].[true], [Time].[2010]}
{[InterneOuExterne].[true], [Time].[2011]}
{[InterneOuExterne].[true], [Time].[2012]}
{[InterneOuExterne].[true], [Time].[2013]}
Axis #2:
{[Measures].[NbrSecrets]}
Row #0: 0
Row #0: 0
Row #0: 0
Row #0: 0
Row #0: 0
Row #0: 0
Row #0: 0
Row #0: 0
Row #0: 0
Row #0: 0
Row #0: 0
Row #0: 0
Row #0: 0
Row #0: 1
Row #0: 14
Row #0: 336
Row #0: 486
Row #0: 1,227
Row #0: 1,736
Row #0: 412

Related

PL/SQL - How to find out values missing from table by comparing to another table

I am building my knowledge of using SQL by using the basic 10x10 (-5 to 5) grid system on the game battleships to work out which grids avoid getting hit over a series of games.
I have come up with the following queries, to compare the X and Y grids on the board (game) to a table containing 11 rows of the -5 to 5 (including 0) numbers used per axis (grid_format). I have inserted 9 lines of test data (some of which are on the same grids).
The result should return about 114 rows, instead I only get 49 rows. Individually, the x_coord and y_coord queries return 7 rows, excluding the ones that were used on game, meaning that it is working. The problem lies with bringing them together, where entire y_coord grids are omitted.
Both these queries have given me the same result. Is anyone able to help me solve this dilemma please?
-- Table
CREATE TABLE game(
x_coord NUMBER(2,0),
y_coord NUMBER(2,0));
INSERT INTO game VALUES (4,4);
INSERT INTO game VALUES (1,-4);
INSERT INTO game VALUES (0,0);
INSERT INTO game VALUES (0,0);
INSERT INTO game VALUES (1,-5);
INSERT INTO game VALUES (1,-5);
INSERT INTO game VALUES (-2,4);
INSERT INTO game VALUES (1,-5);
INSERT INTO game VALUES (0,0);
CREATE TABLE grid_format(
grid NUMBER(2,0));
INSERT INTO grid_format VALUES (-5);
INSERT INTO grid_format VALUES (-4);
INSERT INTO grid_format VALUES (-3);
INSERT INTO grid_format VALUES (-2);
INSERT INTO grid_format VALUES (-1);
INSERT INTO grid_format VALUES (-0);
INSERT INTO grid_format VALUES (1);
INSERT INTO grid_format VALUES (2);
INSERT INTO grid_format VALUES (3);
INSERT INTO grid_format VALUES (4);
INSERT INTO grid_format VALUES (5);
-- Query
SELECT X_Grid, Y_Grid
FROM
(SELECT grid AS Y_Grid
FROM grid_format
WHERE
NOT EXISTS (
SELECT *
FROM game
WHERE game.y_coord = grid_format.grid)),
(SELECT grid AS X_Grid
FROM grid_format
WHERE
NOT EXISTS (
SELECT *
FROM game
WHERE game.x_coord = grid_format.grid)
ORDER BY X_Grid DESC);
-- Alternative Solution
SELECT X_Grid, Y_Grid
FROM
(SELECT grid AS X_Grid
FROM grid_format
MINUS
SELECT x_coord
FROM game),
(SELECT grid AS Y_Grid
FROM grid_format
MINUS
SELECT y_coord
FROM game)
Here are the results for the test (see link):
Result from query
Thank you.
You could use something like that:
WITH
xaxis AS (SELECT grid AS x FROM grid_format),
yaxis AS (SELECT grid AS y FROM grid_format),
grid AS (SELECT x, y FROM xaxis, yaxis)
SELECT *
FROM grid
LEFT JOIN game
ON grid.x = game.x_coord
AND grid.y = game.y_coord;
which returns all the grid positions (x,y) and, if there is a ship, a value in X_COORD and Y_COORD, something like
X Y X_COORD Y_COORD
3 3 3 3
-5 2
-2 5
3 -5
-2 -4
EDIT:
You could even display the grid graphically with something like
WITH
xaxis AS (SELECT grid AS x_coord FROM grid_format),
yaxis AS (SELECT grid AS y_coord FROM grid_format),
grid AS (SELECT x_coord, y_coord FROM xaxis, yaxis),
grid2 AS (SELECT x_coord, y_coord, NVL2(game.rowid,1,0) as ship
FROM grid LEFT JOIN game USING(x_coord, y_coord))
SELECT *
FROM grid2
PIVOT (sum(ship)
FOR x_coord IN (-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5))
ORDER BY y_coord;
-5 0 0 0 0 0 0 0 0 0 0 0
-4 0 0 0 0 0 0 0 0 0 0 0
-3 0 0 0 0 0 0 0 0 0 0 0
-2 0 0 0 0 0 0 0 0 0 0 0
-1 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0 0 0
2 0 0 0 0 0 0 0 0 0 0 0
3 0 0 0 0 0 0 0 0 1 0 0
4 0 0 0 0 0 0 0 0 0 0 0
5 0 0 0 0 0 0 0 0 0 0 0

How to read size of a pbm file in ASCII mode?

Let us consider the example:
The pbm file "imFile.pbm" contains the pixels as follows :
P1
# Comment
9 6
0 0 0 0 0 0 0 0 0
0 1 1 0 0 0 0 0 0
0 1 1 0 0 0 0 0 0
0 1 1 0 0 0 0 0 0
0 1 1 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
How can I determine the width and Height of the image. I have used the following code but failed.
with open("imFile.pbm", 'rb') as f:
image = f.size
print image
f.close()
When I compiled it in my ubuntu14.04 os, it shows error. Any suggestion will be appreciated. Thank you in advance.
The width and height is right there in the file, in the first row after the first one, skipping comments. That's not what .size is for; you need to read and parse the file.
with open("imFile.pbm", 'r') as f:
lines = f.readlines()
lines.pop(0) # skip header
line = lines.pop(0) # get next line
while line.startswith("#"): # repeat till that line is not a comment
line = lines.pop(0)
width, height= line.split() # split the first non-comment lin
print("%s x %s" % (width, height)) # => 9 x 6

How to reorder a binary matrix such that at least '1' value in the column

I have a binary matrix A as
A=[0 0 0 1
0 1 1 0;
0 1 0 1;
0 0 0 1;
0 1 1 1]
I want to reorder the matrix A such that at least 1 column has '1' values. So, the matrix A will be come
%% switch first col and last col in the first row
A=[1 0 0 0
0 1 1 0;
0 1 0 1;
0 0 0 1;
0 1 1 1]
Now, A is satified the above condition. Is it possible to implement it in MATLAB? Thank all
Second example
A=[1 0 0 1;
0 0 1 1;
0 0 0 1]
Then result is
A=[1 0 0 1;
0 1 1 0; %% second and fourth col is switched
0 0 0 1]
Update: what is happen if the row of A is comming on fly. It means that at t=0, the first row comes and A=[1 0 0 1]. Then next time, the second row comes. The matrix A will be A=[1 0 0 1; 0 0 1 1]. Then the algorithm will be check here because the second col. of A is zero. Performs switching and then next col of A comes, so on. Could you design help me the implementation for that task?
Simple deterministic strategy, start from the top left, fill as many columns as you can with the first row, then continue with the next row and the next columns. For the remaining rows, just start over at the first column.
%Get rows in which ones can be found.
[~,r]=find(A.');
%Assign new column values for the ones
c=mod(0:numel(r)-1,size(A,2))+1;
B=zeros(size(A));
B(sub2ind(size(A),r(:),c(:)))=1;
I guess this would do (at least for tall matrices)
for ind = 1:min(size(A))
t = A(ind,:);
A(ind,:) = circshift(t,[0 -(find(t)-ind)]);
end
I found the dumbest way to do this :D
A=[0 0 0 1
0 1 1 0;
0 1 0 1;
0 0 0 1;
0 1 1 1];
while ~(any(A(:,1)) && any(A(:,2)) && any(A(:,3)) && any(A(:,4)))
for ii = 1:length(A(:,1))
A(ii,:) = A(ii,randperm(4,4));
end
end
disp(A)
The code checks each column in A to have 1. If not, it randomly shifts rows in A and repeats until the requirement is satisfied.

Postscript layout from top to bottom

Hi I have a postscript file that layouts an image from left to right.
%%% Temporary
/Fix_long 4.2 cm def
/Fix_short 3.2 cm def
%%% Set Image Scale
/SetFixScale { 2 copy gt { Fix_long Fix_short }{ Fix_short Fix_long }ifelse scale } bind def
%%% Set put coordinate
/SetXAdjust { 2 copy gt
{ X_Step Fix_long sub 2 div floor }
{ Fix_long Fix_short sub 2 div} ifelse /XAdjust exch def
} bind def
/YAdjust 1.0 cm def
%%% Temporary
/Row 4 def
/Column 5 def
/X_Step urx llx sub Row div floor def
/Y_Step ury lly sub Column div floor def
/Row_pos 0 def
/Column_pos 1 def
/SetPutPosition {
llx X_Step Row_pos mul add
ury Y_Step Column_pos mul sub translate
DrawFrame
DrawFileName
XAdjust YAdjust translate
Row 1 sub Row_pos eq { /Row_pos 0 def /Column_pos Column_pos 1 add def }{ /Row_pos Row_pos 1 add def } ifelse
Column_pos Column gt { /Column_pos 1 def } if
} bind def
I tried changing the postscript to layout the images from top to bottom. I can layout the image from top to bottom but I can only put it on the first column.
/SetPutPosition {
llx X_Step Row_pos mul add
ury Y_Step Column_pos mul sub translate
DrawFrame
DrawFileName
XAdjust YAdjust translate
Row 1 sub Row_pos eq { /Row_pos 0 def /Column_pos Column_pos 1 add def }{ /Column_pos Column_pos 1 add def } ifelse
Column_pos Column gt { /Row_pos 1 def } if
} bind def
Not all the program is given, for example llx, lly, urx and ury are not defined. SO its impossible to reproduce what you are doing.....
The definition of rows and columns seems odd to me too, since rows increment in the x direction and columns decrement in the y direction.
I'd assume that the program operates in a loop (also not shown). On each iteration of the loop it sets the position on the page to:
x = llx + (X_Step * Row_pos)
y = ury - (Y_Step * Column_pos)
Then program then subtracts 1 from Row and compares with Row_pos. If they are the same then we reset the row, otherwise we add 1 to Row_pos. In effect we increment Row_pos until we get to Row - 1.
Now, if we reset the row, then we set Row_pos back to 0, and add 1 to column_pos.
Finally, we compare Column_pos and Column, if Column_pos is greater than Column, then we reset Column_pos to 1. Since we would have also reset Row_pos in the preceding control block, this is effectively a complete page reset, and starts again from the initial values.
Your code starts by checking Row_pos against Row again (when you should be checking Column_pos against Column). If Row_pos hasn't reached Row then you add 1 to Column_pos. Then you check Column_pos against Column and if its greater you reset Row_pos.
Notice that the only way you can alter Row_pos is if Row_pos is equal to Row - 1
If it is, then you reset Row_pos to 0. After that its impossible to increment Row_pos unless Row is 1.
Basically your logic is broken.
You want to compare Column_pos against Column - 1. When they are equal you want to set Column_pos to 1 and increment Row_pos, otherwise you want to increment Column_pos. Finally, if Row_pos is greater than Row, you want to reset Row_pos top 0.
So, bearing in mind thaqt I can't test this, because not all the code is present, something like :
/SetPutPosition {
llx X_Step Row_pos mul add
ury Y_Step Column_pos mul sub translate
DrawFrame
DrawFileName
XAdjust YAdjust translate
Column 1 sub Column_pos eq { /Column_pos 1 def /Row_pos Row_pos 1 add def }{ /Column_pos Column_pos 1 add def } ifelse
Row_pos Row gt { /Row_pos 0 def } if
} bind def

Extract column data from a simple text table

I got such stdout.
Queues
queue dur autoDel excl msg msgIn msgOut bytes bytesIn bytesOut cons bind
==============================================================================================================================
14531c8d-dd9b-4f41-9d92-c1344774d21c:0.0 Y Y 0 0 0 0 0 0 1 2
qmfagent-425fa29c-0892-4c08-a2d9-e7331a37dc13 Y Y 0 0 0 0 0 0 1 4
So I need to parse this output and get only something like this
14531c8d-dd9b-4f41-9d92-c1344774d21c:0.0
qmfagent-425fa29c-0892-4c08-a2d9-e7331a37dc13
Can anybody tell me how to do this ruby? Of course there could be more lines.
Split lines by newline (\n). Get last two lines.
output = <<EOD
Queues
queue dur autoDel excl msg msgIn msgOut bytes bytesIn bytesOut cons bind
==============================================================================================================================
14531c8d-dd9b-4f41-9d92-c1344774d21c:0.0 Y Y 0 0 0 0 0 0 1 2
qmfagent-425fa29c-0892-4c08-a2d9-e7331a37dc13 Y Y 0 0 0 0 0 0 1 4
EOD
lines = output.strip.split("\n") # Split lines by newline
last_two_lines = lines[-2..-1] # Get the last 2 lines.
p last_two_lines.map {|line| line.split[0]} # Get the first fields.
prints
["14531c8d-dd9b-4f41-9d92-c1344774d21c:0.0", "qmfagent-425fa29c-0892-4c08-a2d9-e7331a37dc13"]
queues = <<EOS
queue dur autoDel excl msg msgIn msgOut bytes bytesIn bytesOut cons bind
==============================================================================================================================
14531c8d-dd9b-4f41-9d92-c1344774d21c:0.0 Y Y 0 0 0 0 0 0 1 2
qmfagent-425fa29c-0892-4c08-a2d9-e7331a37dc13 Y Y 0 0 0 0 0 0 1 4
EOS
queues.lines.each {|line|
puts line.split.first if line =~ /[[\da-f]]{4}/i # detects 4 consecutive hexadecimals
}

Resources