summarize unique value, except select only one of multipication - hive

I have a sample table as below
StoreID RepID OrderAmt
1234 CameronB 100,000
9075 HelenW 1,750,000
1234 JackM 700,000
7000 JasonA 325,000
705 JohnV 325,000
9075 MikeC 250,000
2111 PeterL 500,000
705 RickJ 660,000
501 ShareB 410,000
I would like to summarize OrderAmt by StoreID, however, need to pick only one RepID associated with the same store
Expected output
StoreID RepID Total_OrderAmt
1234 JackM 800,000
9075 MikeC 2,000,000
7000 JasonA 325,000
705 JohnV 985,000
2111 PeterL 500,000
501 ShareB 410,000
I am not sure if the following makes sense, if not, is there a proper way to achive the desired result?
Select StoreID, max(RepID) as RepID, sum(OrderAmt) as Total_OrderAmt
From HAVE
Group by StoreID, RepID

Related

sql sum column in having clause is not summing quarterly values

i would like to sum (purchase_prd) which is quarterly for customerID where purchase value is >0 and <=500. I have same customer ids in multiple purchase_prd, and would also like to see how many records show for their customerID....how do I query this?
I have the following
select purchase_prd, count(*), customerID, sum(purchase_value)
from table a
where purcahse_prd between 201700 and 201712 /*data is quarterly, so 201700, 201703, 201706,201709, 201712*/
group by customerid, purchase_value
having purchase_value >0 and purchase_value<=500
my results show customerids in multiple quarters and the sums of purchase_value exceeds 500, each quarter is separate and not extracting the total of purchase_value for the entire year with the criteria of purchase_value >0 and <=500
my results are:
purchase_prd customer ID purchase_value
201700 714 776
201703 714 120
201706 714 50
201709 714 20
201712 714 100
I'd like 2017 summed for customerID 714 and selected if sum of purchase_value is >0-<=500
I think you want:
select customerID, purchase_prd, count(*), sum(purchase_value)
from table a
where purchase_prd between 201700 and 201712 /*data is quarterly, so 201700, 201703, 201706,201709, 201712*/
group by customerid, purchase_prd
having sum(purchase_value) > 0 and sum(purchase_value) <= 500

SQL: count values in one table based on WHERE from other table

I am not sure, why this is not working.
I have two simple tables:
Orders
OrderTypeID EmployeeID Completion_needed
10308 72 15%
10309 73 20%
10310 74 30%
Customers
Customer ID OrderTypeID OrderDate Order_completed
1 10308 2015-09-18 5%
2 10309 2015-09-19 30%
3 10310 2017-09-20 25%
4 10308 2015-09-18 17%
2 10308 2015-09-19 20%
3 10309 2017-09-20 7%
I want to calculate how many customers have non completed orders, where Order_completed in the Customers table is less than Completion_needed in the Orders table (please not that a customer can have more than one order type).
This is my query, but I get the wrong result:
SELECT COUNT(c.CustomerID) as count_employees
FROM Orders od
JOIN Customers c
ON od.OrderTypeID = c.OrderTypeID
WHERE od.Completion_needed > c.Order_completed
I get 1; but I should get the count of 2.
I don't see how you get "1" from your query. I see it producing "3". So, I think what you need is COUNT(DISTINCT):
SELECT COUNT(DISTINCT c.CustomerId)
FROM Orders od JOIN
Customers c
ON od.OrderTypeID = c.OrderTypeID
WHERE c.Order_completed < od.Completion_needed;

Power Pivot - Inner Join like access, calculate and show grand total, possible?

I'm trying to acive the following with power pivot. Still couldn't figure out how. Is it possible?
Example from access
Gourped by “MatNr”. “Qty cases” are summed. From the “tbl_Weight” show the “Cases on trolley” per material. Divide “summed Qty cases” by “Cases on trolley” and show the results in the fourth column. Most importantly total the “summed Qty cases” column and the “Nr of trolleys per mat” column.
Two tables:
tbl_Sales
MatNr Qty cases
4564 100
4654 565
4564 100
4654 50
tbl_Weight
MatNr Cases on trolley
4564 10
4654 20
Query:
SELECT tbl_Sales.MatNr, Sum(tbl_Sales.[Qty cases]) AS [SummevonQty cases], tbl_Weight.[Cases on trolley], Sum([Qty cases]/[Cases on trolley]) AS [Nr of trolleys per mat]
FROM tbl_Sales INNER JOIN tbl_Weight ON tbl_Sales.MatNr = tbl_Weight.MatNr
GROUP BY tbl_Sales.MatNr, tbl_Weight.[Cases on trolley];
Expected results:
MatNr SummevonQty cases Cases on trolley Nr of trolleys per mat
4564 200 10 20
4654 615 20 30.75
Totals 815 50.75

postgres query for sales projections

=================
This is my query:
SELECT
SUM(sub_total) AS sales,
CASE WHEN (sub_total<100) THEN '0-99'
WHEN (sub_total>=100 AND sub_total<200) THEN '100-199'
WHEN (sub_total>=200 AND sub_total<300) THEN '200-299'
WHEN (sub_total>=300 AND sub_total<400) THEN '300-399'
WHEN (sub_total>=400 AND sub_total<500) THEN '400-499'
ELSE '500+'
END
AS product_sales_range
FROM order_item
GROUP BY sub_total
========================
It gives the ressult as:
sales product_sales_range
----- -------------------
10398.96 200-299
600 100-199
300 0-99
7699.78 300-399
6799.32 100-199
600 0-99
26599.24 500+
==========================================
I would like to get the result like this :
sales product_sales_range sales_increase_by_10% sales_increase_by_20%
----- ------------------- ------------------ ------------------
10398.96 200-299
600 100-199
300 0-99
7699.78 300-399
6799.32 100-199
600 0-99
26599.24 500+
============
Requirement:
Currently we have sales figures. Need to find out the sales projection as if the sales will be increased by 10%, 20%, 30% etc. Example: today the sales for product_sales_range (0-99) is 300. If the sales is increased by 10%, sales_increase_by_10% should be 330
Am I missing something, or do you just need to use:
SELECT SUM(sub_total) AS sales,
CASE WHEN (sub_total<100) THEN '0-99'
WHEN (sub_total>=100 AND sub_total<200) THEN '100-199'
WHEN (sub_total>=200 AND sub_total<300) THEN '200-299'
WHEN (sub_total>=300 AND sub_total<400) THEN '300-399'
WHEN (sub_total>=400 AND sub_total<500) THEN '400-499'
ELSE '500+'
END AS product_sales_range,
SUM(sub_total) * 1.1 AS sales_increase_by_10Percent,
SUM(sub_total) * 1.2 AS sales_increase_by_20Percent
FROM order_item
GROUP BY sub_total

Getting the max(price) for each item ordered, given multiple different prices for any ordered item

If I have an items_ordered table that looks like this:
items_ordered
customerid order_date item quantity price
10330 30-Jun-1999 Pogo stick 1 28.00
10101 30-Jun-1999 Raft 1 58.00
10298 01-Jul-1999 Skateboard 1 33.00
10101 01-Jul-1999 Life Vest 4 125.00
10299 06-Jul-1999 Parachute 1 1250.00
10339 27-Jul-1999 Umbrella 1 4.50
10449 13-Aug-1999 Unicycle 1 180.79
And I want to get the max price for each distinct item in the table, given that an item could appear multiple times in this table with different prices, how would I do that, assuming that this doesn't work:
select item, max(price) from items_ordered;
Add a Group By and you're golden.
select item, max(price) from items_ordered group by item;