Postgres select max id only if value of another column is negative - sql

So i need to write a query that will return some values but only if the latest record in that table for any user is less than 0. Here is what if been playing around with:
select
et.id,
et.user_id,
et.amount,
et.trans_type,
COALESCE(et."endingBalance", 0) AS current_balance,
et."processId",
upu.email,
et.created_at
from "users-permissions_user" upu
join employer_transactions et
on upu.id = et.user_id
and et.id = (
select max(et2.id) from employer_transactions et2 where et2."endingBalance" < 0
)
and this is what is is returning:
id
user_id
amount
trans_type
current_balance
email
created_at
1946
333
150
CREDIT
-900.31
...
...
but if i run this query to test that query for that user_id:
select
id,
user_id,
amount ,
trans_type,
"endingBalance"
from employer_transactions et
where user_id = 333
order by id desc;
here is what i see:
id
user_id
amount
trans_type
ending_balance
1952
333
3
DEBIT
1297.31
1951
333
1
DEBIT
1299.31
1950
333
2
DEBIT
1298.31
1947
333
400
CREDIT
1300.31
1946
333
150
CREDIT
-900.31
so in this case what im looking for was for this query to have returned nothing because the record with the highest id is not negative
but lets say the sample data set is this:
id
user_id
amount
trans_type
ending_balance
900
333
3
DEBIT
-1297.31
899
333
1
DEBIT
1299.31
700
222
2
DEBIT
-1298.31
699
222
400
CREDIT
1300.31
600
111
150
CREDIT
900.31
599
111
150
CREDIT
-800.31
then what im looking for my query to return is
id
user_id
amount
trans_type
current_balance
email
created_at
900
333
3
DEBIT
-1297.31
...
...
700
222
2
DEBIT
-1298.31
...
...
because those were the latest records for that particular user_id and the current_balance was negative but noting for user_id: 111 becasue while yes there was a negative record but it wasnt the latest record for that user_id

SELECT T.*
FROM
(
SELECT *, RANK() OVER(PARTITION BY user_id ORDER BY id DESC) RNK
FROM trans_tbl
) T
WHERE T.RNK = 1 AND T.ending_balance < 0;
Fiddle

A common technique, for me, is to do a subquery that finds a desired subset. After that, do more filtering.
with ids as (
select max(id) as last_id
from t
group by user_id
)
select *
from t
where end_balance < 0
and id in (select last_id from ids)
;
The above gives desired output, given that the `id`'s are unique and form an ascending sequence.

Related

SQL - Summing/counting rows based on matching columns

I have the 2 following tables
Tracking
tracking_id item_extension quantity
a 144 100
b 144 200
c 250 150
Account
tracking_id account
a 999
b 999
c 999
Here's my query -
SELECT sum(qty) as qty, count(item_extension) as total, t.tracking_id, item_extension, account
FROM Tracking t
INNER JOIN Account a ON t.tracking_id = a.tracking_id
GROUP BY t.tracking_id, item_extension, account
What I want to happen here is get count of item_extension and sum of quantity based on matching account/item_extension fields. So because there are 2 rows with matching account and item_extension fields, it should sum up 2 of them like so:
qty total tracking_id item_extension account
300 2 a 144 999
300 2 b 144 999
150 1 c 250 999
Instead I get this result:
qty total tracking_id item_extension account
100 1 a 144 999
200 1 b 144 999
150 1 c 250 999
Is there a good way of doing this?
You want to count item_extension values that are not in the current row. So, use window functions. I think this does what you want:
SELECT sum(qty) as qty,
sum(count(*)) over (partition by item_extension) as total,
t.tracking_id, item_extension, account
FROM Tracking t
INNER JOIN Account a ON t.tracking_id = a.tracking_id
GROUP BY t.tracking_id, item_extension, account;

How to select rows where values changed for an ID

I have a table that looks like the following
id effective_date number_of_int_customers
123 10/01/19 0
123 02/01/20 3
456 10/01/19 6
456 02/01/20 6
789 10/01/19 5
789 02/01/20 4
999 10/01/19 0
999 02/01/20 1
I want to write a query that looks at each ID to see if the salespeople have newly started working internationally between October 1st and February 1st.
The result I am looking for is the following:
id effective_date number_of_int_customers
123 02/01/20 3
999 02/01/20 1
The result would return only the salespeople who originally had 0 international customers and now have at least 1.
I have seen similar posts here that use nested queries to pull records where the first date and last have different values. But I only want to pull records where the original value was 0. Is there a way to do this in one query in SQL?
In your case, a simple aggregation would do -- assuming that 0 is the earliest value:
select id, max(number_of_int_customers)
from t
where effective_date in ('2019-10-01', '2020-02-01')
group by id
having min(number_of_int_customers) = 0;
Obviously, this is not correct if the values can decrease to zero. But this having clause fixes that problem:
having min(case when number_of_int_customers = 0 then effective_date end) = min(effective_date)
An alternative is to use window functions, such asfirst_value():
select distinct id, last_noic
from (select t.*,
first_value(number_of_int_customers) over (partition by id order by effective_date) as first_noic,
first_value(number_of_int_customers) over (partition by id order by effective_date desc) as last_noic,
from t
where effective_date in ('2019-10-01', '2020-02-01')
) t
where first_noic = 0;
Hmmm, on second thought, I like lag() better:
select id, number_of_int_customers
from (select t.*,
lag(number_of_int_customers) over (partition by id order by effective_date) as prev_noic
from t
where effective_date in ('2019-10-01', '2020-02-01')
) t
where prev_noic = 0;

How to get latest records based on two columns of max

I have a table called Inventory with the below columns
item warehouse date sequence number value
111 100 2019-09-25 12:29:41.000 1 10
111 100 2019-09-26 12:29:41.000 1 20
222 200 2019-09-21 16:07:10.000 1 5
222 200 2019-09-21 16:07:10.000 2 10
333 300 2020-01-19 12:05:23.000 1 4
333 300 2020-01-20 12:05:23.000 1 5
Expected Output:
item warehouse date sequence number value
111 100 2019-09-26 12:29:41.000 1 20
222 200 2019-09-21 16:07:10.000 2 10
333 300 2020-01-20 12:05:23.000 1 5
Based on item and warehouse, i need to pick latest date and latest sequence number of value.
I tried with below code
select item,warehouse,sequencenumber,sum(value),max(date) as date1
from Inventory t1
where
t1.date IN (select max(date) from Inventory t2
where t1.warehouse=t2.warehouse
and t1.item = t2.item
group by t2.item,t2.warehouse)
group by t1.item,t1.warehouse,t1.sequencenumber
Its working for latest date but not for latest sequence number.
Can you please suggest how to write a query to get my expected output.
You can use row_number() for this:
select *
from (
select
t.*,
row_number() over(
partition by item, warehouse
order by date desc, sequence_number desc, value desc
) rn
from mytable t
) t
where rn = 1

SQL - Case when product exists, fill up its corresponding value to all rows within the partition

I have the something like the following monthly data set.
I have a Product, company ID, Date, and Quantity. A company (denoted by Company ID) can buy multiple products. I want to create a new column that will have the quantity of Product 'C' if the company bought in the month at each line item. If Product 'C' is not bought, then return 0.
Product Company_ID Date Quantity Desired_Calculated_Column
A 1 5/1/2019 100 300
B 1 5/1/2019 200 300
C 1 5/1/2019 300 300
A 2 6/1/2019 150 125
B 2 6/1/2019 250 125
C 2 6/1/2019 125 125
A 3 7/1/2019 175 0
B 3 7/1/2019 275 0
I have been trying to partition the data based on Product and Company ID. I have been trying to leverage the LAST_VALUE but haven't been successful.
LAST_VALUE(quantity) OVER (PARTITION BY Date, Company_ID
ORDER BY product_group
) AS Desired_Calculated_Column
You don't want last_value(). You can use conditional aggregation, assuming that 'C' occurs once per group:
MAX(CASE WHEN product_group = 'C' THEN quantity ELSE 0 END) OVER
(PARTITION BY Date, Company_ID) AS C_quantity

How to select 6 top records of each individual records at the database when selecting from all rows

Assume that i have the following table
CREATE TABLE #tblUsersPokemons (
RecordId int NOT NULL,
PokemonId int NOT NULL,
PokemonExp int NOT NULL,
PokemonLevel int NOT NULL,
UserId int NOT NULL
)
Now the below query works awesome as expected
select
SUM(cast(PokemonExp as bigint)) as TotalExp,
MAX(PokemonLevel) as MaxPokeLevel,
Count(PokemonId) as TotalPoke,
UserId
from #tblUsersPokemons
group by UserId
Here example result of such query
ToplamExp MaxPokeLevel TotalPoke UserId
----------- --------------- ----------- --------
29372294 101 4 1
1134696 98 1 2
1400 98 1 101
24534365 98 4 102
1400 98 1 1102
1400 98 1 1103
1400 98 1 2102
1400 98 1 2103
789220 98 7 2105
1468 98 1 3104
Now here my question comes
I want to limit counted PokemonIds. What i mean is i want to select maximum 6 of each same PokemonId records. And from these records top 6 ordered desc by PokemonExp should be counted in.
For example a user has the below records
From this table the query should take record id : 1,2,3,4,5,6,9 and not take 7,8 since top 6 records for PokemonId 1 taken
If I understand correctly, you want the aggregations on the top 6 rows for each user. You can do this easily using row_number():
select SUM(cast(PokemonExp as bigint)) as ToplamExp,
MAX(PokemonLevel) as MaxPokeLevel,
Count(PokemonId) as TotalPoke,UserId
from (select p.*,
row_number() over (partition by userid order by pokemanexp desc) as seqnum
from tblUsersPokemons p
) p
where seqnum <= 6
group by UserId;
EDIT:
I think you want to include PokemonId in the partition by clause:
select SUM(cast(PokemonExp as bigint)) as ToplamExp,
MAX(PokemonLevel) as MaxPokeLevel,
Count(PokemonId) as TotalPoke,UserId
from (select p.*,
row_number() over (partition by userid, PokemonId
order by pokemanexp desc) as seqnum
from tblUsersPokemons p
) p
where seqnum <= 6
group by UserId;