PostgreSQL tracking votes in a table? - postgresql

I have a table with a simple up/down vote column which I originally created as a boolean. true is a vote up, false is a vote down. However, I'm not sure how to use aggregate functions to achieve this kind of query result. For example, 5 true rows and 2 false should equal a vote of +3.
I'm thinking that I need to change the column to a smallint with +1 and -1. Is this correct? Is there a better way to query something like this?

No need to change the datatype, simply use a CASE to convert it to -1 and 1, then sum over the expression:
SELECT sum(case when vote_column then 1 else -1 end)
FROM your_table
To properly deal with NULL values, use the following
SELECT sum(case vote_column
when true then 1
when false then -1
else 0
end)
FROM your_table

Related

Using case expressions in a calculated column postgresql

Edited to provide sample data
I am trying to create a calculated column that sums 2 other columns, which should be easy. However, some values in both of the columns are null, so I want to use a case expression to replace null values in both columns with 0s and then add up the resulting values. The other complicating factor is that the second column contains text values with commas that need to be converted to numerical before I can add them. What I am currently trying to do is:
SELECT (case when pm."PS" is null then 0 else pm."PS" end) + (case when pm."PS-PREV1" is null then 0 else replace(pm."PS-PREV1", ',', '')::numeric end) AS "Sales"
FROM pm
Sample data:
PS
PS-PREV1
20000
null
30000
20,000
null
null
null
30,000
Desired output:
output
20000
50000
0
30000
This is just returning the value of the 1st column without adding in the second column. Where am I going wrong? Am I overthinking this?
your code should work , however you can write it a little bit more clean:
SELECT COALESCE(pm."PS",0)
+ COALESCE(replace(pm."PS-PREV1", ',', '')::numeric,0) AS "Sales"
FROM pm

How to get a sum of all rows that meets condition in postgres

I am trying to return sums with their specific conditions.
SELECT
COUNT(*),
SUM("transactionTotal" WHERE "entryType"=sold) as soldtotal,
SUM(case when "entryType"=new then "transactionTotal" else 0 end) as newtotal
FROM "MoneyTransactions"
WHERE cast("createdAt" as date) BETWEEN '2020-10-08' AND '2020-10-09'
I am trying to sum up the rows with "entryType"=sold and "entryType"=new and return those values separately.
obviously both my logic are wrong.
can someone lend a hand.
You were on the right track to use conditional aggregation, but your syntax is slightly off. Try this version:
SELECT
COUNT(*) AS total_cnt,
SUM(transactionTotal) FILTER (WHERE entryType = 'sold') AS soldtotal,
SUM(transactionTotal) FILTER (WHERE entryType = 'new') AS newtotal
FROM MoneyTransactions
WHERE
createdAt::date BETWEEN '2020-10-08' AND '2020-10-09';
Note: Assuming your createdAt column already be date, then there is no point in casting it. If it is text, then yes you would need to convert it, but you might have to use TO_DATE depending on its format.

Query works in mysql but does not display correctly in grafana

So I'm just building a small project that involves the use of MySQL and Grafana.
Here's my MySQL query and it's corresponding correct output:
select state, sum(total_cases) from PolDATA group by state order by sum(total_cases) desc limit 5;
Correct output:
I have enetered the same query in Grafana as well. Also, the format is Table and not Time Series. The output in Grafana is this: when I display as a gauge. Here I do not want the text to be sum(total_cases). I want it to display the name of the state. I tried altering it in Visualization->Field->Title but that changes all the labels to the same value. I even tried adding to the query select sum(total_cases) as "Sometext" but it changes all the labels to "Sometext" instead.
Help is appreciated. Thank you (:
First you need to configure your Grafana as the below image.
You need to write pivot table query as below.
select
sum(case when state = 'MAHARASHTRA' then total_cases else 0 end) as "MAHARASHTRA",
sum(case when state = 'GUJARAT' then total_cases else 0 end) as "GUJARAT",
sum(case when state = 'MADHYA PRADESH' then total_cases else 0 end) as "MADHYA PRADESH",
sum(case when state = 'RAJASTHAN' then total_cases else 0 end) as "RAJASTHAN",
sum(case when state = 'DELHI' then total_cases else 0 end) as "DELHI"
from PolDATA
group by state
E.g.
Note: If your "state" column is dynamic, then you need to try dynamic pivoting.
E.g. https://stackoverflow.com/a/12599372/4249637

Tableau - Error when COUNT boolean T/F

My goal here is to count the # of True, False and NULL per day using my boolean calculation. Next step would be to calculate the percentage of each T, F, N with overall day.
Below is the visualization I would like to achieve but instead of blue dots, I would need the count of them in number.
Initially, I tried
IF [boolean] THEN 1 ELSE 0 END
But only changing from True to 1, False to 0 and missing the NULL - not counting:
I also tried different Calculated Fields to get the count of it but always getting the same error:
SUM(IF [Field]=TRUE then 1 else 0 end)
COUNT(IF [Field]=TRUE then [ID] end)
ERROR:
Could someone please assist me with a Calculate Field or any other solution where I could get a count of T, F, and NULL that would also assist me with the percentage?
Thank you
You're working too hard.
Just put SUM(Number of Records) on one shelf with DAY(Disconnected ...) on another, with the Boolean field that classifies data rows as > 0 or not on some other shelf, such as Color. No need for a table calc.
Here is an image of a solution posted at https://community.tableau.com/message/601862#601862

Count the number of occurrences of Null in a column in tableau

I am relatively new to Tableau and I am wondering if there is a way to calculate null values in a column.
I have a column called Email of type string and want to know how many people have not entered their email i.e. Null.
I tried to create a calculated field with
count(ISNULL([Email]))
But this gives me the total count and not the count of null.
Thanks.
You cannot count NULL since COUNT ignores NULLs.
You can do this, though:
SUM(IF ISNULL([Email]) THEN 1 ELSE 0 END)
Per your additional comment, if you wanted to count where two fields are both NULL then:
SUM(IF ISNULL([Email]) AND ISNULL([Phone]) THEN 1 ELSE 0 END)
You can continue this for any number of fields, as needed.
You can use a filter for that field. Set it to only show you Null values, and set the measure to Count instead of the default Sum.
You can create another column where all null values are set to 1 and anything else to 0, >then count that. It should be something like
>COUNT(IF "null" THEN 1 ELSE 0)
A working syntax for that would had been:
SUM(IF ([Your field] = null) THEN 1
ELSE 0
END)
You can create another column where all null values are set to 1 and anything else to 0, then count that. It should be something like
COUNT(IF "null" THEN 1 ELSE 0)