I have tried looking for similar questions here in SO and have not found any so far. Feel free to tag the duplicate or similar question to this one if you'd like.
I want to join several columns to display a report.
I have 3 tables.
Users
ID Name
1 Chris
2 John
3 Rick
InMessages
ID Content
1 Hello1
2 Hello2
3 Response1
4 Response2
5 Hello3
6 Hello4
OutMessages
ID UserID InMessageID Content CurrentRate
1 1 1 ReplyHello1 10
2 2 2 Reply1Hello2 10
3 3 2 Reply2Hello2 10
4 3 2 Reply3Hello2 10
5 1 3 ReplyResponse1 10
6 2 4 ReplyResponse2 10
7 1 5 ReplyHello3 4
8 3 6 ReplyHello4 4
And the report I'd like to see would be something like:
User InMessagesCount OutMessagesCount Rate10 Rate4
Chris 3 3 2 1
John 2 2 1 1
Rick 2 3 2 1
I have tried the existing queries like:
count(distinct InMessages.ID) as InMessagesCount,
count(distinct OutMessages.ID) as OutMessagesCount
But I am stuck as to how I could make the one for the rates.
Thanks in advance!
You issue seems to be that your multiple joins are creating duplicate records, hence needing DISTINCT in your current counts. But you can't use that for counting the number of occurrences of a non distinct field
As such I would be tempted to try something like this:-
COUNT(DISTINCT IF(OutMessages.CurrentRate = 10, OutMessages.ID, NULL) ) AS Rate10,
COUNT(DISTINCT IF(OutMessages.CurrentRate = 4, OutMessages.ID, NULL) ) AS Rate4
Ie, if the id is 10 then use the id, otherwise use NULL. COUNT like this should (I think) only count the non null values.
For Mysql you can use sum() with expression so it will evaluate as a count that you need for your 2 rates
select
u.Name,
count(distinct o.InMessageID) as InMessagesCount,
count(distinct o.ID) as OutMessagesCount,
sum(o.CurrentRate = 10) rate10,
sum(o.CurrentRate = 4) rate10
from OutMessages o
joins users u on(u.id = o.user_id)
group by u.id
You can sum up those records that match a specific rate:
SUM(IF(OutMessages.CurrentRate = 10, 1,0)) AS Rate10,
SUM(IF(OutMessages.CurrentRate = 4, 1,0)) AS Rate4
You can use IF and SUM, like this:
SELECT UserID,
COUNT(DISTINCT InMessages.ID) AS InMessagesCount,
COUNT(DISTINCT OutMessages.ID) AS OutMessagesCount,
SUM(IF(OutMessages.CurrentRate = 10, 1, 0)) AS Rate10,
SUM(IF(OutMessages.CurrentRate = 4, 1, 0)) AS Rate4
FROM OutMessages
GROUP BY UserID
Related
I have a MySQL table called personxmessage, extracted from emails. It has three fields, id (primary key), MessageID, and PersonID. For each message referenced by MessageID, there are two or more records, each with a PersonID for a person who was included in the email (i.e. in sender, sent to, or cc).
I want to query this table to get a link list showing all the people who were linked to a given PersonID=XXXX, and the links between them defined by being included on the emails that include XXXX, preferably including a weight on the links showing the number of occurrences. Another way of saying this, in graph terminology, is I'm trying to get the neighborhood of XXXX. For example, for entries like
MID PID
1 1
1 2
1 3
2 1
2 2
3 1
3 3
3 4
For PersonID 1 I would like to get link list
P1 P2 Count
1 1 3
1 2 2
1 3 2
1 4 1
2 3 1
3 4 1
Is it possible to do this with some kind of self-join? If so what query would I use? I have done some simpler joins (for example to get the star-graph of XXXX and other PersonIDs that are with XXXX on emails) but this one is over my head.
You can use GROUP_CONCAT do to something sort of like that. It's not a table but a result set of the related pids per mid.
select mid, group_concat(distinct pid order by pid separator ', ') as related_ppl
from personxmessage
group by mid;
result
mid related_ppl
1 1, 2, 3
2 1, 2
3 1, 3, 4
I think this is what you're looking for:
select p.pid as pid1, pp.pid as pid2, count(*) as cnt
from personxmessagep
left join personxmessagepp
on p.mid = pp.mid
and p.pid < pp.pid
group by p.pid, pp.pid;
result
pid pid2 cnt
1 2 2
1 3 2
1 4 1
2 1
2 3 1
3 1
3 4 1
4 1
I'm trying to get the query below to show for each item for each store the amount of each of 4 items we have.
It works great, and I created the temporary table to try to increase speed but my problem is that if the table has no rows for a certain product that product does not show up at all.
I'd like to show all four products(prodNo) regardless of if there is actually any of rows for that specific store.
I researched this site and could not find something similar enough for me to figure it out.
CREATE TEMPORARY TABLE IF NOT EXISTS temp_invoice_dates AS
(
SELECT Invoice_detail.del_date,invoice_Detail.StoreNo,mast_stores.SDesc, invoice_Detail.ProdNo,sold_qty,retn_price,retn_qty,sold_price FROM Invoice_detail
LEFT JOIN mast_stores on invoice_detail.StoreNO=mast_stores.Snum
LEFT JOIN invoice on invoice_detail.Del_Date=invoice.Del_Date and invoice_detail.Invoice_No=invoice.Invoice_No
WHERE Cnum IN ('200','210') AND invoice_detail.Del_Date >= "2016-03-01" AND invoice_detail.Del_Date < "2016-04-01"
);
SELECT
temp_invoice_dates.StoreNo,
temp_invoice_dates.SDesc,
DATE_FORMAT(temp_invoice_dates.Del_Date,'%Y') as Year,
DATE_FORMAT(temp_invoice_dates.Del_Date,'%M') as Month,
temp_invoice_dates.ProdNo,
mast_items.IDesc,
SUM(sold_qty) as TotalIn,
SUM(retn_qty) as TotalOut,
ROUND(SUM((sold_qty*sold_price)-(retn_qty*retn_price)),2) as NetSales,
CONCAT(ROUND(SUM(retn_qty)/SUM(sold_qty),2)*100,'%') as StalePerc
FROM mast_Items
LEFT JOIN temp_invoice_dates on temp_invoice_dates.ProdNo=mast_items.Inum
WHERE mast_items.Inum in ('3502','3512','4162','4182')
GROUP BY temp_invoice_dates.StoreNo, ProdNo
ORDER BY temp_invoice_dates.StoreNo, ProdNo;
Drop table temp_invoice_dates;
Results are similar to:
StoreNo Product Count....
1 1 1
1 2 5
1 3 2
1 4 1
2 1 14
2 2 1
2 4 4
3 2 33
3 3 3
Where as I'd like it to be
StoreNo Product Count ....
1 1 1
1 2 5
1 3 2
1 4 1
2 1 14
2 2 1
2 3 0
2 4 4
3 1 0
3 2 33
3 3 3
3 4 0
Something like this should work.
SELECT sp.StoreNo, sp.ProdNo
, ...stuff...
, sp.IDesc, sp.SDesc
, ...more stuff...
FROM (
SELECT i.Inum AS ProdNo, s.Snum AS StoreNo
, i.IDesc, s.SDesc
FROM mast_Items AS i, mast_stores AS s
WHERE i.Inum IN ('3502','3512','4162','4182')
) AS sp
LEFT JOIN temp_invoice_dates AS tid
ON sp.ProdNo = tid.ProdNo
AND sp.StoreNo = tid.StoreNo
GROUP BY sp.StoreNo, sp.ProdNo
ORDER BY sp.StoreNo, sp.ProdNo
;
Normally I recommend against cross joins (as seen in the subquery) but in this case it is exactly what is needed. If the query is slow, you can instead insert the subquery results into a temp table beforehand, index that, and then use the temp table in place of the subquery.
(Edit: should use sp fields when available for grouping and results)
I'm trying to make an update on a table so that it can increment the values on 1 column depending on another's order.
Here's how it'd go
ID GROUP_ID ORDER(Desired) ORDER(NOW)
1 1 1 2
2 1 2 3
3 1 3 1
4 2 1 2
5 2 2 1
6 3 1 1
7 3 2 1
8 3 3 2
So what I need is for each ID, to update the ORDER column so it can be consecutive, starting from 1, within each GROUP_ID.
I have found some solutions to similar problems regarding the updates and orders, but none that uses multiple orders for groups within the same table.
Hope I illustrated the problem right. Thanks in advance
You can do it by "ranking" the rows over again. Mysql doesn't support window functions but you can achieve the same results with join and count like this:
UPDATE YourTable t
INNER JOIN(SELECT s.id,s.group_id,count(*) as cnt
FROM YourTable s
INNER JOIN YourTable ss
ON(s.group_id = ss.group_id and s.id >= ss.id)
GROUP BY s.id,s.group_id) tt
ON (t.id = tt.id and t.group_id = tt.group_id)
SET t.order = tt.cnt
I have read the different answers here on SO, but I am stuck on this question. Please help.
I have this mysql view named "activeuser":
userid COUNT(*) ACRONYM
1 23 admin
2 2 doe
3 4 tompa
12 4 Marre
13 1 Mia
1 2 admin
3 1 tompa
12 1 Marre
13 1 Mia
2 1 doe
3 1 tompa
12 1 Marre
How can I sum the COUNT column so that I get the following wanted result?
userid COUNT(*) ACRONYM
1 25 admin
2 3 doe
3 6 tompa
12 6 Marre
13 1 Mia
EDITED:
I used this query to create the view:
CREATE VIEW activeuser AS
(SELECT boats_comments.userid, COUNT(boats_comments.userid), boats_user.acronym, boats_user.email
FROM boats_comments
INNER JOIN boats_user
ON boats_comments.userid = boats_user.id
GROUP BY boats_comments.userid
ORDER BY COUNT(boats_comments.userid) DESC)
UNION ALL
(SELECT boats_answers.userid, COUNT(boats_answers.userid), boats_user.acronym, boats_user.email
FROM boats_answers
INNER JOIN boats_user
ON boats_answers.userid = boats_user.id
GROUP BY boats_answers.userid
ORDER BY COUNT(boats_answers.userid) DESC)
UNION ALL
(SELECT boats_questions.userid, COUNT(boats_questions.userid), boats_user.acronym, boats_user.email
FROM boats_questions
INNER JOIN boats_user
ON boats_questions.userid = boats_user.id
GROUP BY boats_questions.userid
ORDER BY COUNT(boats_questions.userid) DESC)
My goal is to see which users are the most active by checking the number of comments, questions and answers... but I got stuck...
As the results in your view has duplicates I guess the underlying code for the view is grouping on something it maybe shouldn't be grouping on.
You can get the results you want by applying SUM to it:
select userid, sum("whatever column2 is named") as "Count", Acronym
from activeuser group by userid, Acronym;
select userid, count(*) from activeuser group by userid;
I have the following 2 tables:
1) Companies
ID CompanyName Abbreviation Notes
1 CompanyA CA ...
2 CompanyB CB ...
3 CompanyC CC ...
2) PlannedDeployments
ID CompanyID TypeID DepDate NumDeployed
1 1 2 09/2010 5
2 1 2 10/2010 5
3 1 3 09/2010 3
4 1 3 10/2010 3
5 1 4 10/2010 4
6 2 2 12/2010 10
7 2 4 10/2010 1
8 3 2 11/2010 6
Note that TypeID is a number between 1 and 5 describing what type of person is being deployed. For the purposes of this query, I'm interested in Type2 employees for each company and then the sum of Types 3 & 4 for each date. What I eventually want to end up with is a crosstab that looks like the following:
Crosstab
Date/Company CompanyA CompanyB CompanyC SumOfTypes3and4
09/2010 5 3
10/2010 5 8
11/2010 6
12/2010 10
The problem is that final column - the sum of Type 3 and Type 4 employees. The current crosstab that I have includes everything except that sum column and looks like the following:
TRANSFORM Sum(PlannedDeployments.NumDeployed) AS ["NumDeployed"]
SELECT PlannedDeployments.DepDate
FROM PlannedDeployments LEFT JOIN Companies ON Companies.ID=PlannedDeployments.CompanyID
WHERE PlannedDeployments.TypeID=2 AND (PlannedDeployments.DepDate Between FormFieldValue("Form", "Control") AND FormFieldValue("Form", "Control"))
GROUP BY PlannedDeployments.DepDate
PIVOT Companies.CompanyName;
The second part of that WHERE clause is just limiting the data by some form controls. Anyway - I'm having a lot of trouble getting that final column. Anyone have any ideas?
Edit: Building on the solution provided by Remou below, here's what the final query ended up looking like:
TRANSFORM Sum(PlannedDeployments.NumDeployed) AS ["NumDeployed"]
SELECT PlannedDeployments.DepDate, q.SumOfNumDeployed
FROM (SELECT PlannedDeployments.DepDate, Sum(PlannedDeployments.NumDeployed) AS SumOfNumDeployed
FROM PlannedDeployments
WHERE (((PlannedDeployments.[TypeID]) In (3,4)))
GROUP BY PlannedDeployments.DepDate) AS q
RIGHT JOIN (PlannedDeployments
INNER JOIN Companies ON PlannedDeployments.CompanyID = Companies.ID)
ON q.DepDate = PlannedDeployments.DepDate
WHERE PlannedDeployments.TypeID=2
AND (PlannedDeployments.DepDate Between FormFieldValue("Form", "Control")
AND FormFieldValue("Form", "Control"))
GROUP BY PlannedDeployments.DepDate, q.SumOfNumDeployed
PIVOT Companies.CompanyName;
You can use a subquery:
TRANSFORM Sum(PlannedDeployments.NumDeployed) AS ["NumDeployed"]
SELECT PlannedDeployments.DepDate, Sum(q.SumOfNumDeployed) AS SumOfSumOfNumDeployed
FROM (SELECT PlannedDeployments.DepDate, Sum(PlannedDeployments.NumDeployed) AS SumOfNumDeployed
FROM PlannedDeployments
WHERE (((PlannedDeployments.[TypeID]) In (3,4)))
GROUP BY PlannedDeployments.DepDate) AS q
RIGHT JOIN (PlannedDeployments
INNER JOIN Companies ON PlannedDeployments.CompanyID = Companies.ID)
ON q.DepDate = PlannedDeployments.DepDate
WHERE PlannedDeployments.TypeID=2
AND (PlannedDeployments.DepDate Between FormFieldValue("Form", "Control")
AND FormFieldValue("Form", "Control"))
GROUP BY PlannedDeployments.DepDate
PIVOT Companies.CompanyName;