how to count 2 table into another one in access 2013? - ms-access

I have 2 system log table in access 2013, one is loged by email and another one is loged by userid.
log1
email datetime msg
log2
userid datetime msg
I want to count how many msg the user send by month and save into a table user_msg_count
yearmonth userid msg_count
SELECT format([log1].datetime,"yyyy/mm") AS yearmonth, log1.email, COUNT(log1.msg) AS msg_count INTO user_msg_count
FROM log1
GROUP BY FORMAT(log1.datetime,"yyyy/mm", log1.email;
then I get userid from users table
users
userid email
UPDATE user_msg_count, users SET user_msg_count.userid = users.userid
WHERE user_msg_count.email = users.email;
but I dont know how to plus the log2 into the user_msg_count table.

Is the email the sender (User) email or the receipient?
If User email:
LogUnion query:
SELECT Users.UserID, Format([DateSend],"yyyymm") AS YrMo, "L1" AS Source FROM Users INNER JOIN Log1 ON Users.emailAdd = Log1.email
UNION ALL SELECT UserID, Format([DateSend],"yyyymm"), "L2" FROM Log2;
TotalCount query:
SELECT LogUnion.UserID, LogUnion.YrMo, Count(*) AS CountMsg
FROM LogUnion
GROUP BY LogUnion.UserID, LogUnion.YrMo;
Nested all-in-one query:
SELECT LogUnion.UserID, LogUnion.YrMo, Count(*) AS CountOfSource
FROM (SELECT Users.UserID, Format([DateSend],"yyyymm") AS YrMo, "L1" AS Source FROM Users INNER JOIN Log1 ON Users.emailAdd = Log1.email
UNION ALL SELECT UserID, Format([DateSend],"yyyymm"), "L2" FROM Log2) AS LogUnion
GROUP BY LogUnion.UserID, LogUnion.YrMo;

Related

Mysql self join for unique combinations

I have a table session as below:
sessionid conversationid agentid
SES153130074572578571 001a60f3-d95c-4434-a8c0-fabb7d815277_OD112807172056802000 igs.399760
SES15313020119321989 001a60f3-d95c-4434-a8c0-fabb7d815277_OD112807172056802000 hgs.20073
SES153130276854998136 001a60f3-d95c-4434-a8c0-fabb7d815277_OD112807172056802000 igs.100000000308235
SES153128080803552749 00803e49-0325-4bef-b133-8c5e97f2fccc_OD112706138643350000 hgs.20031
SES153128342589414965 00803e49-0325-4bef-b133-8c5e97f2fccc_OD112706138643350000 hgs.20031
SES153129466185930775 008bba3a-98fa-414a-8f99-96956513bc66_OD112757403158502000 igs.401836
SES153129612178429544 008bba3a-98fa-414a-8f99-96956513bc66_OD112757403158502000 hgs.20076
SES153129179161527928 00976601-aac7-4ec7-9e03-61cd59875650_Payment Related Queries hgs.20071
SES153129238786010778 00976601-aac7-4ec7-9e03-61cd59875650_Payment Related Queries igs.100000000307102
i want the rows where a conversationid is assigned to only one agent id (cases where one conversationid is assigned to more than one agentid would be omitted)
i.e. my output should be as below:
sessionid conversationid agentid
SES153128080803552749 00803e49-0325-4bef-b133-8c5e97f2fccc_OD112706138643350000 hgs.20031
SES153128342589414965 00803e49-0325-4bef-b133-8c5e97f2fccc_OD112706138643350000 hgs.20031
Kindly help with the query
select sessionid,agentid,conversationid from session as S
inner join
(
select agentid from session
group by agentid
having count(*)=1
) T
on S.agentid=T.agentid
Try the following:
select s.sessionid, s.agentid, s.conversationid
from session s
inner join
(
select conversationid
from session
group by conversationid
having count(distinct agentid) = 1
) uq
on s.conversationid = uq.conversationid
uq will contain all conversationid where the number of distinct agents is 1. Join that with the original data to get all rows for those conversationid.

SQL: count rows from 3rd table

I have 3 tables, the first table is the account_has_account1 where i store the relation between accounts and it's columns are account_id, account_id1, status where account_id is the account doing following to account_id1 and status is an enum type with values active, inactive where active indicates if the account is actually following, if it's inactive, then account stopped following.
the second table is named account_has_photos which i store the photos one account has stored in the database, so it's columns are account_id, photos_id, so i need this table to get all photos from one account which another account is following.
But all these have messages posted on them, and here is where the 3rd table comes which is named photos_has_message_photos, from this table i only need a count of all posted messages in one photo, the columns are photos_id, message_photos_id
for now my query is this:
SELECT account_has_photos.photos_id as id, "photos" as type, account_has_photos.update_at, account_has_photos.account_id
FROM account_has_account1
JOIN account_has_photos
ON (account_has_photos.account_id = account_has_account1.account_id1 AND account_has_photos.type_id = 17)
WHERE account_has_account1.account_id = 7 AND account_has_account1.`status` = "Active"
it shows all photos from accounts on which account id 7 is following, but on my attempts on getting the total messages have failed, i thought on doing an INNER JOIN like this:
INNER JOIN (
SELECT photos_has_message_photos.photos_id, count(photos_has_message_photos.photos_id) as total
FROM photos_has_message_photos
) posts
ON(posts.photos_id = account_has_photos.photos_id)
and then i select from main posts.total, but it does not show any row, not even the photos, the result is empty at this point and i have no idea why and what to do.
the complete query is like this:
SELECT account_has_photos.photos_id as id, "photos" as type, account_has_photos.update_at, account_has_photos.account_id, posts.total
FROM account_has_account1
JOIN account_has_photos
ON (account_has_photos.account_id = account_has_account1.account_id1 AND account_has_photos.type_id = 17)
INNER JOIN (
SELECT photos_has_message_photos.photos_id, count(photos_has_message_photos.photos_id) as total
FROM photos_has_message_photos
) posts
ON(posts.photos_id = account_has_photos.photos_id)
WHERE account_has_account1.account_id = 7 AND account_has_account1.`status` = "Active"
again, i only need a total of rows which are messages from each photos found
Try this query updated inner select
SELECT ahp.photos_id as id, "photos" as type, ahp.update_at, ahp.account_id,posts.total
FROM account_has_account1
JOIN account_has_photos
ON (ahp.account_id = account_has_account1.account_id1 AND ahp.type_id = 17) INNER JOIN (
SELECT phmp.photos_id, count(*) as total FROM photos_has_message_photos GROUP BY phmp.photos_id
) posts
ON(posts.photos_id = ahp.photos_id) WHERE account_has_account1.account_id = 7 AND account_has_account1.`status` = "Active"

Sql renaming several raw data

I have been through some solutions published here, but none of them solved my problem.
I want to set my username column to receive now unique usernames.
But for that I need to rename in certain situations, more than 1000 duplicated usernames already registered.
I tried this solution:
UPDATE profile n
JOIN (SELECT username, MIN(profile_id) min_id FROM profile
GROUP BY username HAVING COUNT(*) > 1) d
ON n.username = d.username AND n.profile_id <> d.min_id SET n.username = CONCAT(n.username, '1');
But it gives me for the same user name for example tony, tony1, tony11, tony111 and so on up to tony1111111111111... up to 1000, make the username have a long long lenght.
I would like a solution to get only up 4digites after the username word. 0001,0002,0003,0004....1000
Can somebody help me here?
Thank you in advance
how about something like:
UPDATE profile n JOIN (
SELECT profile_id, username, (#row_number:=#row_number+1) as cntr
FROM profile, (SELECT #row_number:=0) AS t
WHERE username IN ( SELECT username
FROM profile
GROUP BY username HAVING COUNT(*) > 1 )
and (username, profile_id) not in ( SELECT username, MIN(profile_id)
FROM profile
GROUP BY username HAVING COUNT(*) > 1 )
) d ON n.profile_id = d.profile_id
SET n.username = CONCAT(n.username, d.cntr);
This is the best I can come up with at the moment.... the problem is that it will share the counter between all usernames... you you will have Alejandro, Alejandro1, Pedro, Pedro2, Juan, Juan3 .....
I believe that this that you've commented is wrong...No Update. Only select: select * from profile n JOIN ( SELECT username, min_id, #row_number:=#row_number+1 as cntr FROM ( SELECT username, MIN(profile_id) min_id FROM profile GROUP BY username HAVING COUNT(*) > 1 ) AS t2 , (SELECT #row_number:=0) AS t ) d ON n.username = d.username

SQL JOIN, Replace id with value

I have a query that is giving me trouble, im not sure how to do this
I have to retrieve records from a TICKETS table and join it together with 2 others,, That is not the problem, i need to replace one of the values in the record for a value in another table....
For Eg:
Table tickets:
numint
user_id
type
desc
attach
priority
status
date
assignation
Table users
numint
company_id
name
email
username
password
ps
security
token
date
Table companies
numint
name
Example Record
company_name - user_name - type - title - priority - status - date - assignation
"someCompany" - "someUser" - "someTitle" - 1 - "open" - "yyy/mm/dd" - 2(user_id)
in the assignation field of the returned record i need to replace it with the correspondant value from the users table IF it's NOT 0 (zero)
This is my Query so far
SELECT tickets.numint, tickets.type, tickets.title, tickets.description,
tickets.priority, tickets.status, tickets.date,
users.name AS user_name, companies.name AS company_name,
CASE WHEN tickets.assignation=0 THEN 'not-assigned'
ELSE ????????? END AS assignation
FROM tickets
LEFT JOIN users ON tickets.user_id = users.numint
LEFT JOIN companies ON users.company_id = companies.numint
i dont know how to replace that value, or if the CASE should after the joins...
Do you want to show username as assignation. use users.name in case.
SELECT tickets.numint, tickets.type, tickets.title, tickets.description,
tickets.priority, tickets.status, tickets.date,
users.name AS user_name, companies.name AS company_name,
CASE WHEN tickets.assignation=0 THEN 'not-assigned'
ELSE users1.name END AS assignation
FROM tickets
LEFT JOIN users ON tickets.user_id = users.numint
LEFT JOIN companies ON users.company_id = companies.numint
LEFT JOIN users AS users1 ON tickets.assignation = users1.numint

Fetching a result twice with deferend name

I have 1 table with tasks named opentask:columns: id,title,description,expires,creator_id,creator_name, executer_id, executer_name, priority_id, status_id1 table with users named user:
with columns: user_id, username
What I want is to create a query where there will be all columns from opentask table where the executer_id will be equal to the user_id of user table AND the creator_id will be equal to the user_id again. This creates a confusion because the first equality excludes the second.So I need somehow to create a query where I will include the usernames for the executer with something like where "opentask.executer_id=user_user_id" and at the same time I will include the username again (as a differend name?) for the creator with something like "where opentask.executer_id=user_user_id"So I try this, which of course I know that is missing something, can you help?
SELECT DISTINCT id, title, description, expires, creator_id, executer_id, oc_opentask.priority_id, oc_opentask.status_id, priority_name, status_name, user_id, username, (SELECT username FROM oc_opentask, oc_user WHERE oc_opentask.creator_id=oc_user.user_id) AS username2 FROM oc_opentask, oc_opentask_priority, oc_user, oc_opentask_status WHERE oc_opentask.priority_id=oc_opentask_priority.priority_id AND oc_opentask.executer_id=oc_user.user_id AND oc_opentask.status_id=oc_opentask_status.status_id ORDER BY oc_opentask.expires DESC
ReJOIN the table oc_user twice with different aliases creators and executers like so:
SELECT DISTINCT
t.*,
creators.user_name 'Creator name',
executers.username 'Executor name',
tp.priority_name,
s.status_name
FROM oc_opentask t
INNER JOIN oc_opentask_priority tp ON t.priority_id = tp.priority_id
INNER JOIN oc_user executers ON t.executer_id = executes.user_id
INNER JOIN oc_user creators ON t.creator_id = creators.user_id
INNER JOIN oc_opentask_status s ON t.status_id = s.status_id
ORDER BY t.expires DESC