Having the following data in a table:
ID --------- Category --------- Value
1234 -------- Cat01 ----------- V001
1234 -------- Cat02 ----------- V002
1234 -------- Cat03 ----------- V003
1234 -------- Cat03 ----------- V004
1234 -------- Cat03 ----------- V005
I want to have the following output:
ID --------- Cat01 --------- Cat02 --------- Cat03
1234 ------- V001 ---------- V002 ---------- V003
1234 ------- V001 ---------- V002 ---------- V004
1234 ------- V001 ---------- V002 ---------- V005
How can it be done in PostgreSQL. As you can see, the value in Cat01 and Cat02 columns are repeated for each entry in Cat03 column
Many thanks for your help!
How about something like this:
SELECT a.val AS cat01, b.val AS cat02, c.val AS cat03
FROM
test_pivot AS a,
test_pivot AS b,
test_pivot AS c
WHERE
a.category = 'Cat01'
AND
b.category = 'Cat02'
AND
c.category = 'Cat03'
Related
We would like the subtotals from rollup to show at the beginning/end of each respective category. Below is the query on the following tables we tried. However, the Coalesce is not replacing the nulls with "total" because there are no nulls or "total" showing in the output, which means the rollup did not work. Our hypothesis is that something is not working with the sequence of the script (with the sequence). We are only familiar with Postgres sql. Thank you for any suggestions!
Tables
customers table:
| customer_id|customer_name|segment |
----------- ----------- ----------
|1 |Bob |Consumer |
|2 |Mary |Corporate |
|3 |Bill |Home Office|
|4 |Kathy |Consumer |
products table:
|product_id |category |sub_category|product_name |
-------------- ---------- ----------- -----------------------
|FUR-ADV-10000002|furniture |furnishings |Advantus Clock Ergonomic|
|FUR-BO-10000002 |furniture |bookcases |Bush Classic Bookcase |
|TEC-BRO-10000348|technology|copiers |Brother Copy Machine |
|TEC-EPS-10000053|technology|machines |Epson Printer Red |
|OFF-AR-10000019 |office sup|art |BIC Highlighters Blue |
|OFF-EAT-10000522|office sup|paper |Eaton Comp Printout |
orders tables:
|order_id |product_id |customer_id|sales |
-------------- --------------- ---------- -----
|AE-2016-1308551|FUR-ADV-10000002|1 |82.67 |
|AE-2016-1308552|FUR-BO-10000002 |1 |101.54|
|AE-2016-1308553|TEC-BRO-10000348|2 |79.28 |
|AE-2016-1308554|TEC-EPS-10000053|3 |101.23|
|AE-2016-1308555|OFF-AR-10000019 |4 |39.78 |
Query:
SELECT COALESCE(products.category, 'total'), COALESCE(products.sub_category, 'total') , SUM(orders.sales) AS total_sales
FROM products
INNER JOIN orders
ON products.product_id=orders.product_id,
(SELECT products.category, products.sub_category, SUM (orders.sales)
FROM orders
INNER JOIN products
ON orders.product_id=products.product_id
GROUP BY
ROLLUP (1,2)) t
GROUP BY 1,2
ORDER BY 3 desc;
To get an appropriate table rows count I thought to use a naive approach: use count 1 construct. And it works in a simple case:
q)t:([]sym:`a`a`b`b);
q)select cnt: count 1 by sym from t
sym| cnt
---| ---
a | 2
b | 2
But when I added other fields, I've got wrong result:
q)select cnt: count 1, sym by sym from t
sym| cnt sym
---| -------
a | 1 a a
b | 1 b b
Why does count 1 work (or just it seems so) in one column case and failed with multiple columns?
Upd: Expected to get something like this
sym| cnt sym
---| -------
a | 2 a a
b | 2 b b
I don't think count 1 will produce the result you're looking for, nor even a consistent one.
I think you might want to use count i instead. When selecting by sym you are specifying which column you want to count by.
q)t:([]sym:`a`a`b`b)
q)select cnt:count i,sym by sym from t
sym| cnt sym
---| -------
a | 2 a a
b | 2 b b
q).z.K
3.6
A point to note however is that this solution will not work on kdb+ 4.0.
q)t:([]sym:`a`a`b`b)
q)select cnt:count i,sym by sym from t
'dup names for cols/groups sym
[0] select cnt:count i,sym by sym from t
^
q).z.K
4f
For a where clause to filter on symbol columns, = works fine, but why does the match operator ~ not work?
q)t:([sym:`aa`bb]qty:20 30)
q)t
sym| qty
---| ---
aa | 20
bb | 30
q)select from t where sym=`aa
sym| qty
---| ---
aa | 20
q)select from t where sym~`aa
sym| qty
---| ---
Match is comparing `aa to the entire symbol column, where equals is comparing to each element
q)`a=`a`b`c
100b
q)`a~`a`b`c
0b
You could do
q)select from t where sym~\:`aa
sym| qty
---| ---
aa | 20
I have this RDD:
val resultRdd: RDD[(VertexId, String, Seq[Long])]
I want to count the distinct values in Seq of all records.
for example, if I have 3 records with Seq values as follows:
VertexId ------- String -------Seq[Long]
1 ----------------- x ------------- 1, 3
2 ----------------- x ------------- 1, 5
3 ----------------- x--------------- 2, 3, 6
the result should be = 5 , the count of {1,3,5,2,6}
Thanks :)
resultRdd.flatMap(_._3).distinct().count()
I have a project setup like this:
bin/fizzbuzz-game.pl
lib/FizzBuzz.pm
test/TestFizzBuzz.pm
test/TestFizzBuzz.t
When I run coverage on this, using
perl -MDevel::Cover=-db,/tmp/cover_db test/*.t
... I get the following output:
----------------------------------- ------ ------ ------ ------ ------ ------
File stmt bran cond sub time total
----------------------------------- ------ ------ ------ ------ ------ ------
lib/FizzBuzz.pm 100.0 100.0 n/a 100.0 1.4 100.0
test/TestFizzBuzz.pm 100.0 n/a n/a 100.0 97.9 100.0
test/TestFizzBuzz.t 100.0 n/a n/a 100.0 0.7 100.0
Total 100.0 100.0 n/a 100.0 100.0 100.0
----------------------------------- ------ ------ ------ ------ ------ ------
That is: the totally-uncovered file bin/fizzbuzz-game.pl is not included in the results.
How do I fix this?
Have you checked the documentation? The section on Selecting which files to cover seems most helpful. :) It looks like the +select option is the one you are looking for.
I figured out a work-around for this.
The core of this problem is that the uncovered code in the main file (fizzbuzz-game.pl) is not included in the coverage report, hence the overall percentage is wrong. The underlying problem is that substantial logic resides in the main file instead of testable modules. This is a smell (don't know which, but I'm pretty sure there is a name for "lots of logic in main()").
By getting rid of this smell, eg. moving all substatial code from bin/fizzbuzz-game.pl to lib/FizzBuzzGame.pm, the code can theoretically be tested, and can definitively be included in the test run.
The coverage report after this becomes:
----------------------------------- ------ ------ ------ ------ ------ ------
File stmt bran cond sub time total
----------------------------------- ------ ------ ------ ------ ------ ------
lib/FizzBuzz.pm 100.0 100.0 n/a 100.0 0.0 100.0
lib/FizzBuzzGame.pm 75.0 n/a n/a 75.0 100.0 75.0
Total 87.5 100.0 n/a 83.3 100.0 88.9
----------------------------------- ------ ------ ------ ------ ------ ------