I want to retrieve all information as well as sum of amount by the name so i use this query for that purpose.
select SUM(Amount)as total,RecieptNo,Name,UniqueId,Date,Amount,LateFee,Other from Amount where Name='Shaikh'
but this error is occured.
Column 'Amount.RecieptNo' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
so please help me.
Select RecieptNo, Name
, UniqueId, Date
, Amount
, LateFee, Other
, ( Select Sum( A1.Amount )
From Amount As A1
Where A1.Name = A.Name ) As Total
From Amount As A
Where Name='Shaikh'
Another choice available in SQL Server 2005+:
Select RecieptNo, Name
, UniqueId, Date
, Amount
, LateFee, Other
, Sum( Amount ) Over( Partition By Name ) As Total
From Amount As A
Where Name='Shaikh'
select SUM(Amount)as total,RecieptNo,Name,UniqueId,Date,Amount,LateFee,Other from Amount
group by RecieptNo,Name,UniqueId,Date,Amount,LateFee,Other having Name='Shaikh'
When using aggregate SQL functions such as SUM(), you have to use a GROUP BY clause. The example below demonstrates that. This may not be the most ideal approach to tackling your query however. What I've done is added a group by clause that includes all columns that you've selected other than your SUM. For example:
select RecieptNo, Total, UniqueId, Date, Amount, LateFee, Other SUM(Amount)as total from
Amount where Name='Shaikh' group by RecieptNo, Total, UniqueId, Date, Amount, LateFee, Other
You need to group by
select SUM(Amount)as total,Name from Amount where Name='Shaikh' group by Name
If you want to add other thing in select list you require to add this field in you group by clause also otherwise it will give error
Related
I want to use COUNT(*) on a group and then one more row at the end to give the full count.
I have used UNION to run the COUNT(*) without GROUP BY in a separate sub-query to give the total count, but that approach seems inefficient. Any ways to combine both?
SELECT PCUST, COUNT(*) FROM PAYMENT
WHERE PAYMENT_DATE = '2020-12-30'
GROUP BY PCUST
UNION
SELECT 'TOTAL', COUNT(*) FROM PAYMENT
WHERE PAYMENT_DATE = '2020-12-30'
As you can see from above query, I am repeating the same condition to get the partial and total counts.
Plz suggest any way to optimize this.
DB2 supports GROUPING SETS. Assuming no PCUST values are NULL and it is a string, the simplest method is:
SELECT COALESCE(PCUST, 'TOTAL'), COUNT(*)
FROM PAYMENT
WHERE PAYMENT_DATE = '2020-12-30'
GROUP BY GROUPING_SETS ( (PCUST), () )
I have a situation where I need to get DISTINCT values from column "note" and then get the SUM of "price" for above records.
I tried with different queries but none of them are working fine.
SELECT DISTINCT note ,price( select SUM (price) FROM [tableName] where Archived ='0'
SELECT sum(price),(SELECT DISTINCT note , price from [tableName] where Archived ='0')
In a nutshell I need to get the sum of prices for the distinct records.
Are you looking for group by?
SELECT note, SUM(price)
FROM [tableName]
WHERE Archived = '0'
GROUP BY note;
If you want the sum over ALL the records, just use window functions like this:
SELECT note, SUM(price) as note_sum, SUM(SUM(price)) OVER () as total_sum
FROM [tableName]
WHERE Archived = '0'
GROUP BY note;
Maxbe this will be one way to do it:
select distinct sum(price) over(partition by note), note
from tablename
where Archived = 0
Here is a demo on SQLServer
If I have understood you correctly you need distinct note values and only one sum for all of them ... then something like this:
select distinct note, (select sum(price) from tablename) sum_tot
from tablename
where Archived = 0
P.S. do add expected result....
I need percentage for each status
Select NAME, STATUS, Count(*) From DATA
group by NAME, STATUS
order by NAME
You need to create subquery for count without status, then you need to get percentage user wise, you can try this below:
Select f1.NAME, f1.STATUS, Count(*),CONCAT((Count(*)*100)/(select Count(*) from data f2 where NAME=f1.Name group by f2.NAME),'%') From DATA f1
group by f1.NAME, f1.STATUS
order by f1.NAME
Or if you have user id then it's better query and perfect result
Select f1.NAME, f1.STATUS, Count(*),CONCAT((Count(*)*100)/(select Count(*) from data f2 where UserId=f1.UserId group by f2.UserId),'%') From DATA f1
group by f1.UserId,f1.NAME, f1.STATUS
order by f1.NAME
I think you have used below formula in your percentage calculate,
count(row first count)*100/sum of all count rows, i.e.448*100/1560=28.71.
So, dear, I request you please mention all the problems with your question.
Try the below query and enjoy it.
Select NAME, STATUS, Count(*),
Count(*)*100/(select sum(CT) from (select NAME, STATUS, Count(*) CT from data group by NAME, STATUS))PERCENTAGE
From DATA
group by NAME, STATUS
order by NAME
Here's how to get the percentage. First is to get the total count(), then multiple by 1.0, to cast the result in decimal format.
select name, status, count(1)
, ((count(1) * 1.0)
/(select count(1) from data)) * 100 as percentage
from data
group by name, status
order by name
Check this, using SUM to calculate the total and then use group by to group the names
SELECT name, status, COUNT(status)/(
SELECT COUNT(status)
FROM data
)*100
FROM percentage
GROUP BY name, status
ORDER BY name
In the data tabel when the PRICE_TYPE = MSRP, the amount should be added to column msrp and when PRICE_TYPE = SELP the amount should be added selp column,
How can i write a query to get the above task done , thanks in advance.
the first image should be the desired output.
One of the ways to get your expected result:
select MATERIAL_NUMBER, [MSRP],[SELP]
FROM
(
SELECT
MATERIAL_NUMBER,
PriceType,
Sum(Amount)
FROM Org_table
GROUP BY
MATERIAL_NUMBER,
PriceType
) tbl
pivot
(
sum(Amount)
for PriceType in ([MSRP],[SELP])
)
hope it helps :)
You seem like want to get pivot, you can try to use condition aggregate function.
CASE WHEN with aggregate function.
SELECT MATERIAL_NUMBER,
SUM(CASE WHEN PRICE_TYPE = 'MSRP' THEN AMOUNT END ) MSRP,
SUM(CASE WHEN PRICE_TYPE = 'SELP' THEN AMOUNT END ) SELP
FROM T
GROUP BY
MATERIAL_NUMBER
sqlfiddle
NOTE
because your Amount seem like a float value, some I use SUM
SELECT price,total_price,SUM(mount) As TotalAmount
FROM sales
group by [date]
is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
You cannot select fields in the SELECT part which is not a part of an Aggregated function or specified in the the GROUP BY clause when using GROUP BY.
You can select "mount" because it's aggregated (SUM), but not price or total_price because they're not aggregated nor part of your group by date
You will need to either change your group by functionality, or the select part.
You have to group by the remaining tables that are not a function, therefore:
SELECT price,total_price,SUM(mount) As TotalAmount
FROM sales
Group by price,total_price
Order by date