How to count and categorize values by the same criteria - sql

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

Related

Populate SQL Server table column with percentage

I have created a table that has a column for prices and I have another column called percentage. I want to be able to auto populate the percentage column by dividing a row's price by the total price*100. Is there any way that this is possible?
Example:
Price Percentage
-------------------
8.00 80%
2.00 20%
to get these percentage values:
(price in row 1 / sum of prices) * 100
(price in row 2 / sum of prices) * 100

SQL - how to show SKUs with the lowest price from a query

I am making a query for a price comparison site and I have to provide them a csv file with certain data. I have done the query and the first problem was that the prices that showed up in it were the full prices from sql database. So when we have for example a discount applied to the whole category of products the price shown is still the full price not the discounted one. I solved that by checking if an SKU is in a discounted category and the query calculates the price accordingly.
The new problem is now that a query checks through categories and if one sku is in multiple categories it creates a row with same SKU for each category and I end up with multiple rows with same SKU and only one has correct price (discounted one).
Example query result. NOTE that the Price column is a calculated result not the data from the table.
SKU
Product
Price
A
Ball
19
A
Ball
19
A
Ball
15
B
Cube
10
B
Cube
8
How do I filter it to just this
SKU
Product
Price
A
Ball
15
B
Cube
8
You need GROUP BY with aggregate function MIN()
SELECT SKU
, Product
, MIN(Price) as Price
FROM <table>
GROUP
BY SKU
, Product

SQL where function used with Over Partition

I am calculation a range of figures, one of the calculated columns are a weighted average calculation based on "Accountlink" by "DTstamp".
The problem is that I need the final result (per accountlink) to show in a new column - each Accountlink's final result per accountlink - to be used in a new calculation - as per example below:
Accountlink WA_Calc WA_Calc_Final
1 20.00 30.00
1 40.00 30.00
1 30.00 30.00
2 15.00 20.00
2 35.00 20.00
2 28.00 20.00
on the image is an extract of the script I am compiling.
However, I am just getting errors.
Any assistance and/or direction will be much appreciated.
You need a column that specifies the ordering. SQL tables represent unordered sets, so there is no "last row", unless a column specifies that.
Let me assume you have one. I'll just call it ?.
Then you can use first_value():
select t.*,
first_value(wa_calc) over (partition by id order by ? desc) as wa_calc_final
from t;

sql query and sum the two fields

I want to display unit price, quantity but my problem is I want to multiply the unit price * quantity. How to combine this 3 fields? I want the result like this:
UNITPRICE QUANTITY AMOUNT
2 3 6
2X3 equals 6.. please help me how to query this..
thank you newbie in multiplying table
I made this sqlfiddle for you. Perhaps it will help you.
select
UNITPRICE,
QUANTITY,
UNITPRICE * QUANTITY as [Amount]
from data

sum top 80% and count on how many customers make it up

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.