I have a table like this structure:
lineID lineItemPrice
1 20
2 25
3 27
4 30
4 30
4 30
I want to get the sum of lineItemPrice where lineId is distinct.
I am not sure what should be sql query? Please help.
The output should be 102.
I cant quite tell if you are looking for this:
select
sum(lineItemPrice), lineID
from
table
group by lineID
Or this:
select
sum(lineItemPrice)
from
(select distinct lineID, lineItemPrice from table)
If you want the sum of the whole table:
select
sum(lineItemPrice)
from
table
The first would give results that would sum up all the lineItemPrice's for their respective lineID's
lineID lineItemPrice
1 20
2 25
3 27
4 90
The second would sum all these distinct records giving 102 as the answer
lineID lineItemPrice
1 20
2 25
3 27
4 30
The third:
lineItemPrice
162
Try this:
SELECT SUM(lineItemPrice) as TotalSum FROM
(SELECT lineItemPrice
FROM TableName
GROUP BY lineID,lineITemPrice) T
Result:
TOTALSUM
102
See result in SQL Fiddle.
Please try:
select
SUM(lineItemPrice) lineItemPrice
from(
select
distinct lineID, lineItemPrice
From tbl
)x
You can use below query if lineID lineITemPrice for a same value pair.
select
SUM(DISTINCT lineItemPrice) lineItemPrice
From tbl
This should work :
Select lineID, SUM(lineItemPrice) Total From YourTableName Group By lineID
Very Simple.
The GROUP BY statement is used in conjunction with the aggregate functions (we used SUM function here) to group the result-set by one or more columns.
Why Not this
SELECT * FROM
(
Select lineID, SUM(lineItemPrice) Over(Partition By lineID) Total
From TableName
) T
Group By lineID,Total
Fiddle Demo
Output:
lineID lineItemPrice
1 20
2 25
3 27
4 90
Related
I've been trying to figure a query out.
Let's say a table looks like this:
cus_id prod_category agreement_id type_id
111 10 123456 1
111 10 123456 1
111 10 123456 2
111 20 123456 2
123 20 987654 6
999 0 135790 99
999 0 246810 99
and so on...
I would like to get the count of prod_category for distinct values over agreement_id and type_id
so I would like to get a result like this:
cus_id prod_id count
111 10 2
111 20 1
123 20 1
999 0 2
We can use the following two level aggregation query:
SELECT cus_id, prod_category, COUNT(*) AS count
FROM
(
SELECT DISTINCT cus_id, prod_category, agreement_id, type_id
FROM yourTable
) t
GROUP BY cus_id, prod_category;
The inner distinct query de-duplicated tuples, and the outer aggregation query counts the number of distinct tuples per customer and product category.
You want to count distinct (agreement_id, type_id) tuples per (cus_id, prod_category) tuple.
"Per (cus_id, prod_category) tuple" translates to GROUP BY cus_id, prod_category in SQL.
And we count distinct (agreement_id, type_id) tuples with COUNT(DISTINCT agreement_id, type_id).
SELECT cus_id, prod_category, COUNT(DISTINCT agreement_id, type_id) AS distinct_count
FROM mytable
GROUP BY cus_id, prod_category
ORDER BY cus_id, prod_category;
I have a table invoices with a field invoice_number. This is what happens when i execute select invoice_number from invoice
invoice_number
1
2
3
5
6
10
11
I want a SQL that gives me the following result:
gap_start
gap_end
1
3
5
6
10
11
demo:db<>fiddle
You can use row_number() window function to create a row count and use the difference to your actual values as group criterion:
SELECT
MIN(invoice) AS start,
MAX(invoice) AS end
FROM (
SELECT
*,
invoice - row_number() OVER (ORDER BY invoice) as group_id
FROM t
) s
GROUP BY group_id
ORDER BY start
I have a table as
ID TOTAL SUM(TOTAL)
1 62 62
1 53 53
2 62 62
2 47 47
I thought the SUM(TOTAL) should look like
ID TOTAL SUM(TOTAL)
1 62 115
1 53 115
2 62 109
2 47 109
This is the query I used
select ID, TOTAL, SUM(TOTAL)
from tablename
GROUP BY TOTAL, ID
You could do a windowed SUM instead:
Select ID, TOTAL, SUM(TOTAL) OVER(PARTITION BY ID)
From tablename
This will give you the results you expected. It will display the ID and TOTAL for each row, along with the SUM of the ID grouping.
A GROUP BY is not necessary for this type of summation.
You'll need to remove the TOTAL column from the group by, and therefore the select list. Because TOTAL is in your select list and group by it can't properly aggregate how you're expecting.
SELECT ID, SUM(TOTAL)
FROM tablename
GROUP BY ID
To get your exact output:
SELECT tablename.ID,
TOTAL,
TOTALSUM
FROM (SELECT ID,
SUM(TOTAL) AS TOTALSUM
FROM tablename
GROUP BY ID) AS t
INNER JOIN
tablename
ON tablename.ID = t.ID;
I have a table:
PropertyID Amount
--------------------------
1 40
1 20
1 10
2 10
2 90
I would like to achieve :
PropertyId Amount Total_Amount
---------------------------------------
1 40 70
1 20 70
1 10 70
2 10 100
2 90 100
using below query :
SELECT
PropertyID,
SUM(Amount),
SUM(TotalAmount)
FROM
yourTable
WHERE
EndDate IS NULL
GROUP BY
PropertyID
Output:
PropertyId Amount TotalAmount
-------------------------------------
1 70 70
2 100 100
Let me know how can I get my desired output ...
You can do this using window functions:
select PropertyID, Amount,
sum(Amount) over (partition by PropertyId) as TotalAmount
from yourtable;
The window function for sum() does the following. It calculates the sum of amount for groups of rows in the same group. The group is defined by the partition by clause, so rows with the same value of PropertyId are in the same group.
SELECT PropertyID,
Amount,
(select sum(yt.Amount)
from yourTable yt where yt.PropertyID==y.PropertyID and yt.EndDate IS NULL)
as TotalAmount
FROM yourTable y
WHERE y.EndDate IS NULL
I am having table as below..
r_Id Marks
1 25
1 25
1 25
2 30
2 30
now i want a query in which sum of marks for each r_id should be calculated and result should be
r_Id Sum
1 75
2 60
Without using cursor or looping in sql server 2008. Please help me out to do it.
This should do it
SELECT r_Id, SUM(Marks) AS [Sum]
FROM SomeTable
GROUP BY r_Id;
GO
I hope this helps.
Please try:
SELECT
r_Id,
SUM(Marks) AS [Sum]
FROM
YourTable
GROUP BY r_Id
OR
SELECT DISTINCT
r_Id,
SUM(Marks) OVER(PARTITION BY r_Id) AS [Sum]
FROM
YourTable