I have a two table.
I want to use the chart_num value for two tables.
Table hospital_payment_data
id chart_num treatment_fees_difference treatment_fees_check_division
1 9 200000 test
2 9 100000 test
Table advenced_payment
id chart_num advenced_amount
1 9 100000
I want result
COUNT if_treatment_fees_check_division sum_init_amount
2 200000 100000
I want treatment_fees_check_division = 'test' count
and
I want treatment_fees_check_division = 'test' condition sum treatment_fees_difference + advenced_amount
How can I achieve I want result? Would you please provide the solution?
I think you are looking for a query as below-
DEMO HERE
Note: As per your condition and data, SUM of if_treatment_fees_check_division should be 300000
SELECT A.chart_num,
COUNT(*) COUN,
SUM(CASE
WHEN treatment_fees_check_division = 'test' THEN treatment_fees_difference
ELSE 0
END) if_treatment_fees_check_division,
(
SELECT SUM(advenced_amount)
FROM advenced_payment
WHERE chart_num = A.chart_num
) sum_init_amount
FROM hospital_payment_data A
GROUP BY A.chart_num
Related
I'm trying to see if it's possible to have several unique columns in a select statement from varying where clause selections. Here is my query.
select org_id, count(org_role) as total
from organization
where org_id = 10 and org_role = 9
group by org_id;
the above works perfectly. It produces:
org_id total
10 19
Id'd like to add another column count named total2 where org_id = 10 and org_role = 7 (the count is 23). So i'd have this result:
org_id total total2
10 19 23
I'm just not sure how to edit the original above query to produce that. Any thoughts would be appreciated.
You can use conditional aggregation. For example:
select
org_id,
sum(case when org_role = 9 then 1 else 0 end) as total,
sum(case when org_role = 7 then 1 else 0 end) as total2
from organization
where org_id = 10 and org_role in (7, 9)
group by org_id;
select org_id, count(org_role) as total, count(total_2)
from organization
where org_id = 10 and org_role = 9
group by org_id;
If it does not work please send your database structure.
I previously posted a question here: Mysql query with joined tables problems
but didn't get good answers, so I thought I break it down to show only the part that gives me a headache, I might get answers faster to this question, and if I can solve this, I can solve the previous problem too.
The values are referring to an item_id, and I want to get item_id's where the item is referring to (('good' OR 'bad') AND 'fast')
So item_id 1 must be listed, because it is good and also fast
item_id 2 should not listed because it is not fast.
The result i want to get
item_id
1
5
if I have a table like this
id item_id value
1 1 'good'
2 1 'fast'
3 2 'good'
4 2 'slow'
5 3 'good'
6 3 'slow'
7 4 'bad'
8 4 'slow'
9 5 'bad'
10 5 'fast'
11 6 'moderate'
12 6 'fast'
Can someone help me?
One way to see this is: you want a statement per item, so you aggregate and group by item. Criteria come in the HAVING clause.
select item_id
from mytable
group by item_id
having count(case when value = 'fast' then 1 end) >= 1
and count(case when value in ('good','bad') then 1 end) >= 1;
(You can also use sum(case when value = 'fast' then 1 else 0 end) >= 1 or max(case when value = 'fast' then 1 else 0 end) = 1 or some expression along those lines. COUNT(expression) counts expressions that are not null. As I am omitting an ELSE branch, a non-matching record results in null and is thus not counted. Some prefer SUM, some prefer COUNT - it's finally a matter of personal preference.)
One way to do this is with an EXISTS query.
Here's an example:
select t1.item_id
from your_table t1
where t1.value = 'fast'
and exists (
select NULL
from your_table t2
where t2.item_id = t1.item_id
and t2.value in ('good','bad')
);
Considering that a item can be good or bad, not both, I would use the simple way:
select item_id your_table
where value in ('good','bad','fast')
group by item_id
having count(*) = 2
count should be always 2 in those cases.
I have a table with the following entries:
id customer amount kind
1 123 15 g
2 123 30 op
3 234 20 g
4 345 25 g
5 456 12 g
6 456 15 op
What I want to do is to sum all amounts with the kind "g".
Now I want to add a condition:
"Only sum the amount to the sum if there is another entry of the customer with the kind 'op'"
Means my result should be 27 in this case and not 72.
What's a good way to add this condition?
Thanks!
To get the sum for each customer do
select customer, sum(case when kind = 'g' then amount else 0 end) as c_sum
from your_table
group by customer
having sum(kind = 'op') > 0
to get the total sum do
select sum(c_sum)
from
(
select customer, sum(case when kind = 'g' then amount else 0 end) as c_sum
from your_table
group by customer
having sum(kind = 'op') > 0
) tmp
I have a table that looks something like this;
(actual table is larger with several million rows)
Test_table
ID Day Value
=============
1 1 4
2 1 -1
3 1 27
4 1 3
5 1 -2
6 1 -5
7 1 3
8 1 1
9 1 1
10 1 Null
11 2 1
12 2 1
13 2 2
14 2 -1
15 2 -3
I want to produce a table of these two columns with the count of the number of times each entry appears, a 2d table with the day down the rows, and the values across the top with each cell containing the count of entries in that criteria like the below;
Desired output
Day Null -5 -3 -2 -1 1 2 3 4 27
==================================================================================
1 1 1 1 1 2 2 1 1
2 1 1 2 1
A query like;
select day, value, count(*) as count
from test_Table
group by day, value
Order by day asc, value desc
;
produces the data as many rows and only 3 columns... How can I get the desired output?
You can do this with conditional aggregation:
select day,
sum(value is NULL) as "NULL",
sum(value = -5) as "-5",
sum(value = -3) as "-3",
sum(value = -2) as "-2",
sum(value = -1) as "-1",
sum(value = 1) as "1",
sum(value = 2) as "2",
sum(value = 3) as "3",
sum(value = 4) as "4",
sum(value = 27) as "27"
from test_Table
group by day
Order by day asc;
Note two things. First, the column values are fixed. If you want dynamic column names, then you need to use dynamic SQL. Second, instead of blanks this will have 0 for the days with no count of a particular value.
The short answer is that it can't be done in MySQL.
The reason is that a SELECT statement has to specify the number of columns to be returned, a name and datatype for each column. And MySQL cannot dynamically generate columns to be returned for you.
The longer answer is that you would need a query of the form:
SELECT t.Day
, SUM(IF(t.value IS NULL,1,0)) AS `Null`
, SUM(IF(t.value = -5 ,1,0) AS `-5`
, SUM(IF(t.value = -3 ,1,0) AS `-3`
, ...
FROM mytable t
GROUP BY t.Day
with each column specified in the SELECT list.
One trick you can use is to use another, separate query to help write that query you need. This has to be a separate step, a separate query. To get the list of values you want returned as column headers would be of the form:
SELECT IFNULL(v.value,'Null') AS val
FROM mytable v
GROUP BY v.value
ORDER BY IF(v.value IS NULL,0,1), v.value
If you are doing this just in MySQL (and not an application), you can have MySQL help generate the required SQL text for you (using SQL to generate SQL)
SELECT CONCAT(' , SUM(IF(t.value',
IFNULL(v.value,' IS NULL',CONCAT(' = ',v.value)),
',1,0)) AS `',v.value,'`'
) AS expr
FROM mytable v
GROUP BY v.value
ORDER BY IF(v.value IS NULL,0,1), v.value
Then copy the string values returned from the expr column, paste those into an editor, and finish creating the SQL statement, like the example shown above.
The answer from Gordon shows the expression IF(col=12,1,0) can be abbreviated to col=12.
I always find myself typing that out the IF(conditional,valtrue,valfalse), but that's just the way my brain works. It's just easier for me to read.
Similarly the expression in the ORDER BY in my example...
ORDER BY IF(v.value IS NULL,0,1)
could be rewritten...
ORDER BY v.value IS NOT NULL
My Data is
ID SCORE
1 55
1 -1
1 25
1 -1
1 -1
1 35
2 25
2 -1
2 65
2 55
2 21
2 -1
Now i want to add/sum the score of each id ignoring -1 and i am trying with this code which is not working
SELECT SUM(CASE when SCORE>-1 THEN SUM(SCORE) ELSE 0 END)
FROM jbit WHERE htno='$id'
Here i am already using WHERE so how can i use another WHERE, if i use multiple Where in single query it may effect other processes.. please help me out
Help me out friends
if there is only two column then there is no need to use SUM, you can try below
SELECT id, IF(SCORE=-1,0,SCORE) AS scoreSum
FROM table1
GROUP BY id
Working DEMO
alternative ( not tested )
SELECT id, SUM(IF(SCORE=-1,0,SCORE)) AS scoreSum
FROM table1
WHERE htno =$id
GROUP BY id
SELECT SUM(score) AS score_sum
FROM jbit
WHERE htno='$id'
AND score <> -1 ;
SELECT SUM(`SCORE`)
FROM `jbit`
WHERE `htno` = '$id'
`SCORE` > 0;
Though, I'd suggest you to change '$id' to just $id if the column type of htno is INTEGER.
SELECT SUM(score) FROM <tablename> WHERE score != -1