I have the following query:
SELECT cashtransactionstatus_id, SUM(cashtransaction_amount) AS cashtransaction_amount
FROM cash_transactions
WHERE cashpaymenttype_id = 1
GROUP BY (cashtransactionstatus_id);
This query returns:
How i can return only one digit number endtotal subtracting and sum depending by the column cashtransactionstatus_id? Ex:
SUM Status(1,3): 1195.00 as col1
SUM Status(2,4,5): 430.00 as col2
then show (col1 - col2) AS endtotal = 765.00
Thank you
You can use conditional aggregates:
SELECT SUM(CASE WHEN cashtransactionstatus_id IN (1,3) THEN cashtransaction_amount END) -
SUM(CASE WHEN cashtransactionstatus_id IN (2,4,5) THEN cashtransaction_amount END) AS endtotal
FROM cash_transactions
WHERE cashpaymenttype_id = 1
SQL Fiddle Demo
Related
I have a query:
select AVG(tickets_number)
from TABLE_ONE
where ofd_id = :ofdId and launch_id in
(select id from TABLE_TWO where is_actual = 1 and is_matching_completed = 1)
But I need to calculate average value only if there are at least 10 rows from this query, otherwise I have to return NULL.
How can I do this?
Try this case expression.
select CASE WHEN COUNT(tickets_number) >= 10
THEN AVG(tickets_number)
ELSE NULL END average
from TABLE_ONE ...
I have data with column name Rule1 with values ..Correct, Incorrect and Undefined
I was able to get the count of them in this particular column using Group BY
select Rule1, count(*) from table_name group by Rule1;
I have one more column Rule2 with Values Correct, Incorrect only
When I try a similar select statement as above, I am getting the only count of Correct and Incorrect
I want to get the count of Undefined as zero as no Undefined is present in Rule2 column
How to do that
How I can write a query that needs to show the count of fixed values in multiple columns
You can create a derived table that enumerates the possible values with union all, then bring your table with a left join:
select x.rule1, count(t.rule1) cnt
from (select 'Correct' rule1 union all select 'Incorrect' union all select 'Undefined') x
left join mytable t on t.rule1 = x.rule1
group by x.rule1
This is a cross-database syntax that will work in both MySQL and SQL Server.
In very recent versions of MySQL, you can use the values(row ...) syntax:
select x.rule1, count(t.rule1) cnt
from (values row('Correct'), row('Incorrect'), row('Undefined')) x(rule1)
left join mytable t on t.rule1 = x.rule1
group by x.rule1
In SQL Server, the equivalent query is:
select x.rule1, count(t.rule1) cnt
from (values ('Correct'), ('Incorrect'), ('Undefined')) x(rule1)
left join mytable t on t.rule1 = x.rule1
group by x.rule1
You could also use sum like this:
SELECT
sum(case when Rule1 = 'Correct' then 1 else 0 end) AS Rule1Correct,
sum(case when Rule1 = 'Incorrect' then 1 else 0 end) AS Rule1Incorrect,
sum(case when Rule1 = 'Undefined' then 1 else 0 end) AS Rule1Undefined,
sum(case when Rule2 = 'Correct' then 1 else 0 end) AS Rule2Correct,
sum(case when Rule2 = 'Incorrect' then 1 else 0 end) AS Rule2Incorrect
FROM
table_name
WHERE
1
It enables multiple counts in one query. I added all the counts, thought the first two was enough for the idea.
Futhermore I have no idea what is better for the serverload, this or using unions.
I have a MySQL table (below):
I can fetch the min or max of the column values simply but my problem is:
If any 3 or more fields value matched then fetch that value (if 3 fields values matched more than one value then the lowest of those values), for row #1 2100 will be the output and for row#3 55 will be the output.
If no field value matched with a minimum of 3 fields the lowest value of the entire row will be fetched, for row# 2 the output will be 1900.
I can use IF (e.g. select if(col1 = col2, col1, col2) from table) in select but I didn't find a solution for this situation. Can anyone help to write a MySQL query for this?
Thanks in advance!
You can use this query to get the results you want. It uses two nested subqueries: the first unpivots the data into a single column; the second then counts how many of each column value occur for a given ID value. The outer query then tests the maximum of the counts; if it is >= 3 then the minimum value which has a count of 3 or more is chosen, otherwise the minimum column value is chosen as the minimum column:
SELECT ID,
CASE WHEN MAX(cnt) >= 3 THEN MIN(CASE WHEN cnt >= 3 THEN col END)
ELSE MIN(col)
END AS min_col
FROM (
SELECT ID, col, COUNT(col) AS cnt
FROM (SELECT ID, col1 AS col FROM data
UNION ALL
SELECT ID, col2 FROM data
UNION ALL
SELECT ID, col3 FROM data
UNION ALL
SELECT ID, col4 FROM data
UNION ALL
SELECT ID, col5 FROM data
UNION ALL
SELECT ID, col6 FROM data
UNION ALL
SELECT ID, col7 FROM data
) d
GROUP BY ID, col
) dd
GROUP BY ID
Output (for your sample data):
ID min_col
1 2100
2 1900
3 55
Demo on SQLFiddle
Background
I want to rename my case statement in sql select statement dynamically.
Eg:
SELECT (case when id= x.id then x.sums end) x.id
as (select id,count(*) sums from table
group by id) x
what i want the output is list of columns created ,with Labels as distinct id's from "id" column.
However,this variable x.id is not dynamically outputing values,rather i get output a single column x.id.
Eg:
Columns in table...
id---c1----c2
1----x1---x2
2----x2----x3
3----x4----x5
columns expected after running query...
1-----2----3
but actual o/p column is::
x.id
Query
Any ideas,how to generate columns dynamically using select query,please correct me ,if i am wrong.
Below is for BigQuery!
Please note: your expectations about output column names are not correct!
Column name cannot start with digit - so in below example - i will be using id_1, id_2 and id_3 instead of 1, 2 and 3
SELECT
SUM(CASE WHEN id = 1 THEN 1 END) AS id_1,
SUM(CASE WHEN id = 2 THEN 1 END) AS id_2,
SUM(CASE WHEN id = 3 THEN 1 END) AS id_3
FROM YourTable
Above example assumes you know in advance your IDs and there are very few of them so it is not a big deal to write manually few numbers of lines with SUM(...) for each id
If this is not a case - you can first generate above query programmatically by running below query
SELECT 'SELECT ' +
GROUP_CONCAT_UNQUOTED(
'SUM(CASE WHEN id = ' + STRING(id) + ' THEN 1 END) AS id_' + STRING(id)
)
+ ' FROM YourTable'
FROM (
SELECT id FROM (
SELECT * FROM YourTable GROUP BY id ORDER BY id
)
as a result - you will get string like below
SELECT SUM(CASE WHEN id = 1 THEN 1 END) AS id_1,SUM(CASE WHEN id = 2 THEN 1 END) AS id_2,SUM(CASE WHEN id = 3 THEN 1 END) AS id_3 FROM YourTable
So, now just copy it and paste into Query Editor and run it
you can see similar example here - https://stackoverflow.com/a/36623258/5221944
I have a MySQL table with 20 fields.
out of 20, 15 have 3 possible value like 0,NA,1+. i need to write a query to fetch each field count which has value > 0. is it possible to get it in single query.?
Thanks Guys.
Unless I'm missing something...
SELECT
COUNT(CASE WHEN c1<>'0' THEN 1 END) AS c1_count,
COUNT(CASE WHEN c2<>'0' THEN 1 END) AS c2_count,
-- ...
COUNT(CASE WHEN c15<>'0' THEN 1 END) AS c15_count
FROM t
select "column1", count(*) from your_table where value>0
union all
select "column2", count(*) from your_table where value>0
...
... // repeat until column20
interesting documentation :- http://dev.mysql.com/doc/refman/5.0/en/union.html