SQL query to get maximum of all maximum values per customer - sql

I'm struggling with this specific Access 2010 SQL query for quite some time now. Let me first show you what my table looks like:
customerID value
123456789 100
123456789 -100
123456789 300
123456789 -300
123456789 150
123456789 -150
123456789 200
123456789 200
987654321 500
987654321 -500
987654321 200
987654321 -200
987654321 210
987654321 210
You see I have multiple entries for one customerID with several values. These values can be positive and negative. Negative values represent corrections so the corresponding positive value "gets nulled".
What I need to query now is the maximum value of all maximum values per customerID. In the example above, the maximum value of customerID 123456789 is 200, because all other values on this customerID annul each other. The maximum value on customerID 987654321 hence is 210.
Ultimately my query should return the value of 210 as the maximum out of all maximum values per customerID that didn't get corrected/anulled by negative values.
Can you please help me with this?
Edit: Added (duplicate) values 200 and 210 to both customerIDs to make clear that a SUM() wont work here.
Edit #2: Here's some (nearly) real life data: http://pastebin.com/TbNRTw5A

I don't know if this would be your answer, it's just assuming that all negative values have 1 corresponding equal positive value paired up.
SELECT CustomerID, SUM(Stack1.Value) FROM Stack1
GROUP BY CustomerID
So the result would be:
CustomerID Value
123456789 200
987654321 210
Hope this helps
How about this?
WITH tmpPositive AS (SELECT
Stack1.CustomerID, Stack1.Value FROM Stack1 WHERE Stack1.Value > 0),
tmpNegative AS (SELECT
Stack1.CustomerID, Stack1.Value FROM Stack1 WHERE Stack1.Value < 0)
SELECT tmpPositive.CustomerID, MAX(tmpPositive.Value) AS MaxValue FROM tmpPositive
LEFT OUTER JOIN tmpNegative
ON tmpPositive.CustomerID = tmpNegative.CustomerID AND
-tmpPositive.Value = tmpNegative.Value
WHERE tmpNegative.CustomerID IS NULL
GROUP BY tmpPositive.CustomerID;
Here's the test data:
CustomerID Value
---------------------
123456789 100
123456789 -100
123456789 300
123456789 -300
123456789 150
123456789 -150
123456789 200
987654321 500
987654321 -500
987654321 200
987654321 -200
987654321 210
123456789 200
123456789 110
987654321 1250
And the result I have for above query.
CustomerID MaxValue
--------------------
123456789 200
987654321 1250

Related

How to find the right budget value into the activities in Tableau (Relationship)

I have related two custom sql query in Tableau (via making relationship)
The outcome of the queries looks like :
Q1 : (It shows the starting time of the valid budget.If a user has multiple rows in this table, it means his/her budget has been updated with new amount)
id_user
budgete_start_date
budget_amount
1234
06-11-2021
120
1234
06-07-2022
200
56789
06-01-2022
1200
56789
06-07-2022
2000
643
05-05-2022
30
Q2 :(It shows the budget usage)
id_user
budgete_usage_date
amount_usage
1234
01-12-2021
50
1234
05-08-2022
100
56789
10-02-2022
60
56789
07-08-2022
500
643
05-07-2022
17
I need to find a way to create the following view to know what was the valid budget at
budgete_usage_date.
id_user
budgete_usage_date
amount_usage
valid budget
1234
01-12-2021
50
120
1234
05-08-2022
100
200
56789
10-02-2022
60
1200
56789
07-08-2022
500
2000
643
05-07-2022
17
30
How can I do that with calculated field in Tableau (with db made by relationship)?
If that's not possible, how can I do that directly in query? (changing the relationship to a single query)

How to groupby external time series data together

I have been trying to groupby some state data together. This is how my data looks like for example, with Date as the index and the rest is features:
Date
Population
Num_Men
Num_Women
State
Region
2020-01-01
500
300
200
NY
North
2020-02-01
800
500
300
GL
Middle
2020-02-01
1000
400
600
""
Middle
2020-02-01
200
50
150
nan
Middle
2020-02-01
600
400
200
NY
North
I know how to group the NY states ones, but if I want to group the ones with state values: GL, "", and nan together. I'm not sure how to do that.
I was looking for the final result to look like:
Date
Population
Num_Men
Num_Women
State
Region
2020-01-01
500
300
200
NY
North
2020-02-01
2000
950
1050
GL
Middle
2020-02-01
600
400
200
NY
North
I did something like this: df.groupby(df.index, {'State': ["GL", "", np.nan]}, but that didn't work. Any help would be appreciated! Thanks!
Let us do replace then groupby with sum and first
df.State = df.State.replace({"''":np.nan,'nan':np.nan})
out = df.groupby(['Region','Date'],as_index=False).\
agg({'Population':'sum',
'Num_Men':'sum',
'Num_Women':'sum',
'State':'first'})
Out[99]:
Region Date Population Num_Men Num_Women State
0 Middle 2020-02-01 2000 950 1050 GL
1 North 2020-01-01 500 300 200 NY
2 North 2020-02-01 600 400 200 NY

how to split one value from sql database into two rows

how to split one value from sql database and devide into two rows (one row set 1000000, one row is the balance) Others less than 1000000.or any idea using coldfusion . Thanks in advance.
Example
No. Code Name Account No Total
1 123 black 123456789 160000.00
2 124 red 111111222 5200.00
3 124 blue 444555666 121000.00
I want This result
No. Code Name Account No Total
1 123 black 123456789 100000.00
2 123 black 123456789 60000.00
3 124 red 111111222 5200.00
4 124 blue 444555666 100000.00
5 124 blue 444555666 21000.00
First you need to insert a new row:
INSERT INTO TABLE (CODE, NAME, ACCOUNT_NO, TOTAL)
SELECT
CODE,
NAME,
ACCOUNT_NO,
TOTAL MOD 100000 AS NEW_TOTAL
FROM TABLE
WHERE TOTAL > 100000;
That command will insert the second row. Now you need to fix the first row.
UPDATE TABLE
SET TOTAL = TOTAL - (TOTAL MOD 100000)
WHERE TOTAL > 100000;

How to maintain sequence number for particular record for longer time even if number of records inserted in order?

How to maintain sequence number for same record for longer time even if number of
records inserted
I want to insert the sequence number in reference_id column for same customer with different territory name
Row_id customer_name customer_id territory reference_id
1 A 100 USA 5000
2 B 200 UK 5500
3 C 300 LA 6000
4 A 100 CANADA 5001
5 B 200 CONGO 5501

Sum values from Duplicated rows

There's my sql data:
code name total
---------------
3 Sprite 2400
17 Coke 1500
6 Dew 1000
17 Coke 3000
6 Dew 2000
But code and name has duplicated values and I want to sum total from each duplicated field.
Something like this:
code name total
---------------
3 Sprite 2400
17 Coke 4500
6 Dew 3000
How could I do that in sql?
SELECT code, name, sum(total) AS total FROM table GROUP BY code, name