Let's say I have table like this:
some_id
date
1
2022-02-01
2
2022-02-02
3
2022-02-03
3
2022-02-04
3
2022-02-05
3
2022-02-06
I want to get the number of rows based on the id where the date was found?
I tried this but it's not working:
SELECT COUNT(id) FROM dates WHERE date = '2022-02-04'
Expected output should be 4 rows since there are 4 same id's where the 2022-02-04 was found.
This should do the job:
SELECT COUNT(*) FROM tbl
WHERE id IN (
SELECT id FROM tbl WHERE `date`='2022-02-04'
)
An exists query should do it:
SELECT id, COUNT(*)
FROM t
WHERE EXISTS (
SELECT 1
FROM t AS x
WHERE x.id = t.id
AND x.date = '2022-02-04'
)
GROUP BY id
Using exists logic we can try:
SELECT COUNT(*)
FROM dates d1
WHERE EXISTS (SELECT 1 FROM dates d2
WHERE d2.some_id = d1.some_id AND
d2.date = '2022-02-04');
Given this transactions table
ID Date Name Stat1
1 1 a 1
1 2 b 1
1 3 c 1
2 4 d 1
2 5 e 1
I want to group the records by ID, and have the column 'Name' per group ID to show the last name by date as it is appears in table (high number in date is chronologically last). final result:
ID Date Name Stat1
1 3 c 3
2 5 e 2
Unfortunately this code:
Select ID,Date,Name,sum(Stat1)
From myTable
Group by ID
will generate "random" name,
how do i force choose the first/last record name(by date) in each group (ID)?
Thanks
You can do it with a query like this.
Change min to max if you want to have the first or last row of a group:
SELECT
result.id,
result.mdate,
mt.name,
result.stat1
FROM (
Select
ID,
min(date) mdate,
sum(Stat1) as stat1
From myTable
Group by ID
) AS result
LEFT JOIN myTable mt
ON mt.id = result.id
AND mt.date = result.mdate;
I have a MySQL table
discount_vouchers
------------------
id
email
test_id
My goal is to list all vouchers that appears more than once with a given email and a given test_id from the GROUP BY:
GROUP BY email, test_id
HAVING count(*) >1
How to get read of this group by?
Here is an example:
discount_vouchers
------------------
1 1#test.com 20
2 1#test.com 10
3 1#test.com 20
4 2#test.com 30
I would like to have as a result:
id email test_id count
1 1#test.com 20 2
2 1#test.com 10 1
3 1#test.com 20 2
4 2#test.com 30 2
Try something like the following
SELECT C2, counter from
(SELECT C2, COUNT(*) as counter FROM test.mytable
GROUP BY C2) as aggregation
WHERE counter > 1
Without using group by, you can do something like
SELECT a.* ,
(SELECT count(*) FROM discount_vouchers b
WHERE a.email = b.email AND a.test_id = b.test_id) as count
FROM discount_vouchers a
How about this?
Aggregate using a subquery, and use its results in order to enrich the actual table:
SELECT `discount_vouchers`.*, `counts`.`count`
FROM `discount_vouchers`
INNER JOIN (SELECT `email`, `test_id`, Count(*) AS 'count'
FROM `discount_vouchers`) AS `counts`
ON `discount_vouchers`.`email` = `counts`.`email`
AND `discount_vouchers`.`test_id` = `counts`.`test_id`;
I have two Tables named SavingAccountTransactions and LoanAccountTransactions.
Both the tables are having same columns (fk_MemberId,fk_AgentId,TransactionDate)
Sample Data
SavingAccountTransactions Table
Id fk_MemberId fk_AgentId TransactionDate Amount
--- ----------- ----------- --------------- -------
1 441 12 2014-12-30 100
2 452 6 2014-12-30 300
LoanAccountTransactions Table
Id fk_MemberId fk_AgentId TransactionDate Amount
--- ----------- ----------- --------------- -------
1 441 12 2014-12-30 500
2 452 6 2014-12-30 200
How can i combine SavingAccountTransactions and LoanAccountTransactions tables to get the count of NoOfMembersVisited in a single row for particular member.
Below is the query which i have tried
select u.Fullname as AgentName ,count (distinct t.fk_MemberId ) as NoOfMembersVisited
from dbo.SavingAccountTransactions T
inner join Users u on t.fk_AgentId=u.Id
where t.TransactionDate = CAST(GETDATE() AS DATE)
group by u.Fullname
Union all
select u.Fullname as AgentName, count (distinct l.fk_MemberId ) as NoOfMembersVisited
from LoanAccountTransactions L
inner join Users u on L.fk_AgentId=u.Id
where l.TransactionDate = CAST(GETDATE() AS DATE)
group by u.Fullname
Expected Result:
AgentName NoOfMembersVisited
--------- -------------------
John 2
Kevin 1
etc....
Because you have not provided any sample data it is hard to know what you are getting. But I might have an idea what you problem is. When you use the UNION ALL the users get repeated if they are in both of the table. Because it might be different counts from the tables changing the UNION ALL to UNION will not work. I would like to suggest you to do something like this:
;WITH CTE
AS
(
select u.Fullname as AgentName,l.fk_MemberId
from dbo.SavingAccountTransactions T
inner join Users u on t.fk_AgentId=u.Id
where t.TransactionDate = CAST(GETDATE() AS DATE)
UNION ALL
select u.Fullname as AgentName,l.fk_MemberId
from LoanAccountTransactions L
inner join Users u on L.fk_AgentId=u.Id
where l.TransactionDate = CAST(GETDATE() AS DATE)
)
SELECT
CTE.Fullname,
COUNT(distinct CTE.fk_MemberId) as NoOfMembersVisited
FROM
CTE
group by
CTE.Fullname
As I am not good with MySQL query's so I wish someone help me for creating this kind of sql query.
I having two MySQL tables which is describe bellow:
Table Name: rating
-------------------
property_id user_id area_rate_count safety_rate_count friendly_rate_count walkability_rate_count
4 28 1 1 1 2
5 38 2 3 4 1
5 40 2 2 3 1
6 40 2 3 1 4
10 43 2 2 3 1
Table Name: listing
-------------------
property_id title
4 Sample 1
5 Sample 2
6 Sample 3
10 Sample 4
11 Sample 5
12 Sample 6
Now first I want to sum each column and divide. (area_rate_count, safety_rate_count, friendly_rate_count, walkability_rate_count). For example In property_id:5 having two times so first calculate column sum and divide by 2.
After calculation we will get this output:
Table Name: rating (After Calculation)
--------------------------------------
property_id rate
4 5
5 9 (Divided by 2 because this property_id is two times in table)
6 10
10 8
And Finally I want join this result to my listing table and result looks something like this:
Table Name: listing
-------------------
property_id title rate
4 Sample 1 5
5 Sample 2 9 (Divided by 2 becouse property_id is two times in table)
6 Sample 3 10
10 Sample 4 8
11 Sample 5 0
12 Sample 6 0
Thanks.
I think you want the avg() aggregation function along with a join:
select l.property_id, l.title,
coalesce(avg(area_rate_count + safety_rate_count + friendly_rate_count + walkability_rate_count
), 0) as rate
from listing l left outer join
property_id p
on l.property_id = p.property_id
group by l.property_id, l.title ;
If I understood it right I think you need this:
select l.property_id, l.title, coalesce(r.ssum/if(r.ct=0,1,r.ct), 0) as rate
from listing l LEFT JOIN
(select property_id,
sum(area_rate_count+safety_rate_count
+friendly_rate_count+walkability_rate_count) ssum,
count(*) ct
from rating
group by property_id ) r
ON l.property_id = r.property_id
order by l.property_id
See it here on fiddle: http://sqlfiddle.com/#!2/589d6/5
Edit
As OP asked on the comments that he wants all columns from listing here is what he want:
select l.*, coalesce(r.ssum/if(r.ct=0,1,r.ct), 0) as rate
from listing l LEFT JOIN
(select property_id,
sum(area_rate_count+safety_rate_count
+friendly_rate_count+walkability_rate_count) ssum,
count(*) ct
from rating
group by property_id ) r
ON l.property_id = r.property_id
order by l.property_id
CREATE TEMPORARY TABLE IF NOT EXISTS
temp_table ( INDEX(col_2) )
ENGINE=MyISAM
AS (
SELECT
property_id,
AVG(area_rate_count) as area_rate_count,
AVG(safety_rate_count) as safety_rate_count,
AVG(friendly_rate_count) as friendly_rate_count,
AVG(walkability_rate_count) as walkability_rate_count
FROM rating
GROUP BY property_id
)
SELECT * FROM listing L
JOIN temp_table T
ON L.property_id = T.property_id
Use the below statement to get distinct property_id with its own rate
select property_id, sum(separaterating)/count(property_id) from (
select property_id,sum(area_rate_count , safety_rate_count , friendly_rate_count , walkability_rate_count) as separaterating from rating group by property_id AS temp ) group by
property_id
you can then join with the other table to get the final result as below
select * from ( select property_id, sum(separaterating)/count(property_id) from (
select property_id,sum(area_rate_count , safety_rate_count , friendly_rate_count , walkability_rate_count) as separaterating from rating group by property_id AS temp ) group by
property_id) AS A inner join listing AS B on A.property_id = B.property_id
try this:
select a.prop_id as property_id, l.title, a.allratings / b.numberofreviews as rate
from
(
select property_id as prop_id, SUM(coalesce(area_rate_count,0) + coalesce(safety_rate_count,0) + coalesce(friendly_rate_count,0) + coalesce(walkability_rate_count,0)) as allratings
from rating
group by property_id
) a inner join
(
select property_id, count(distinct user_id) as numberofreviews
from rating
group by property_id
) b on a.property_id = b.property_id
inner join listing l on a.property_id = l.property_id
Try This Query
select ls.property_id,ls.title,inr.rate from listing as ls
left join
(select r.property_id as pid,r.rate/r.cnt as rate from
(select property_id,user_id,(area_rate_count+safefty_rate_count+friendly_rate_count+walkability_rate_count) as rate,count(*) as cnt from rating group by property_id) as r) as inr on inr.pid=ls.property_id