Percent between two field PostgreSQL - sql

I have 3 fields which are called game_series_count, game_series_wins and game_series_lost. I need to find wins percent.
select
"game_series_count",
"game_series_wins",
"game_series_lost",
round(( (game_series_wins / game_series_count) * 100), 1) as win_percent
from "statistic_teams"
but there is wrong result
game_series_count | game_series_wins | game_series_lost | win_percent
2 1 1 0
2 1 1 0
2 1 1 0
2 1 1 0
1 1 0 100
1 1 0 100
1 0 1 0
1 0 1 0

You are doing integer division. The simplest method for your purpose is probably:
round(game_series_wins * 100.0 / game_series_count, 1) as win_percent
Note that you can also economize on parentheses.

Related

unpivot with counts in hive columns transpose

I have table with data needs to unpivot and get aggregated counts.
Source table:
primary_id sys_1 sys_2 sys3_ sy5 sys100
newa889 0 1 0 1 0
den7899 1 1 1 1 0
geo8988 1 1 1 1 0
atla8766 0 1 0 1 1
chic7898 0 1 0 0 1
Desired output:
sys_name count(primary_key) flag_0_or_1
sys_1 129999 0
sys_1 544545 1
sys_2 23333 0
sys2 23322323 1
sys3_ 332233 0
sys3_ 323232 1
sy5 32332 0
sy5 32323 1
Looking to get the data transpose get 0's and 1's counts from each sys_ column.

Pairwise swapping of rows in sql

I have a device settings table with threshold types and values.
Let's say ThresholdType = 0 is Min and ThresholdType = 1 is Max
The initial table looks like this:
DeviceID ThresholdType ThresholdValue
1 0 5
1 1 10
2 0 15
2 1 20
3 0 21
3 1 34
4 0 1
4 1 8
Then I had to change ThresholdType meaning - 0 became Max threshold and 1 became Min one.
I want the table look like that:
DeviceID ThresholdType ThresholdValue
1 0 10
1 1 5
2 0 20
2 1 15
3 0 34
3 1 21
4 0 8
4 1 1
Is it possible to change update it with a single SQL without loops?
Update ThresholdType instead:
update tablename set ThresholdType = 1 - ThresholdType
In case other ThresholdType values might show up later, you can add WHERE ThresholdType IN (1, 2), to be a bit safer.
Just swap the ThresholdType:
UPDATE t SET ThresholdType = CASE ThresholdType
WHEN 1 THEN 0
WHEN 0 THEN 1
ELSE ThresholdType
END
Execute the query exactly once.
You can do:
update t
set ThresholdValue = (
select x.ThresholdValue
from t x
where x.DeviceID = t.DeviceID and x.ThresholdType <> t.ThresholdType
)
Result:
DeviceID ThresholdType ThresholdValue
--------- -------------- --------------
1 0 10
1 1 5
2 0 20
2 1 15
See running example at db<>fiddle.

Return 1 if multiple columns are true, return 0 if only one column is true

I have a table that looks like the following:
id
bucket_1
bucket_2
bucket_3
bucket_4
7689
1
1
0
1
3456
1
0
1
1
4567
0
0
1
1
5677
1
0
0
0
4567
1
0
0
0
What I need to do is to write SQK code that looks at bucket 1 to 4 and if more than one of these columns have a value of 1, then mark it as 1 or true, and otherwise mark it as 0 or false.
Here is the table that I am trying to make:
id
bucket_1
bucket_2
bucket_3
bucket_4
result
7689
1
1
0
1
1
3456
1
0
1
1
1
4567
0
0
1
1
1
5677
1
0
0
0
0
4567
1
0
0
0
0
Could you help how this can be achieved? I am not sure how it can be done with the case clause.
You can use a case expression:
select t.*,
(case when bucket_1 + bucket_2 + bucket_3 + bucket_4 > 1
then 1 else 0
end) as result
from t;
You could directly do:
select t.*,
+(bucket_1 + bucket_2 + bucket_3 + bucket_4 > 1) result
from t

How to merge and count per column in a pivot table sql

I have a view with Columns:
WeekNo, MerchantId, Transactions
With a Select Query let's say that we have the following results:
TrnWeek AgencyId WeeklyTrn
1 110008 1
2 110008 2
3 110008 2
1 110045 4
3 110065 4
3 110124 1
1 110153 1
1 110155 3
2 110163 1
2 110165 1
making a pivot (stored procedure which creates dynamically the columns) i get the TrnWeek as Columns and as a result the following:
[1] [2] [3]
1 1 1
1 0 0
1 0 0
1 0 0
0 1 1
0 1 0
0 0 1
what I want to get is a "matrix" as follows:
TrnWeek 1 2 3
1 4 1 1
2 0 2 1
3 0 0 1
ih which i calculate how many merchants performed a transaction in the first week (position: 1,1), how many of them performed a transaction in the second one (position: 1,2), how many performed their first transaction in 2nd week (position: 2,2) etc.

stored procedure to find value in 2 columns out of 3

I am putting in the samle date and i am supposed to do something similar what i am asking.
I want to run a query that would pull values in any two columns out 3 if it has a 1 or if any one column has a 1 it will return just those results. However it should search all three columns and in any of the three columns where it found value as 1 it should return that result. Can anyone please help me with this. Thanks in advance.
ID Patient Patient Name prio prio2 prio3
-------------------------------------------------
1 101563 Robert Riley 1 1 1
2 101583 Cody Ayers 1 0 1
3 101825 Jason Lawler 0 0 1
4 101984 Dustin Lumis 1 0 0
5 102365 Stacy smith 1 0 0
6 102564 Frank Milon 1 0 0
7 102692 Thomas Kroning 1 0 0
8 102856 Andrew Philips 1 0 0
9 102915 Alice Davies 0 0 1
10 103785 Jon Durley 0 0 1
11 103958 Clayton Folsom 1 1 1
12 104696 Michelle Holsley 1 1 1
13 104983 Teresa Jones 1 0 1
14 105892 Betsy Prat 1 1 0
15 106859 Casey Ayers 1 1 0
So, basically you want to pull anything where any of the 3 columns prio,prio2, or prio3 =1? Please clarify your question if this isn't what you are asking( for a better answer). Also, you should tag it with what type of SQL.
SELECT ID,Patient,[Patient Name],prio,prio2, prio3
FROM uRtable
WHERE prio = 1 OR prio2 = 1 OR prio3 = 1
But, if you are saying that you want to pull back any row where any of the 3 columns prio,prio2, or prio3 = 1, but at least one of them is 0 (Get any where any of the 3 = 1 but exclude where they all = 1), probably the easiest way to understand that would be
SELECT ID,Patient,[Patient Name],prio,prio2, prio3
FROM uRtable
WHERE (prio = 1 OR prio2 = 1 OR prio3 = 1)
AND (prio = 0 OR prio2 = 0 OR prio3 = 0)
Try this:
select * from mytable
where prio + prio2 + prio3 = (
select max(prio + prio2 + prio3)
from mytable
where prio = 1 or prio2 = 1 or prio3 = 1
)
SELECT *
FROM tbl
WHERE 1 IN (prio,prio2,prio3)