SQL access Group By and Sum - sql

I'm having an issue with MS Access with my SQL statement.
I have companies and teams, and they each have a balance of money.
(Company1 can have team 1,2,3,4 and Company2 can have team 1,2,3,4,5. Though Comapny1 team1 is not the same as Company2 team1!)
But I have a ton of entries which each correspond to a seller.
I want to sum every balances for each company and each team, no matter which seller it is:
I actually have :
SELECT Company, Team, Sum(Balance) AS tot_balance
FROM Retro2014
GROUP BY Company, Team
But the amounts are 5 to 10 time bigger then they should be when i sum it manually. (But I have around 1200 seller, I can't do it all manually)
EDIT: What I want is something like this:
Company Team tot_balance
------- ---- -----------
Company1 Team1 1000
Company1 Team2 1530
Company1 Team3 120
Company1 Team4 500
Company2 Team1 800
Company2 Team2 750
Company2 Team3 420
Company2 Team4 820
Company2 Team5 120
... ... ...
EDIT2:
I have those values now :
Company Team tot_balance REAL_Balance
10 90 2 534.60 269.06
10 92 813.30 120.89
10 95 1 384.75 210.89
10 96 950.72 142.43
10 97 3 957.03 789.92
10 98 4 822.34 1128.71
EDIT3 : And the source values are those:
COMPANY TEAM SELLER BALANCE
10 50 123.65
10 90 L07630 245.06
10 90 L07630 4
10 90 L07630 8
10 90 L07630 4
10 90 L07630 8
10 92 L96420 32.93
10 92 L96420 87.96
10 95 35.74
10 95 16
10 95 4
10 95 12
10 95 12
10 95 131.15
10 96 L04771 65.5
10 96 L04771 12
10 96 L04771 8
10 96 L04771 8
10 96 L04771 48.93
10 97 L94605 61.93
10 97 L94605 4
10 97 L94605 8
10 97 L94605 233.76
10 97 L94605 344.97
10 97 L94605 90.33
10 97 L94605 38.93
10 97 L94605 4
10 97 L94605 4
10 98 L95652 42.51
10 98 L95652 34.75
10 98 L95652 549.26
10 98 L95652 320.36
10 98 L95652 20
10 98 L95652 112.58
10 98 L95652 41.25
10 98 L95652 8
Thanks,
Phil

As long as the table don't contain multiple entries on Company, Team, Balance
than you SQL should work just fine.
But given your explained issue, I pressume there are more values than what are shown and can therefore cause more rows with the same information shown more than once, which would result in an incorrect summerization. Here is what I would suggest:
Select Company, Team, Sum(Balance) as tot_balance from (
SELECT Company, Team, Balance
FROM Retro2014
GROUP BY Company, Team, Balance ) as b
GROUP by Company, Team

Related

SQL Server : how to count a column that if exist duplicate data, it reuse duplicate data first exist count number

Table A
shop
amount
count
sameShopCount
shop5
100
1
1
shop2
99
2
1
shop3
98
3
1
shop4
97
4
1
shop1
96
5
1
shop2
95
6
2
shop4
94
7
2
shop5
93
8
2
shop5
92
9
3
shop1
91
10
2
shop5
90
11
4
shop3
89
12
2
Expected Result (order by amount desc):
shop
amount
expected result
shop5
100
1
shop2
99
2
shop3
98
3
shop4
97
4
shop1
96
5
shop2
95
2
shop4
94
4
shop5
93
1
shop5
92
1
shop1
91
5
shop5
90
1
shop3
89
3
I want to count shop column similar to count column in Table A. But also if shop exist more than 1 time it will reuse the first exist count number.
How can I achieved this with/without a temp table in SQL Server respectively? (SQL Server 2014 - build v12.0.6108.1)
I had tried something like:
ROW_NUMBER() OVER (ORDER BY amount DESC)
DENSE_RANK() OVER (PARTITION BY shop ORDER BY amount DESC)
Try using max and dense_rank window functions as the following:
with max_shop_amount as
(
select *,
max(amount) over (partition by shop) as mx
from table_name
)
select shop, amount,
dense_rank() over (order by mx desc) expected
from max_shop_amount
order by amount desc
See demo

What is the best why to aggregate data for last 7,30,60.. days in SQL

Hi I have a table with date and the number of views that we had in our channel at the same day
date views
03/06/2020 5
08/06/2020 49
09/06/2020 50
10/06/2020 1
13/06/2020 1
16/06/2020 1
17/06/2020 102
23/06/2020 97
29/06/2020 98
07/07/2020 2
08/07/2020 198
12/07/2020 1
14/07/2020 168
23/07/2020 292
No we want to see in each calendar date the sum of the past 7 and 30 days
so the result will be
date sum_of_7d sum_of_30d
01/06/2020 0 0
02/06/2020 0 0
03/06/2020 5 5
04/06/2020 5 5
05/06/2020 5 5
06/06/2020 5 5
07/06/2020 5 5
08/06/2020 54 54
09/06/2020 104 104
10/06/2020 100 105
11/06/2020 100 105
12/06/2020 100 105
13/06/2020 101 106
14/06/2020 101 106
15/06/2020 52 106
16/06/2020 53 107
17/06/2020 105 209
18/06/2020 105 209
so I was wondering what is the best SQL that I can write in order to get it
I'm working on redshift and the actual table (not this example) include over 40B rows
I used to do something like this:
select dates_helper.date
, tbl1.cnt
, sum(tbl1.cnt) over (order by date rows between 7 preceding and current row ) as sum_7d
, sum(tbl1.cnt) over (order by date rows between 30 preceding and current row ) as sum_7d
from bi_db.dates_helper
left join tbl1
on tbl1.invite_date = dates_helper.date

Oracle - Group By Creating Duplicate Rows

I have a query that looks like this:
select nvl(trim(a.code), 'Blanks') as Ward, count(b.apcasekey) as UNSP, count(c.apcasekey) as GRAPH,
count(d.apcasekey) as "ANI/PIG",
(count(b.apcasekey) + count(c.apcasekey) + count(d.apcasekey)) as "TOTAL ACTIVE",
count(a.apcasekey) as "TOTAL OPEN" from (etc...)
group by a.code
order by Ward
The reason I have nvl(trim(a.code), 'Blanks') as Ward is that sometimes a.code is a blank string, sometimes it's a null.
The problem is that when I use the Group By statement, I can't use Ward or I get the error
Ward: Invalid Identifier
I can only use a.code so I get 2 rows for 'Blanks', as per below
1 Blanks 7 0 0 7 7
2 Blanks 23 1 1 25 30
3 W01 75 4 0 79 91
4 W02 62 1 0 63 72
5 W03 140 2 0 142 162
6 W04 6 1 0 7 7
7 W05 46 0 1 47 48
8 W06 322 46 1 369 425
9 W07 91 0 1 92 108
10 W08 93 2 0 95 104
11 W09 28 1 0 29 30
12 W10 25 0 0 25 28
What I need, is for the row with 'Blanks' to combined into 1 row. Little help?
Thanks.
You can not use the alias in the GROUP BY, but you can use the expression that builds the value:
GROUP BY nvl(trim(a.code), 'Blanks')

SUM column values based on two rows in the same tables in SQL

I have one table like below in my SQL server.
Trans_id br_code bill_no amount
1 22 111 10
2 22 111 20
3 22 111 30
4 22 111 40
5 22 111 10
6 23 112 20
7 23 112 20
8 23 112 20
9 23 112 30
and I want desired output like below table
s.no br_code bill_no amount
1 22 111 110
2 23 112 90
try this:
select br_code, bill_no, sum(amount)
from TABLE
group by br_code, bill_no

Exclude the specific kind of record

I am using SQL Server 2008 R2. I do have records as below in a table :
Id Sys Dia Type UniqueId
1 156 20 first 12345
2 157 20 first 12345
3 150 15 last 12345
4 160 17 Average 12345
5 150 15 additional 12345
6 157 35 last 891011
7 156 25 Average 891011
8 163 35 last 789521
9 145 25 Average 789521
10 156 20 first 963215
11 150 15 last 963215
12 160 17 Average 963215
13 156 20 first 456878
14 157 20 first 456878
15 150 15 last 456878
16 160 17 Average 456878
17 150 15 last 246977
18 160 17 Average 246977
19 150 15 additional 246977
Regarding this data, these records are kind of groups that have common UniqueId. The records can be of type "first, last, average and additional". Now, from these records I want to select "average" type of records only if they have "first" or "additional" kind of reading in group. Else I want to exclude them from selection..
The expected result is :
Id Sys Dia Type UniqueId
1 156 20 first 12345
2 157 20 first 12345
3 150 15 last 12345
4 160 17 Average 12345
5 150 15 additional 12345
6 157 35 last 891011
7 163 35 last 789521
8 156 20 first 963215
9 150 15 last 963215
10 160 17 Average 963215
11 156 20 first 456878
12 157 20 first 456878
13 150 15 last 456878
14 160 17 Average 456878
15 150 15 last 246977
16 160 17 Average 246977
17 150 15 additional 246977
In short, I don't want to select the record that have type="Average" and have only "last" type of record with same UniqueId. Any solution?
Using EXISTS operator along correlated sub-query:
SELECT * FROM dbo.Table1 t1
WHERE [Type] != 'Average'
OR EXISTS (SELECT * FROM Table1 t2
WHERE t1.UniqueId = t2.UniqueId
AND t1.[Type] = 'Average'
AND t2.[Type] IN ('first','additional'))
SQLFiddle DEMO
Try something like this:
SELECT * FROM MyTable WHERE [Type] <> 'Average'
UNION ALL
SELECT * FROM MyTable T WHERE [Type] = 'Average'
AND EXISTS (SELECT * FROM MyTable
WHERE [Type] IN ('first', 'additional')
AND UniqueId = T.UniqueId)
The first SELECT statement gets all records except the ones with Type = 'Average'. The second SELECT statement gets only the Type = 'Average' records that have at least one record with the same UniqueId, that is of type 'first' or 'additional'.