how to find percentage without calculating manually? - sql

Thanks you for looking.I am new to tsql and dont know how to proceed. I have a table with 10 different companies and 20 department for each(the departments are same for all the companies).
I am trying to calculate percentage of expenses for each department and want an extra column 'Percentage' to be displayed in the result.
please note that for every company the first department is totalcompexpenses which is just the total expenses of the company for all the department combined and dont need to calculate that and should be calculated from the next row.
Is it possible to do this by using while loop or any other way instead of doing it manually for each one of them?
ID |Company_name| Department |Expenses | Percentage
1 |Company1 |TotalComp1Expenses |50000 | -
2 |Company1 |Department1 |4000 | ?
3 |Company1 |Department2 |8000 | ?
4 |Company1 |Department3 |8000 | ?
5 |Company1 |Department4 |7000 | ?
6 |Company1 |Department5 |10000 | ?
...
11 |Company2 |TotalComp2Expenses |100000 | -
12 |Company2 |Department1 |6000 | ?
13 |Company2 |Department2 |5000 | ?
15 |Company2 |Department3 |8000 | ?
15 |Company2 |Department4 |7000 | ?
16 |Company2 |Department5 |10000 | ?
...
21 |Company3 |TotalComp3Expenses |70000 | -
22 |Company3 |Department1 |2000 | ?
23 |Company3 |Department2 |7000 | ?
24 |Company3 |Department3 |9000 | ?
25 |Company3 |Department4 |8000 | ?
26 |Company3 |Department5 |10000 | ?
...

I think the clearest way is to use window functions. If you want the percentages based on the Total% columns, then you can do it as:
select ID, Company_name, Department, Expenses,
(100.0* Expenses /
max(case when Department like 'Total%Expenses' then Expenses end) over
(partition by Company_Name)
) as Percentage
from t;
You can also do this as a sum of the non-Total expenses:
select ID, Company_name, Department, Expenses,
(100.0* Expenses /
max(case when Department not like 'Total%Expenses' then Expenses end) over
(partition by Company_Name)
) as Percentage
from t;
The window function is like an aggregation function, but without the aggregation. The sum for each group is added as an additional column on each row. The definition of the grouping is based on the partition by clause.

Add this column to the query
Expenses * 200.0 / SUM(expenses) over (partition by company_name) as PercentageExepenses
You have to multiply expenses by 200.0 to take into account that you already have the total for the company and therefore double count.

if you self-join and thus have the total of each company in a separate column, you can calculate the percentages. the company total has 100% then, which i deem as correct
select
id
, company_name
, department
, expenses
, expenses/total*100 as percentage
from table_expenses tbx
inner join
(select
company_name
, sum(expenses/2) as expenses
from table_expenses
group by
company_name
) sums
on
(tbx.company_name = sums.company_name)

EDIT:
Are you actually storing the company totals in your database? If, so then this should work for the CTE:
select
compname,
expense as CompExp
from
<YourTable>
where
Department like 'Total%'
But I don't know why you would want to store subtotals like that.
Using your "table" as an example:
;with CompTotal as (
select
compname,
sum(expenses) as CompExp
from
<YourTable>
group by CompName)
select
C.CompName,
Department,
CompTotal.CompExp,
sum(Expenses)as DeptEexpense,
(sum(Expenses) / (CompTotal.CompExp * 1.0)) * 100 as Pct
from
<YourTable> C
inner join
CompTotal
on C.CompName = CompTotal.CompName
group by
C.CompName,
Department,
CompTotal.CompExp
The CTE gives us totals by company. We then join that back to the original table on company name, and total up by Department. Then just regular math gives us the percentage of each department of it's company total.
(SQLFiddle is down, or I'd link to a full example there)

Related

Cumulative Sum Query in SQL table with distinct elements

I have a table like this, with column names as Date of Sale and insurance Salesman Names -
Date of Sale | Salesman Name | Sale Amount
2021-03-01 | Jack | 40
2021-03-02 | Mark | 60
2021-03-03 | Sam | 30
2021-03-03 | Mark | 70
2021-03-02 | Sam | 100
I want to do a group by, using the date of sale. The next column should display the cumulative count of the sellers who have made the sale till that date. But same sellers shouldn't be considered again.
For example,
The following table is incorrect,
Date of Sale | Count(Salesman Name) | Sum(Sale Amount)
2021-03-01 | 1 | 40
2021-03-02 | 3 | 200
2021-03-03 | 5 | 300
The following table is correct,
Date of Sale | Count(Salesman Name) | Sum(Sale Amount)
2021-03-01 | 1 | 40
2021-03-02 | 3 | 200
2021-03-03 | 3 | 300
I am not sure how to frame the SQL query, because there are two conditions involved here, cumulative count while ignoring the duplicates. I think the OVER clause along with the unbounded row preceding may be of some use here? Request your help
Edit - I have added the Sale Amount as a column. I need the cumulative sum for the Sales Amount also. But in this case , all the sale amounts should be considered unlike the salesman name case where only unique names were being considered.
One approach uses a self join and aggregation:
WITH cte AS (
SELECT t1.SaleDate,
COUNT(CASE WHEN t2.Salesman IS NULL THEN 1 END) AS cnt,
SUM(t1.SaleAmount) AS amt
FROM yourTable t1
LEFT JOIN yourTable t2
ON t2.Salesman = t1.Saleman AND
t2.SaleDate < t1.SaleDate
GROUP BY t1.SaleDate
)
SELECT
SaleDate,
SUM(cnt) OVER (ORDER BY SaleDate) AS NumSalesman,
SUM(amt) OVER (ORDER BY SaleDate) AS TotalAmount
FROM cte
ORDER BY SaleDate;
The logic in the CTE is that we try to find, for each salesman, an earlier record for the same salesman. If we can't find such a record, then we assume the record in question is the first appearance. Then we aggregate by date to get the counts per day, and finally take a rolling sum of counts in the outer query.
The best way to do this uses window functions to determine the first time a sales person appears. Then, you just want cumulative sums:
select saledate,
sum(case when seqnum = 1 then 1 else 0 end) over (order by saledate) as num_salespersons,
sum(sum(sales)) over (order by saledate) as running_sales
from (select t.*,
row_number() over (partition by salesperson order by saledate) as seqnum
from t
) t
group by saledate
order by saledate;
Note that this in addition to being more concise, this should have much, much better performance than a solution that uses a self-join.

Oracle SQL Help Data Totals

I am on Oracle 12c and need help with the simple query.
Here is the sample data of what I currently have:
Table Name: customer
Table DDL
create table customer(
customer_id varchar2(50),
name varchar2(50),
activation_dt date,
space_occupied number(50)
);
Sample Table Data:
customer_id name activation_dt space_occupied
abc abc-001 2016-09-12 20
xyz xyz-001 2016-09-12 10
Sample Data Output
The query I am looking for will provide the following:
customer_id name activation_dt space_occupied
abc abc-001 2016-09-12 20
xyz xyz-001 2016-09-12 10
Total_Space null null 30
Here is a slightly hack-y approach to this, using the grouping function ROLLUP(). Find out more.
SQL> select coalesce(customer_id, 'Total Space') as customer_id
2 , name
3 , activation_dt
4 , sum(space_occupied) as space_occupied
5 from customer
6 group by ROLLUP(customer_id, name, activation_dt)
7 having grouping(customer_id) = 1
8 or (grouping(name) + grouping(customer_id)+ grouping(activation_dt)) = 0;
CUSTOMER_ID NAME ACTIVATIO SPACE_OCCUPIED
------------ ------------ --------- --------------
abc abc-001 12-SEP-16 20
xyz xyz-001 12-SEP-16 10
Total Space 30
SQL>
ROLLUP() generates intermediate totals for each combination of column; the verbose HAVING clause filters them out and retains only the grand total.
What you want is a bit unusual, as if customer_id is integer, then you have to cast it to string etc, but it this is your requirement, then if be achieved this way.
SELECT customer_id,
name,
activation_dt,
space_occupied
FROM
(SELECT 1 AS seq,
customer_id,
name,
activation_dt,
space_occupied
FROM customer
UNION ALL
SELECT 2 AS seq,
'Total_Space' AS customer_id,
NULL AS name,
NULL AS activation_dt,
sum(space_occupied) AS space_occupied
FROM customer
)
ORDER BY seq
Explanation:
Inner query:
First part of union all; I added 1 as seq to give 1
hardcoded with your resultset from customer.
Second part of union
all: I am just calculating sum(space_occupied) and hardcoding other
columns, including 2 as seq
Outer query; Selecting the data
columns and order by seq, so Total_Space is returned at last.
Output
+-------------+---------+---------------+----------------+
| CUSTOMER_ID | NAME | ACTIVATION_DT | SPACE_OCCUPIED |
+-------------+---------+---------------+----------------+
| abc | abc-001 | 12-SEP-16 | 20 |
| xyz | xyz-001 | 12-SEP-16 | 10 |
| Total_Space | null | null | 30 |
+-------------+---------+---------------+----------------+
Seems like a great place to use group by grouping sets seems like this is what they were designed for. Doc link
SELECT coalesce(Customer_Id,'Total_Space') as Customer_ID
, Name
, ActiviatioN_DT
, sum(Space_occupied) space_Occupied
FROM customer
GROUP BY GROUPING SETS ((Customer_ID, Name, Activation_DT, Space_Occupied)
,())
The key thing here is we are summing space occupied. The two different grouping mechanisms tell the engine to keep each row in it's original form and 1 records with space_occupied summed; since we group by () empty set; only aggregated values will be returned; along with constants (coalesce hardcoded value for total!)
The power of this is that if you needed to group by other things as well you could have multiple grouping sets. imagine a material with a product division, group and line and I want a report with sales totals by division, group and line. You could simply group by () to get grand total, (product_division, Product_Group, line) to get a product line (product_Divsion, product_group) to get a product_group total and (product_division) to get a product Division total. pretty powerful stuff for a partial cube generation.

SQL Get percent of bad records from total

i am relatively new to SQL. Each employee access an account for testing with a tech, sometimes it's a good attempt, sometimes it's bad, so I need to calculate the percentage of the bad attempts mostly, my report should look something like this:
SELECT
employee, event, total, percentage
FROM my_table
employee | event | total | percentage|
user1 | good | 50 | 50% |
user1 | bad | 50 | 50% |
Calculate the total in a subquery and then JOIN to calculate percentage on each row.
SELECT employee, event, COUNT(*), COUNT(*) * 100.0 / t.total as percentage
FROM my_table
JOIN (SELECT employee, count(*) total
FROM my_table
GROUP BY employee) T
ON my_table.employee = t.employee
GROUP BY employee, event
Try something like this calculate the bad event percentage for each employee
select employee,(sum(case when event = 'bad' then 1 else 0 end) / count(*)) * 100
From Yourtable
Group by employee

SQL Server: how to divide the result of sum of total for every customer id

I have 4 tables like this (you can ignore table B because this problem did not use that table)
I want to show the sum of 'total' for each 'sales_id' from table 'sales_detail'
What I want (the result) is like this:
sales_id | total
S01 | 3
S02 | 2
S03 | 4
S04 | 1
S05 | 2
S05 | 3
I have tried with this query:
select
sum(total)
from
sales_detail
where
sales_id = any (select sales_id
from sales
where customer_id = any (select customer_id
from customer)
)
but the query returns a value if 15 because they are the sum of those rows of data.
I have tried to use "distinct" before sum
and the result is [ 1, 2, 3 ] because those are distinct of those rows of data (not sum of each sales_id)
It's all about subquery
You are just so far off track that a simple comment won't help. Your query only concerns one table, sales_detail. It has nothing to do with the other two.
And, it is just an aggregation query:
select sd.sales_id, sum(sd.total)
from sales_detail sd
group by sd.sales_id;
This is actually pretty close to what the question itself is asking.

How to insert multiple columns value under a single column and separate the column name?

In the database there is a table
Name | id | Yearly_Profit | Yearly_Loss | Monthly_Profit | Monthly_loss
Alex 1 10 20 30 40
Ben 2 100 200 300 400
The output table will be like this
Name | id | Profit | Loss | Type
Alex 1 10 20 Yearly
Ben 2 100 200 Yearly
Alex 1 30 40 Monthly
Ben 2 300 400 Monthly
How can I do this?
Is this something like pivot or other?
You could use unpivot, but union all would be the simplest solution for you.
select Name, id, Yearly_Profit as Profit, Yearly_Loss as Loss, 'Yearly' as Type
from your_table
union all
select Name, id, Monthly_Profit , Monthly_loss, 'Monthly'
from your_table
This query would work for you, in this specific scenario.
SELECT
Name,
ID,
Yearly_Profit AS 'Profit',
Yearly_Loss AS 'Loss',
'Yearly' AS 'Type'
FROM Table
UNION ALL
SELECT
Name,
ID,
Monthly_Profit AS 'Profit',
Monthly_Loss AS 'Loss',
'Monthly' AS 'Type'
FROM Table
ORDER BY 5
But, if you'd have multiple columns in your table then you'd probably have to use a UNPIVOT.