I'm trying to build a sales query for a rolling 12 months. I know my sales by customer, and my total sales. I'm trying to sum up the top 80% of sales $ and give a count on how many customers make up that 80%. Any ideas? I have a result set that looks like the below. Thanks in advance!
Customer Sales TotalSales PercentOfSales
8585 19788.81 769658.68 0.03
8429 19598.26 769658.68 0.03
2837 19431.29 769658.68 0.03
6071 19398.11 769658.68 0.03
5027 19223.13 769658.68 0.02
6677 19204.90 769658.68 0.02
I actually have similar issue, only difference is i'm using Hive. i'm not sure if you've tag your question properly, think it's more than a sql-server-2008 issue.
my solution is:
query (use sql) all the records in order of desc.
loop all the records (use any other program languages) to accumulate the value of sales, and put the current customer into a list (say "VIPList")
loop stops when accumulated value is >= the 80% of total value
then all the customer in the VIPList that are what you wanted.
Related
I have a table in SQL Server with just 2 columns, the ProductID and the Discount.
There are many products registered in rows.
So, I want to know how many products there are in this table with the same discount, categorized by the discount column value.
How can I query this by the easiest and simplest way?
The view that I expect for:
Discount - QTD
-------------------
0.25 - 150
0.40 - 320
0.75 - 532
It seems like a simple aggregation should do the trick.
Select Discount
,QTD = count(*)
From YourTable
Group By Discount
In my SQL course I'm trying to answer a problem in which i'm being asked to find X% of a Total then subtracting that result from the original total to produce another result. Then putting these results (the X% of the total and the total-X% of total) in two new columns.
Example: We need to know how much money we owe Tom and Ted. We have total up sales to $1,000,000.00. We owe Tom 75% of that total. The remainder goes to Ted.
I can't seem to find anything in my readings/videos about this nor a google search that isn't an answers that produces ratios or comparing to other records in the table. Also, not sure about how to get the results into their own columns. Thanks for any advice!
Example of what I got so far:
SELECT SUM(Sale_Amount) From Order_Table
Now I have to find the % of that SUM then subtract it from the SUM and push both results to two new columns, one for the percent of the SUM(Sale_Amount) and one for the remainder.
Given it's an SQL course (and it's not 100% clear what's being asked), I'm not going to give you the total answer, but I'll give you components but you'll need to understand them to put them together.
In SQL, you can
Get totals using SUM and GROUP BY
Do normal maths e.g., SELECT 10000 * 60/100 to get percentages of totals
'Save' results by a) having columns/fields to save them in, and b) UPDATE those fields with relevant data
Note if you're not saving data, and simply reporting them, you can just add those to a SELECT statement e.g., SELECT 100000 AS Total, 100000 * 0.75 AS Toms_Share, 100000 * 0.25 AS Teds_Share.
I am working on the workforce analysis project. And I did some case when conditional calculations in Google Data Studio. However, when I successfully conducted the creation of the new field, I couldn't do the calculation again based on the fields I created.
Based on my raw data, I generated the start_headcount, new_hires, terminated, end_headcount by applying the Case When conditional calculations. However, I failed in the next step to calculate the Turnover rate and Retention rate.
The formula for Turnover rate is
terms/((start_headcount+end_headcount)/2)
for retention is
end_headcount/start_headcount
However, the result is wrong. Part of my table is as below:
Supervisor sheadcount newhire terms eheadcount turnover Retention
A 1 3 1 3 200% 0%
B 6 2 2 6 200% 500%
C 6 1 3 4 600% 300%
So the result is wrong. The turnover rate for A should be 1/((1+3)/2)=50%; For B should be 2/((6+6)/2)=33.33%.
I don't know why it is going wrong. Can anyone help?
For example, I wrote below for start_headcount for each employee
CASE
WHEN Last Hire Date<'2018-01-01' AND Termination Date>= '2018-01-01'
OR Last Hire Date<'2018-01-01' AND Termination Date IS NULL
THEN 1
ELSE 0
END
which means if an employee meets the above standard, will get 1. And then they all grouped under a supervisor. I think it might be the problem why the turnover rate in sum is wrong since it is not calculated on the grouped date but on each record and then summed up.
Most likely you are trying to do both steps within the same query and thus newly created fields like start_headcount, etc. not visible yet within the same select statement - instead you need to put first calculation as a subquery as in example below
#standardSQL
SELECT *, terms/((start_headcount+end_headcount)/2) AS turnover
FROM (
<query for your first step>
)
I have two calculated MDX measures based on units and discount. I need to calculate sum of units for each discount value.
I need to perform this calculation only in MDX (2008). Similar to sum over( partition by discount) in SQL.
Example
Product Disc Units SumofUnits
Mango 5% 10 15
Apple 5% 5 15
Apple 7% 8 18
Apple 7% 10 18
Any help is greatly appreciated.
Could you please provide more details about your cube structure? Do you have the Discount attribution? If so, you have to run the following:
SUM(
[Discount].[Discount].[Discount].Members,
[Measures].[Disc]
)
I have a database with 169461 records. Within one field is begin_milepost that numerically counts up from 0 at 0.01 intervals. I want to change the interval from 0.01 to 0.1 by making a new column that averages ten records at a time to create one record, and having it do this all the way down. I'm using access 2013.
Example would be 0.01,0.02,0.03,0.04,0.05,0.06,0.07,0.08,0.09,0.1 being turned into 0.055 and repeating on the next ten records.
See if this gives you what you are looking for:
SELECT (([Marker]-0.01)*100)\10 AS MarkerGroup, Sum([Marker])/Count([Marker]) AS AvgMarker
FROM tblMarkers
GROUP BY (([Marker]-0.01)*100)\10;