How to SUM multiple distinct values and get max value in SQL - sql

I'm a using distinct select in the Oracle SQL, what I want to do is sum up all the data at specific ID. Example in image:
So for example where PlayerIDFK is 1 I want to sum up TwoPointsMade in one column and ThrePointsMade in another, so for that the result would be
PlayerIDFK TwoPointsMade ThreePointsMade
--------------------------------------------------
1 5 2
The query Im using now is:
SELECT PlayerIDFK, TwoPointMade, ThreePointMade
FROM PlayerPerformance
WHERE PlayerIDFK IN (SELECT DISTINCT PlayerIDFK
FROM PlayerPerformance);

I think you could use the groupby-clause where you could group by playerIDFK and sum over twopointsmade and threepointsmade
Something like this might work:
SELECT PlayerIDFK, sum(TwoPointMade), sum(ThreePointMade) FROM PlayerPerformance GROUP BY PlayerIDFK
if you want to get the max you can do a nested query, something like this:
SELECT MAX(inside_query.m1, inside_query.m2) FROM (SELECT PlayerIDFK, sum(TwoPointMade) as m1, sum(ThreePointMade)as m2 FROM PlayerPerformance GROUP BY PlayerIDFK) as inside_query
there might be a more elegant way to do it but thats what I got :)

Maybe this might help:
SELECT * FROM (
SELECT PlayerIDFK, sum(TwoPointMade) as TwoPointMade
FROM PlayerPerformance
GROUP BY PlayerIDFK
ORDER BY sum(TwoPointMade) DESC
) WHERE ROWNUM = 1
UNION ALL
SELECT * FROM (
SELECT PlayerIDFK, sum(ThreePointMade) as ThreePointMade
FROM PlayerPerformance
GROUP BY PlayerIDFK
ORDER BY sum(ThreePointMade) DESC
) WHERE ROWNUM = 1

Related

How to only sum unique values in postgres?

I have a table below where I am trying to sum the order_value grouping by the unique order number. However, the queries I've tried are returning the entire sum. Expected return is: 34.24 but actual return is 50.37. How do I get the correct return value?
Order_Number
Order_Value
id
#1005
16.03
1
#1005
16.03
2
#1006
18.21
3
My query:
SELECT order_number,
SUM(order_value)
FROM customer_response
GROUP BY order_number ;
I've also tried:
SELECT SUM(order_value)
FROM customer_response
GROUP BY order_number, order_value
SELECT SUM(order_value)
FROM customer_response
GROUP BY order_number
This seems like it should be simple but I'm just not sure where I'm going wrong.
Very similar to your query but first extract only distinct rows for order_number.
SELECT order_number, SUM(order_value)
FROM
(
select distinct on (order_number) *
from customer_response
) t
GROUP BY order_number;
If you need to get the overall sum for all orders then remove the grouping.
SELECT SUM(order_value)
FROM
(
select distinct on (order_number) *
from customer_response
) t;
SQL Fiddle
Edit
Actually since only one row is left per order_number summing and grouping is meaningless. So the first query becomes simply
select distinct on (order_number) *
from customer_response;
You can try this :
SELECT sum(t.order_avg)
FROM
( SELECT avg(order_value) AS order_avg
FROM customer_response
GROUP BY order_number
) AS t
If you want to get the sum of all distinct Order_Values per Order_Number, you can group by Order_Number to get the sum in each group and then use SUM() window function to get the total:
SELECT DISTINCT SUM(SUM(DISTINCT Order_Value)) OVER () total_value
FROM customer_response t
GROUP BY Order_Number;
See the demo.

best way to get count and distinct count of rows in single query

What is the best way to get count of rows and distinct rows in a single query?
To get distinct count we can use subquery like this:
select count(*) from
(
select distinct * from table
)
I have 15+ columns and have many duplicates rows as well and I want to calculate count of rows as well as distinct count of rows in one query.
More if I use this
select count(*) as Rowcount , count(distinct *) as DistinctCount from table
This will not give accurate results as count(distinct *) doesn't work.
Why don't you just put the subquery inside another query?
select count(*),
(select count(*) from (select distinct * from table))
from table;
create table tbl
(
col int
);
insert into tbl values(1),(2),(1),(3);
select count(*) as distinct_count, sum(sum) as all_count
from (
select count(col) sum from tbl group by col
)A
I think I have understood what you are looking for. You need to use some window function. So, you query should be look like =>
Select COUNT(*) OVER() YourRowcount ,
COUNT(*) OVER(Partition BY YourColumnofGroup) YourDistinctCount --Basic of the distinct count
FROM Yourtable
NEW Update
select top 1
COUNT(*) OVER() YourRowcount,
DENSE_RANK() OVER(ORDER BY YourColumn) YourDistinctCount
FROM Yourtable ORDER BY TT DESC
Note: This code is written sql server. Please check the code and let me know.

Select Minimum value of column A where Column B=1

I have this table AVERAGE
I want to
select * from table where VhrNum=MIN(VhrNum) and EmptyOrNot=1
I've Tried this query but it's not working
select *
from Average
where Itmnum='1'
and VhrNum = (
select MIN(VhrNum)
from (
select *
from Average
where EmptyOrNot = '1'
)
)
In my case it's should select the third row
Why not just take the top 1 row and order by by the column?
SELECT TOP 1 *
FROM [table]
WHERE EmptyOrNot=1
ORDER BY VhrNum
Use TOP 1 with ORDER BY
select Top 1 * from table where EmptyOrNot=1
Order by VhrNum ASC
If you more than one record with min VhrNum value and you want all the tie records then use TOP 1 WITH TIES
select Top 1 With Ties * from table where EmptyOrNot=1
Order by VhrNum ASC
You can try like this
SELECT MIN(VhrNum) FROM table_name where EmptyOrNot=1;
Aggregate functions need a GROUP BY - or you could ORDER BY and select the first.
Aggregate won't let you do a SELECT *, which isn't really good practice anyway, and aggregate makes it clearer that you're actually trying to get the MIN of this. TOP 1/ORDER BY would let you do a SELECT *, but may be less immediately clear that all you're really trying to get is the MIN(VhrNum).
Aggregate:
SELECT MIN(VhrNum)
FROM Average
WHERE EmptyOrNot = 1
GROUP BY EmptyOrNot
Top:
SELECT TOP(1) VhrNum, *
FROM Average
WHERE EmptyOrNot = 1
ORDER BY VhrNum ASC
You can try this:
select top 1 * from table where VhrNum= (select min(VhrNum) from table);

Getting the ID From MAX(SUM()) SQL GROUP BY

I'm trying to get the PlayerID from the result I get in following image
The query I use to display that is:
SELECT PlayerIDFK, sum(TwoPointMade) as TwoPointMade, sum(ThreePointMade) as ThreePointMAde
FROM PlayerPerformance GROUP BY PlayerIDFK;
I also have another query which returns the maximum value of sums of both columns in this case 47, which is the correct answer, but I also want to get the PlayerIDFK.
SELECT MAX(SUM(TwoPointMade) + SUM(ThreePointMade)) AS "Points"
FROM PlayerPerformance GROUP BY PlayerIDFK;
When I try to get the player ID using the query I get not a single-group group function. This is the query Im trying to use:
SELECT PlayerIDFK, MAX(SUM(TwoPointMade) + SUM(ThreePointMade)) AS "Points"
FROM PlayerPerformance GROUP BY PlayerIDFK;
Use order by and rownum (or fetch first 1 row only in 12c+):
SELECT *
FROM (SELECT PlayerIDFK, sum(TwoPointMade) as TwoPointMade, sum(ThreePointMade) as ThreePointMAde
FROM PlayerPerformance
GROUP BY PlayerIDFK
ORDER BY ( sum(TwoPointMade) + sum(ThreePointMade) ) desc
) pp
WHERE rownum = 1;

Ensuring only distinct records are returned with DISTINCT

Given the following table:
date_field_one date_field_two arbitrary_value
---------------- ---------------- -----------------
1/1/11 1/3/11 cheese
1/1/11 1/4/11 the color orange
2/2/11 2/3/11 1
2/2/11 2/4/11 2
My problem: I'm not sure how to go about structuring a query using a set based approach that yields the following results:
for each distinct date, the record with the earliest
date_field_two value is returned
Any ideas?
Edit for new response! The solution posted by M.Ali may be the best fit for your specific case as it will ensure you only ever get one row result from your base data, even if there exist multiple candidate rows for your answer ( as in, date_field_one, date_field_two combinations are not distinct ). The following will return multiple results per date_field_one, date_field_two combination in the not-distinct scenario:
SELECT t.date_field_one, t.date_field_two, t.arbitrary_value
FROM ( SELECT date_field_one,
date_field_two = MIN( date_field_two )
FROM dbo.[table]
GROUP BY date_field_one ) dl
LEFT JOIN dbo.[table] t
ON dl.date_field_one = t.date_field_one
AND dl.date_field_two = t.date_field_two;
;WITH CTE
AS
(
SELECT *, rn = ROW_NUMBER() OVER (PARTITION BY date_field_one ORDER BY date_field_two
ASC)
FROM TableName
)
SELECT * FROM CTE
WHERE rn = 1
Something like this:
select date_field_one, min(date_field_two)
from yourtable
group by date_field_one
select date_field_one, min(date_fileld_two)
from table
group by date_field_one
try this for latest...........
select date_field_one ,min(date_field_two) date_field_two
from table group by date_field_one