So, I have this:
SELECT COUNT(mailing_recipient_id) AS Total_Recipient,
(SELECT COUNT(mailing_recipient_click_id)
ORDER BY(mailing_recipient_click_type_id)) AS Open_Count,
(SELECT COUNT(cons_action_contribution_id)) AS Total_Donations,
(SELECT COUNT(cons_id) ORDER BY cons_action_contribution_id) AS Donations_Per_Recipient
FROM cons LEFT JOIN mailing_recipient
ON cons.cons_id =mailing_recipient.cons_id
JOIN mailing
ON mailing_recipient.mailing_id = mailing.mailing_id
JOIN mailing_recipient_click
ON mailing_recipient_click.mailing_recipient_id = mailing_recipient.mailing_recipient_id
JOIN cons_action_contribution
ON cons_action_contribution.con_id = cons.cons_id
GROUP BY(mailing_id);
What I am hoping to achieve is a summary including recipient count, open count, opens per recipient, click count, clicks per recipient, number of donation, and donations per recipient.
That is not what I am a getting, and I am unsure why.
mailing_recipient_click_id shows if someone open/clicked the email.
mailing_recipient_click_type_id (1=click, 2=open)
I'm failing to get tables now, but What I am hoping to achieve is the following:
Mailing Recipient Open Open_per Clicks Click_per Donation Donation_per
1 10000 5000 5 4000 3 500 2
2 5000 2500 1 4000 1 50 1
Related
This is a follow up question to my previous one:
SQL Query multiple sums and avarage of total
I got the previous question working with Madhur Bhaiya's answer:
SELECT SUM(h.hours) / COUNT(DISTINCT h.USER_ID) AS daily_average
FROM hours AS h
WHERE h.date = CURDATE()
Though I realised that I have a follow up question which is:
In my hours table I have two users who have reported their time, though I'd like it to count on the avarage of all the users that exists.
The hours database looks like this:
#USER_ID #HOURS #DATE
1 2 2018-01-01
1 1 2018-01-01
2 5 2018-01-01
1 3 2018-01-02
2 8 2018-01-02
2 1 2018-01-02
That information is in my 'users' database, which looks like this:
#USER_ID #USER_NAME
1 aaa
2 bbb
3 ccc
Any idéas on how to do this? I assume I'd have to join the tables 'hours' and 'users'?
The desired output will be (BASED ON 2018-01-01):
(HIDDEN INFO: USER 1 TOTAL = 3)
(HIDDEN INFO: USER 2 TOTAL = 5)
(HIDDEN INFO: USER 3 TOTAL = 0)
**AVARAGE: 2,666666667**
SELECT SUM(h.hours) / COUNT(DISTINCT u.USER_ID) AS daily_average
FROM hours AS h
RIGHT JOIN users u ON h.USER_ID = u.USER_ID
WHERE h.date = CURDATE()
Your issue of not taking into account users that haven't recorded any hours is that they don't exist on the hours table, so using the count of distinct users on the hours table isn't what you are looking for.
If you join the users table you can get the count of all users in the system rather than ones that haven't recorded hours, this should get you your average for all users, not just the ones that have recorded hours.
use left join
select SUM(h.hours) / COUNT(DISTINCT users.USER_ID) AS daily_averag
from users left join hours on users.USER_ID=hour.user_id
You want a LEFT JOIN and to put the condition for hours in the ON clause:
SELECT SUM(h.hours) / COUNT(DISTINCT u.user_id) as daily_average
FROM users u LEFT JOIN
hours h
ON h.user_id = u.user_id and h.date = CURDATE();
The above is totally correct. However, it might be more performant to just use a subquery on users:
SELECT SUM(h.hours) / (SELECT COUNT(*) FROM users) as daily_average
FROM hours h
WHERE h.date = CURDATE();
This question may already asked, but my question refers to two separate tables. I have only seen the answer when using same table with date row on it.
I have two tables, one is the user table and the other is login_log table.
I want to get the event from latest date record and from each user.
I have already made sql query, but cant understand how can I pick the max(date) from separate user.
SELECT s.username_id,s.date,s.event,m.username
FROM sys_log s join
users m
on s.username_id = m.id
id, username
---------
1 test
2 test2
id, username_id, date, event
------------------------------------------
1 1 1/1/2017 22:10:11 logout
2 1 1/1/2017 22:09:11 login
3 2 1/1/2017 21:05:11 logout
4 2 1/1/2017 21:02:11 login
the output should be like this
id, username, event ,date
-------------------------
1 test logout xxx
2 test2 logout xxx
Does this do what you want?
select s.username_id, s.date, s.event, m.username
from sys_log s join
users m
on s.sys_auth_id = m.id
where s.date = (select max(s2.date) from sys_log s2 where s2.sys_auth_id = s.sys_auth_id);
I am fetching data from 4 different tables:
leads
payu_transactions
corporate_user_rides
corporate_users
And there are some conditions:
user rides should be grater than 0
There should be some number of registered and active users
There will be some time period
I have written some SQL queries but i am not getting expected result-
The problem is with number of rides count and user count.
For e.g-
Lets say corporate x actually having 38 rides and 23 users but it's showing 7866 rides and 7866 users.
Another corporate y is actually having 18 rides and 5 users but it's showing 90 rides and 90 users.
Can anyone please help, i am not sure what's i am doing here.
I tried this-
query
SELECT l.id AS leadId,
l.corporate_id AS CorporateID,
"P-1" AS priority,
l.source,
l.user_name AS FirstName,
l.user_name AS LastName,
l.corporate_name AS corpName,
l.user_mail_id AS email,
l.phone_number AS phone,
l.created_At AS leadCreation,
l.comments,
Count(CU.id) AS users,
Count(CUR.id) AS rides,
PUT.amount AS payment
FROM leads l
LEFT JOIN payu_transactions PUT
ON l.user_mail_id = PUT.email
LEFT JOIN corporate_user_rides CUR
ON l.corporate_id = CUR.corporate_id
LEFT JOIN corporate_users CU
ON l.corporate_id = CU.corporate_id
WHERE l.created_at BETWEEN '2015-03-16 12:00:00' AND '2016-03-17 12:00:00'
GROUP BY l.user_mail_id
HAVING Count(CUR.id) > 0
AND Count(CU.id) > 0
AND Count(CASE
WHEN CU.status IN ( 'active' ) THEN 1
END) > 0;
Help will be appreciated.
Use Count (Distinct column.name) instead of Count(column.name).
I have a simple table maintaining messages between users. The table structure looks like
sender receiver message sendtime
1 2 m1 2012-01-01 12:12:12
2 1 m2 2012-01-01 12:50:20
1 2 m3 2012-01-01 12:55:55
1 3 m4 2012-01-02 05:05:05
1 4 m5 2012-01-05 05:20:20
4 1 m6 2012-01-06 06:05:00
4 1 m7 2012-01-07 11:11:11
2 4 m8 2012-01-08 05:01:01
Now, for the user with ID 1, I need the result like this
sender receiver message sendtime
1 2 m3 2012-01-01 12:55:55
1 3 m4 2012-01-02 05:05:05
4 1 m7 2012-01-07 11:11:11
That is I need the recent messages for the particular user whether he is sender or receiver.
Though, it seem easy, I cannot find a way to write a single mysql query.
Also, I want suggestion if for this kind of solution, my table design is poor.
Note: One thing, which I think I should mention is, for instance there are multiple communication between user 1 and user 2. I need the latest communication between them two whether user 1 is a sender or receiver, only the recent.
Most users are suggesting the query, that will bring one record with the sender 1, receiver 2 and then sender 2, receiver 1. I don't need these two record because this is a communication between same user 1 and user 2. I need one the latest one for the user specified with each of other users.
Thanks,
SELECT data.* FROM
(SELECT MAX(sendtime) AS sendtime
FROM data
WHERE 1 IN (sender,receiver)
GROUP BY IF (1 = sender,receiver,sender)) AS latest
LEFT JOIN data ON latest.sendtime = data.sendtime AND 1 IN (data.sender, data.receiver)
GROUP BY IF (1 = data.sender,data.receiver,data.sender)
I recommend to use unique sequence id in your table to avoid external GROUP BY.
If you have incrementing unique message_id (i.e. bigger message_id corresponds to later sendtime), the query would be much simpler:
SELECT data.* FROM
(SELECT MAX(message_id) AS message_id
FROM data
WHERE 1 IN (sender,receiver)
GROUP BY IF (1 = sender,receiver,sender)) AS latest
LEFT JOIN data USING(message_id)
Try making message as the primary key and put sender and receiver in 2 separate tables for the same message like this:
Table 1: {message, sender, sendtime}
Table 2: {message, receiver, sendtime}
Try this:
SELECT Distinct (*)
FROM tableName m
WHERE date =
(
SELECT MAX (date)
FROM tableName
)
To me it feels simpler to use an own ID based on the sender and receiver. No JOIN and just one GROUP BY needed:
SELECT sender, receiver, message, MAX(time)
FROM (
SELECT *,
CASE WHEN sender < receiver
THEN CONCAT(sender,'&', receiver)
ELSE CONCAT(receiver,'&', sender) END AS fromto
FROM data) AS a
GROUP BY fromto
I take 30 mins to find the solution for this problem because I have thew same problem
just take it simple
SELECT DISTINCT sent_by FROM messages WHERE sent_to = 1 UNION SELECT DISTINCT sent_to FROM messages WHERE sent_by = 1
This will work out
$query = "SELECT * FROM tableName WHERE `sender`='$userNumber' OR `receiver`='$userNumber' ORDER BY `sendtime` DESC LIMIT 0,1";
I have a simple table maintaining messages between users. The table structure looks like
sender receiver message sendtime
1 2 m1 2012-01-01 12:12:12
2 1 m2 2012-01-01 12:50:20
1 2 m3 2012-01-01 12:55:55
1 3 m4 2012-01-02 05:05:05
1 4 m5 2012-01-05 05:20:20
4 1 m6 2012-01-06 06:05:00
4 1 m7 2012-01-07 11:11:11
2 4 m8 2012-01-08 05:01:01
Now, for the user with ID 1, I need the result like this
sender receiver message sendtime
1 2 m3 2012-01-01 12:55:55
1 3 m4 2012-01-02 05:05:05
4 1 m7 2012-01-07 11:11:11
That is I need the recent messages for the particular user whether he is sender or receiver.
Though, it seem easy, I cannot find a way to write a single mysql query.
Also, I want suggestion if for this kind of solution, my table design is poor.
Note: One thing, which I think I should mention is, for instance there are multiple communication between user 1 and user 2. I need the latest communication between them two whether user 1 is a sender or receiver, only the recent.
Most users are suggesting the query, that will bring one record with the sender 1, receiver 2 and then sender 2, receiver 1. I don't need these two record because this is a communication between same user 1 and user 2. I need one the latest one for the user specified with each of other users.
Thanks,
SELECT data.* FROM
(SELECT MAX(sendtime) AS sendtime
FROM data
WHERE 1 IN (sender,receiver)
GROUP BY IF (1 = sender,receiver,sender)) AS latest
LEFT JOIN data ON latest.sendtime = data.sendtime AND 1 IN (data.sender, data.receiver)
GROUP BY IF (1 = data.sender,data.receiver,data.sender)
I recommend to use unique sequence id in your table to avoid external GROUP BY.
If you have incrementing unique message_id (i.e. bigger message_id corresponds to later sendtime), the query would be much simpler:
SELECT data.* FROM
(SELECT MAX(message_id) AS message_id
FROM data
WHERE 1 IN (sender,receiver)
GROUP BY IF (1 = sender,receiver,sender)) AS latest
LEFT JOIN data USING(message_id)
Try making message as the primary key and put sender and receiver in 2 separate tables for the same message like this:
Table 1: {message, sender, sendtime}
Table 2: {message, receiver, sendtime}
Try this:
SELECT Distinct (*)
FROM tableName m
WHERE date =
(
SELECT MAX (date)
FROM tableName
)
To me it feels simpler to use an own ID based on the sender and receiver. No JOIN and just one GROUP BY needed:
SELECT sender, receiver, message, MAX(time)
FROM (
SELECT *,
CASE WHEN sender < receiver
THEN CONCAT(sender,'&', receiver)
ELSE CONCAT(receiver,'&', sender) END AS fromto
FROM data) AS a
GROUP BY fromto
I take 30 mins to find the solution for this problem because I have thew same problem
just take it simple
SELECT DISTINCT sent_by FROM messages WHERE sent_to = 1 UNION SELECT DISTINCT sent_to FROM messages WHERE sent_by = 1
This will work out
$query = "SELECT * FROM tableName WHERE `sender`='$userNumber' OR `receiver`='$userNumber' ORDER BY `sendtime` DESC LIMIT 0,1";