How to get count of distinct rows in MySQL? - mysql

I have used the following query:
select tblclass.classname,tblattendance.id
from tblclass,tblattendance
where tblclass.classcode=tblattendance.classcode
and tblattendance.attdate='2013-07-01'
Output of this query is as follows:
Now what I want is rather than the above result I want count of different classes like IB-2,IC-5.
Please tell me what modifications do I need to made in my query to get the desired result

Use the Group By SQL clause and add the aggregate function Count
select tblclass.classname, Count(tblattendance.id) as counter
from tblclass,tblattendance
where tblclass.classcode=tblattendance.classcode and tblattendance.attdate='2013-07-01'
group by tblclass.classname

Try this
select count(tblattendance.id),tblclass.classname from tblclass,tblattendance
where tblclass.classcode=tblattendance.classcode and tblattendance.attdate='2013-07-01'
group by tblclass.classname

Use COUNT() function for that with GROUP BY. Also use JOIN.
SELECT tc.classname, COUNT(tc.classname) AS COUNTS
FROM tblclass tc
JOIN tblattendance tt
ON tc.classcode = tt.classcode
WHERE tt.attdate='2013-07-01'
GROUP BY tc.classname

Related

MySql, use group by with a WHERE clause

I want to add a WHERE clause to my statement, but the problem is,
when I add the WHERE clause, I get an error "Invalid use of group function".
I also tried to replace the WHERE clause and write the condition into the
JOIN .. ON part, but the error is still there.
I want to add the condition so that only the rows " SUM(res.ReservationID) = 2" are returned.
-- works but we only want to get the rows in which the SUM = 2
SELECT ctr.ID, ctr.LastName, ctr.FirstName, SUM(res.ReservationID) as ReservierteSitze
FROM customer as ctr
INNER JOIN reservation AS res ON ctr.ID = res.CustomerID
Group by ctr.ID;
SELECT ctr.ID, ctr.LastName, ctr.FirstName, SUM(res.ReservationID) as ReservierteSitze
FROM customer as ctr
INNER JOIN reservation AS res ON ctr.ID = res.CustomerID
Group by ctr.ID
HAVING ReservierteSitze = 2;
The HAVING clause is like a where clause for the GROUP BY (applies to the groupings)
You can still have a WHERE clause before the GROUP BY clause, but that only applies to the individual rows before the grouping.
having(Aggregation function condition processing)

Query to sum some of the values

Ive got a simple query that is used on a search. My problem is with this query is that as the records in mysql are added everytime there is a transaction, the query returns a list of data when there could only be one or a few more rows instead of a lot more.
SQLFliddle
As you can see here - the query returns a lot of rows, where I want it to return
BLSH103 A001A 31 24/01/2014
Can the qty where the product name & pallet space are the same be summed? And then show the largest date?
just use a sum function on t.Quantity (and a group by clause)
SELECT (t.ProductName) as Pname ,(s.PalletSpace) as PSpace, sum(t.Quantity) as Qty,(t.TransactionDate) as Transac
FROM PalletSpaces s
JOIN ProductTrans t
ON s.PalletSpaceID = t.PalletSpace
WHERE t.ProductName LIKE 'BLSH103' OR s.PalletSpace LIKE 'BLSH103'
group by
Pname,
pSpace,
Transac -- if you want to group by date also...
By the way, using LIKE this way (without %) doesn't make much sense...
see SqlFiddle
You just need to use GROUP BY and SUM in this way:
SELECT (t.ProductName) as Pname ,(s.PalletSpace) as PSpace, SUM(t.Quantity) as Qty,(t.TransactionDate) as Transac
FROM PalletSpaces s
JOIN ProductTrans t
ON s.PalletSpaceID = t.PalletSpace
WHERE t.ProductName LIKE 'BLSH103' OR s.PalletSpace LIKE 'BLSH103'
GROUP BY t.ProductName, s.PalletSpace;

MySQL COUNT on WHERE Statement

Pleas help me on why COUNT condition on WHERE statement commits an error, and how could i fix it?.
SELECT jq.taskqueueid,jq.jobid
FROM (SELECT p.taskID `curentTaskID`,
p.taskName `currentTaskName`,
p.processingType `currentProcessingType`,
p1.taskID `prevTaskID`,
p1.taskName `prevTaskName`,
p1.processingType `prevProcessingType`
FROM projecttask p
LEFT JOIN projecttask p1
ON p.sequenceNo=p1.nextTaskSequence
AND p.projectID=p1.projectID
WHERE p.taskID=18) task
INNER JOIN taskslogs tl
ON tl.taskID=task.`prevTaskID`
AND tl.statusDefinitionID=1
INNER JOIN jobqueue jq
ON tl.taskqueueid=jq.taskqueueid
WHERE COUNT(jq.taskqueueid)=COUNT(tl.taskqueueid)
to use an aggregate function like COUNT() you need to do a grouping of data, if you want to use it as a condition you can't use WHERE for this, since WHERE conditions are considered before aggregation. Use GROUP BY with HAVING instead. (see also http://dev.mysql.com/doc/refman/5.0/en/select.html )

count(*) in mysql returning only one record

I want to count how many records from another table in the same select statement , i used Left join
and in the select statement i put count(ag.*)
see the
Example :
$q = Doctrine_Query::create()
->select("a.answer_id,a.date_added , count(ag.content_id) AS agree_count")
->from('Answer a')
->leftJoin("a.Agree ag ON a.answer_id = ag.content_id AND ag.content_type = 'answer' ")
->where('a.question_id= ? ', $questionId)
But its only returning the first record, can i Fix that? or to make another table and make it only for counting ?
You are missing a GROUP BY in your query.
More infos here.
When you don't have a GROUP BY clause, it's normal to get only one row.
Count(*) will only return one record if you don't use Group By. You are asking it to count all the records, so there can be only one result.
The count() SQL function changes how results are returned from the database - without a GROUP BY the database will only return one record, regardless of other colums in the SELECT.
if you add:
group by a.answer_id
to the end of your SQL query, that might DWYM.

Using an "AS" clause in a sub-query than contains a "WHERE" clause

I want to store result in the variable generated by using "AS" clause in the MS Access,
and use this result in the sub-query with WHERE clause.
I tried this:
SELECT en_date AS date_en, (select sum(amount)
from main where
CrDb='Cr'
and
en_date=date_en) AS CR_AMT
FROM main
GROUP BY en_date;
I'm fairly certain you can't use an alias (the AS destination) in the same SELECT you defined it in.
I can't tell quite what you're trying to do, but it kind of looks like you want to be joining the table to itself.
SELECT
en_date,
SUM(amount)
FROM
main a
INNER JOIN
(
SELECT
en_date AS date_en,
CrDb
FROM
main
WHERE
CrDb='Cr'
)b
ON
a.en_date = b.date_en
AND a.CrDb = b.CrDb
GROUP BY
en_date
select m.en_date date_en, sum(m.amount)
from main m
where CrDb = 'Cr'
group by m.en_date
In other words, I dont think you even need a sub-query to get the results you are looking for.