Where is the mistake in my query
SELECT #Total:=SUM(deposit-cost) as Total FROM `vendor_ledger` Where NOT #Total < 0 GROUP BY
VDR_ID;
Anyone Please Help Me
If you want to ignore any zero or negative deposit-cost amounts in your sum() then use a WHERE condition:
SELECT SUM(deposit-cost) as Total
FROM `vendor_ledger`
WHERE deposit-cost > 0
GROUP BY VDR_ID;
If, instead, you are wanting to ignore any Total where it's less than or equal to 0 then use a HAVING condition:
SELECT SUM(deposit-cost) as Total
FROM `vendor_ledger`
GROUP BY VDR_ID
HAVING Total > 0;
Related
So, I currently have a SQL query that brings up a total amount for all orders for a specific customers. This equation is the following.
SUM((OrdIt.ItemPrice- OrdIt.DiscountAmount) * OrdIt.Quantity) As TotalAmount
However, I only want to display results where the initial ItemPrice is OVER 500.
Where would I put OrdIt.ItemPrice > 500 ?
You could sum a CASE expression which checks the price:
SUM((CASE WHEN OrdIt.ItemPrice > 500
THEN OrdIt.ItemPrice - OrdIt.DiscountAmount
ELSE 0 END) * OrdIt.Quantity)
Can you not issue this SQL query?
SELECT SUM((OrdIt.ItemPrice - OrdIt.DiscountAmount) * OrdIt.Quantity) As
TotalAmount
FROM OrdIt
WHERE OrdIt.ItemPrice > 500;
MySQL version 8.0
Hi say I have a table that looks like this:
Which I got after groupby count operation:
select number
, count(number) as `count`
FROM (select *
, CASE
WHEN column = 0 Then 0 Else 1
END AS number
FROM table) t1;
result in:
number count
0 100
1 900
Now for each number I want to add a column that gives corresponding percentage.
Desired:
number count percentage
0 100 10
1 900 90
Thanks in advance!
If you are running MySQL 8.0, you can use window functions:
select
t.*,
count / sum(count) over() ratio
from mytable t
In earlier versions, an option uses a subquery:
select
t.*,
count / (select sum(count) from mytable) ratio
from mytable t
This gives you a ratio between 0 and 1; you can multiply it by 100 if you want a percentage.
Note that, if you are getting your original resultset from a query, it is very likely that this can be furthermore optimized. You might want to ask a new question, disclosing your original table(s) and query.
In terms of your original query, it would be something like:
select number, count(*), count(*) / sum(count(*)) over () as ratio
from t
group by number;
If you want the percentage rather than ratio than multiply by 100.
I have a database table full of transactions. The transactions contain negative numbers from people returning items. I would like to add up all the amount field while also subtracting the negative values of returns from the total. How can I do this and output it out?
Currently the best I can do is get:
SELECT SUM(amount)
FROM outputaddition
GROUP by SIGN(amount);
But this only puts positive and negative in the same column.
SELECT personId,SUM(CASE WHEN amount<0 THEN amount ELSE 0 END) as NegativeTotal,
SUM(CASE WHEN amount>=0 THEN amount ELSE 0 END) as PostiveTotal
FROM outputaddition
GROUP BY personID
If you want single column
SELECT personId,SUM(amount) as Total
FROM outputaddition
GROUP BY personID
try this,
SELECT SUM(amount) as ActualTotal
,Sum(Case When amount > 0 then amount else 0 end) totalOfPositiveOnly
FROM outputaddition
I'm afraid your answer is ambiguous; we're not sure exactly what you're asking.
Most simply, if "subtracting the negative values" means you just want to ignore the negative returns:
select sum(amount)
where amount > 0;
I have following two queries:
select count * form a where temp>str group by x,y
select count * form a where temp<=str group by x,y
This result in parsing of table twice. Since the table size is 100 of gigs I was thinking if we can reduce this to single query.
select sum(IF(temp>'str',1,0)) as GREATER,
sum(IF(temp<'str',1,0)) as LESSER
from a
group by x,y
This will return a single row with two fields, GREATER and LESSER, which contain the counts of the number of rows which are Greater and Lesser than the string respectively.
IF(temp>'str',1,0)) returns a 1 if greater, else 0. Summing these values will give the total number of rwos which are greater.
IF(temp<'str',1,0)) returns a 1 if lesser, else 0. Summing these values will give the total number of rwos which are lesser.
Try this:
SELECT
SUM(CASE WHEN `temp` > `str` THEN 1 ELSE 0 END) AS `greater`,
SUM(CASE WHEN `temp` < `str` THEN 1 ELSE 0 END) AS `less`
FROM
`a`
GROUP BY
`x`,
`y`
please help me with this issue:
I have the following query:
select SUM(price) from prices_adverts
where advert_id="15"
and room_type_id="3"
and (date >= "2013-09-20" AND date <"2013-09-23")
order by price
This searches to find and SUM price for the room for dates between "2013-09-20" AND "2013-09-23"
but for example if dates 2013-09-21 and 2013-09-22 have value 0 and 2013-09-20 has value 25.00 the query will return sum of 25.00 for the whole date range. When the specific date has value 0 this means the room is not available, but when even 1 day has value greater than 0 the query accept the room as available because it has total value greater than 0.. Please advise me how to change the query so if even 1 day within daterange to has value 0 the query to return total value 0 for the whole period.. I hope you to understand the nature of this issue, thanks
Try this:
IF(SUM(price = 0), 0, SUM(price))
And I assume you'd like to GROUP BY room_id.
Also I'd suggest changing those 0's in your database to NULLs. Because at some point you can possibly have 0 price, which is not equivalent to not available. You won't be able to distinguish them which is no good.
I guess you've missed GROUP BY Room_id. So it should be something like:
select room_id,
CASE WHEN MIN(price) = 0 THEN 0 ELSE SUM(price) END as priceSum
from prices_adverts
where advert_id="15"
and room_type_id="3"
and (date >= "2013-09-20" AND date <"2013-09-23")
GROUP BY room_id
ORDER BY priceSum