I have N Rows with same SlNo but different RowNo as shown below
for eg:
SlNo RowNo Status
1 1 Opened
1 2 Closed
1 3 Opened
1 4 Closed
1 5 Opened
If all the Status Column Rows are Closed , I want to return 1.
else o.
Thanks in advance.
You can do it following:
SELECT STATUS FROM `table_1` where SLNO = 1 group by status
If you get only one record with value "Closed" then execute the next query
UPDATE `table_2` SET Ref_Status = 'Closed' WHERE SLNO = 1;
Update my_table set Status='Closed' where SlNo = 1
Find the count of all rows and check it with the count of Closed values.
Query
select t.`SlNo`,
case when t.`RowNo_Count` = t.`closed_count` then 1 else 0 end as `new_col`
from(
select `SlNo`,
count(`RowNo`) as `RowNo_Count`,
sum(case `Status` when 'Closed' then 1 else 0 end) as `closed_count`
from `your_table_name`
group by `SlNo`
)t;
Related
I have a table like this. Now i want two sum(hour).. 1 for place_type '1' and another for place_type '2'. Is it possible in mysql?
Now my query is
SELECT sum(hour) as totalHour from category_data group by category_data.user_id
also tried with IF condition but not working
SELECT IF(place_type='1',sum(hour),0) home,
IF(place_type='2', sum(hour), 0) center,
from category_data group by category_data.user_id
user_id place_type hour
1 1 2
1 2 5
2 1 3
2 2 4
3 1 2
Thanks in advance
Conditional aggregation is what you need here.
select sum(case when place_type = 1 then `hour` end),
sum(case when place_type = 2 then `hour` end)
from category_data
Example fiddle
I have table for student attendance like below:
id | Present | date
1 | 1
1 | 0
1 | 1
1 | 1
1 | 0
Now I want to get the total days he is present via single query. Is it possible?
Below is the way I used to get the student present/absent count:
select count(*) from table where present =1
select count(*) from table where present =0
But I think can I get both from one query instead of two.
select sum(present = 1) as present1,
sum(present = 0) as present0
from table
You can use the following query:
select (select count(*) from table where present=1) as "present"
, (select count(*) from table where present=0) as "absent";
Try this its working fine :
SELECT SUM( present =1 ) AS present, SUM( present =0 ) AS absent
FROM details
WHERE id =1
Screenshot :
Try this :
For all student with group by
select id,present,absent from
(
select id,sum(case when present = 1 then 1 else 0 end) as present,
sum(case when present = 0 then 1 else 0 end) as absent
from table_name
group by id
) as absen
if you want to show only one student, just add where clause in the end like this :
select id,present,absent from
(
select id,sum(case when present = 1 then 1 else 0 end) as present,
sum(case when present = 0 then 1 else 0 end) as absent
from table_name
group by id
) as absen
where id = '1'
Hope this help you..
I have a table "article_likes" of users and whether they like or dislike the article. With every like/dislike the datetime is saved and the id of the article. The like/dislike is saved as a bool in the "status" column.
id article_id user_id like_date status
1 8 2 2014-11-03 21:30:33 1
2 8 2 2014-11-03 21:31:00 0
3 8 3 2014-11-03 22:30:59 1
4 9 6 2014-11-03 22:36:25 1
5 9 2 2014-11-03 23:19:46 1
I like to get the amount of likes an article has. So only the status of the most recent date is valid for the count.
So the desired result would be:
article_id amount_likes
8 1
9 2
I want to get the result per article id, so not all at once.
Can anybody help me? Thanks!
This will solve your problem :)
SELECT article_id, SUM(IF(status = 1, 1, -1)) AS amount_likes FROM article_likes
GROUP BY article_id
If you store dislikes as -1 then:
SELECT article_id, SUM(status) AS amount_likes FROM article_likes
GROUP BY article_id
Try this:
SELECT `article_id`,COUNT(`user_id`) AS amount_likes FROM `links`
WHERE `status`='1' GROUP BY DATE(`like_date`), `user_id`;
Fiddle here - http://sqlfiddle.com/#!2/3b7bb6/4
Hope it helps
SELECT article_id, COUNT(IF(
SELECT CASE
WHEN status=1 THEN 'true'
ELSE 'false'
END),
'true','false'
)
)
FROM article_likes
GROUP BY article_id
ORDER BY article_id
I have a table containing the following data columns:
ord_number check
OR123 0
OR125 1
OR123 2
OR123 0
OR124 0
OR124 0
OR125 0
OR123 0
The check column indicates if we have fully supplied the order line with a value of zero or below meaning 'yes we have supplied complete' and a value of 1 or above meaning 'no we have not supplied complete' .
To calculate an order fill rate (how many orders have been filled complete) I need to know how many of the orders have only zero check results for each entry and how many have at least one positive check result.
My logic is that I need to GROUP the items and then carry out a COUNTIF within the group (this may be the wrong SQL methodology but logically it makes sense to me in terms of the outcome I'm looking for). From this query I need to return those two values into my PHP script.
The results from this data set would be:
Complete = 1 (orders with only zero checks)
Incomplete = 2 (orders where one or more lines have positive checks)
I've spent the morning trying to find a solution but can't find anything that I either understand enough or is similar enough that I can break it down and understand it.
If someone can help me by pointing me in the right direction or can provide a sample with an explanation I would be very grateful.
For 1 row with 2 counter fields use:
SELECT
SUM(CASE WHEN `check` = 1 THEN 1 ELSE 0 END) AS incomplete,
SUM(CASE WHEN `check` = 0 THEN 1 ELSE 0 END) AS complete
FROM orders
For 2 rows with 1 name use:
SELECT COUNT(`ord_number`) AS fulfillment
FROM orders WHERE (`check`=0)
UNION SELECT COUNT(ord_number)
FROM orders WHERE (`check`=1)
in which case you need to put row 1 into $complete (check=0)
and row 2 into $incomplete (check=1)
you may add:
UNION SELECT COUNT(ord_number) FROM orders
which will give you the total records on row 3.
output will be:
SELECT * FROM orders
ord_number check
---------------------
OR123 0
OR124 1
OR125 0
OR126 0
OR127 0
OR123 0
OR125 1
SELECT COUNT(`ord_number`) AS counts FROM orders or1 WHERE (`check`=0)
UNION SELECT COUNT(ord_number) FROM orders or2 WHERE (`check`=1)
UNION SELECT COUNT(ord_number) FROM orders
counts
-------
5
2
7
SELECT
SUM(CASE WHEN `check` = 1 THEN 1 ELSE 0 END) AS incomplete,
SUM(CASE WHEN `check` = 0 THEN 1 ELSE 0 END) AS complete FROM orders
incomplete complete
-------------------------
2 5
As for PHP:
for 1 row:
mysql_select_db($database_test, $test);
$query_countIn1Row = " SELECT SUM(CASE WHEN `check` = 1 THEN 1 ELSE 0 END) AS
incomplete,
SUM(CASE WHEN `check` = 0 THEN 1 ELSE 0 END) AS complete
FROM orders";
$countIn1Row = mysql_query($query_countIn1Row, $test) or die(mysql_error());
do {
$varIncomplete = $row_countIn1Row['incomplete'];
$varComplete = $row_countIn1Row['complete'];
} while ($row_countIn1Row = mysql_fetch_assoc($countIn1Row));
and for 1 column with counts:
mysql_select_db($database_test, $test);
$query_countOrders = "SELECT COUNT(`ord_number`) AS counts
FROM orders or1 WHERE (`check`=0)
UNION SELECT COUNT(ord_number) FROM orders or2 WHERE (`check`=1)
UNION SELECT COUNT(ord_number) FROM orders";
$countOrders = mysql_query($query_countOrders, $test) or die(mysql_error());
$myCounts = array();
do {
$myCounts[]= $row_countOrders['counts'];
} while ($row_countOrders = mysql_fetch_assoc($countOrders));
$complete = $myCounts[0];
$incomplete = $myCounts[1];
$total = $myCounts[2];
SELECT ord_number,IF(MAX( check) <=0 ,'COMPLETED','PENDING')
from TABLENAME
group by (ord_number )
Through this oneline query you will easily get which order is in pending status and which one is complete status
AS per your given details
The check column indicates if we have fully supplied the order line
with a value of zero or below meaning 'yes we have supplied complete'
and a value of 1 or above meaning 'no we have not supplied complete' .
it means check column can contain negative value as well as 0 for indicating the complete status and 1 or any postive integer number to indicate the pending status.
so the logic should be find the max(check) so if the max for a order is found 0 or any negative number then we will sure that order is complete and if it found a positive number it means there was a pending entry.
You can do this in two steps:
SELECT ord_number,
SUM(CASE WHEN `check` = 0 THEN 1 ELSE 0 END) as Comp,
COUNT(`check`) as total
FROM tab1
GROUP BY ord_number
This would give you the number of rows with 0 (complete) and the total number of rows for each ord_number.
Then you do a COUNT to validate when those two are equal:
SELECT CASE WHEN comp = total THEN 'COMPLETE' ELSE 'INCOMPLETE' END AS status,
COUNT(*) AS statusCount
FROM(
SELECT ord_number,
SUM(CASE WHEN `check` = 0 THEN 1 ELSE 0 END) as Comp,
COUNT(`check`) as total
FROM tab1
GROUP BY ord_number) a
GROUP BY 1;
This should get you what you want.
sqlfiddle demo
Note that i used ` around check since this is a mysql reserved word
I have a table like this
a_count b_count total_count(a_count+b_count)
2 3
5 1
4 7
5 0
This is my table I need to update total count field using a single query. How can I write that kind of a query?
I need output like this
a_count b_count total_count(a_count+b_count)
2 3 5
5 1 6
4 7 11
5 0 5
To update the values of those fields in the table:
UPDATE mytable SET total_count = a_count + b_count
To get those fields from the table:
SELECT a_count, b_count, total_count FROM mytable
To get those fields without that total_count column:
SELECT a_count, b_count, (a_count+b_count) AS total_count FROM mytable
You can also write a trigger for that
DELIMITER //
CREATE TRIGGER `total_count` BEFORE INSERT OR UPDATE on `table`
FOR EACH ROW BEGIN
SET NEW.total = NEW.a+NEW.b;
END;
//
DELIMITER ;