I have one table:
a b
x 5
y 20
z 10
i want to have a sum of column b but without aggregation, thus i would get this as result:
a b c
x 5 35
y 20 35
z 10 35
This should be possible right? How this the Select comming from the table above?
You can use window functions for that:
select a, b, sum(b) over () as c
from the_table
order by a
Related
I have a table:
q)t:([] sym:`AAPL`MSFT`AMZN`AAPL`MSFT`AMZN; px:1 2 3 11 22 33; sh:100 200 300 1000 2000 3000)
I want to get the last px and sh by sym which can be obtained using last function two times:
q)select last px, last sh by sym from t
sym | px sh
----| -------
AAPL| 11 1000
AMZN| 33 3000
MSFT| 22 2000
How can we use last keyword only once to get above output?(Because in practical cases sometimes we need to use last on more than 30 columns)
My Failed attempts:
q)select last (px;sh) by sym from t
q)select {last x}#'(px;sh) by sym from t
For cases like this, using a by phrase together with no selection of columns is the same as applying last to all columns.
select by sym from t
Should do the trick
q)(select last px, last sh by sym from t)~select by sym from t
1b
A common approach to the problem is to use fby which allows you to apply a function such as first or last (or a lambda) across all columns:
t:([]c1:10?`A`B`C;c2:10?10;c3:10?"xyz";c4:.z.D-til 10)
q)select from t where i=(last;i)fby c1
c1 c2 c3 c4
-------------------
A 9 z 2019.10.01
C 7 y 2019.09.29
B 0 x 2019.09.28
q)select from t where i=({first x};i)fby c1
c1 c2 c3 c4
-------------------
B 6 x 2019.10.07
C 6 x 2019.10.06
A 4 y 2019.10.02
To answer your question in a comment regarding applying a different function per column, you would have to use a functional select such as:
q)?[t;();{x!x}1#`c1;{x!((first;last)x in `c2`c4),'x}cols[t]except`c1]
c1| c2 c3 c4
--| ----------------
A | 9 y 2019.10.01
B | 0 x 2019.09.28
C | 7 x 2019.09.29
This uses last for columns c2 and c4, then uses first for the other columns
person quantity
A 2
B 3
C 4
D 5
E 8
F 10
G 15
H 20
I 55
J 30
K 21
L 4
M 6
N 10
O 25
P 22
No of people having quantity <25
No of people having quantity >25
please help for the above questions
First what you need to do is make a parameter of the type string:
Then you make a field that is working from the values set in the parameter. Use a case statement which has the logic "If the parameter is x, do this; if y do this"
For the "do this" part, you can use an if statement as well:
CASE [Quantity Paramter]
WHEN ">= 25 Quantity" THEN IF [Quantity] >= 25 THEN [Person] END
WHEN "< 25 Quantity" THEN IF [Quantity] < 25 THEN Person END
END
This newly created field is your new Person field and so you should drag that onto the Columns or Rows pills
Make sure to exclude the null values by using a filter on your new field. The null values in this context are the values that are not satisfying what you've chosen in the parameter. So if you've selected >=25, then a person with 2 would be a null
The below is demonstrating it working with the sample data:
Person Quantity
A 3
B 57
C 4
D 5
E 10
F 50
G 7
H 3
I 2
I have data which includes 2 columns, ages, and groups similar to
B 1 B 1 B 1 B 4 B 5 B 8 D 2 D 2 D 3 D 3 D 3 D 4 D 6 D 7 D 9 D 9
In Tableau, I wish to plot a line for each group B and D, % number of records(observations) (of group in group), against the age range, 1 to 9.
So B 1 - 3/6*100, B 5 1/6*100, D 3 - 3/10*100.
Any help or pointers would be really appreciated.
Enda
Drag 'Age' measure in columns.
Drag 'Group' dimension in 'Color'
Drag the tableau default measure of 'Number of Records' in rows. Make it's aggregation as 'sum', add quick table calculation of 'Percent of Total'. Change it's 'Compute Using' to 'Age'.
That's it! Hopefully this is what you were trying to do.
Consider the following table:
sym A B
X 1 2
Y 4 1
X 6 9
Z 6 3
Z 3 7
Y 1 8
I want to find the minimum A value and maximum B value for each of my symbols X, Y & Z and display them in a new table, i.e.
sym minA maxB
X 1 9
Y 1 8
Z 3 7
Thanks.
This should do it;
select minA:min A, maxB:max B by sym from table
x a b c d
----------
A 1 2 3 4
B 5 6 7 8
C 6 7 8 9
I want my sum of A = 1 + 2 + 3 + 4 and so for B and C, Is there any command that can sum row of data in PostgreSQL?
There is no such built-in function, but you can simply do the following:
select x, a+b+c+d as column_sum from mytable
Assuming, of course, that the data type of a, b, c and d are numeric.