So I have and email data base and where each email send has it's own line. I'm trying to figure out how many times each user is being sent to. So I'm using this query to find out out how many times each user has been mailed.
SELECT count(`id`)
FROM `bde_export`
WHERE `record.type` = 'senttomta'
GROUP BY `user.id`
Now what I'm trying to do is count that count so I get a summary telling me how many users have been mailed 1,2,3,4 times and so on. I know that is a bit confusing please let me know if clarification is needed.
well why dont you try another count???
like this
SELECT count(`tmp`.*) FROM (
SELECT count(`id`)
FROM `bde_export`
WHERE `record`.`type` = 'senttomta'
GROUP BY `user`.`id`
) `tmp`
i dont know what exactly is your problem but with current explanation i think it works. i just copied your query and didnt change that.
but if it doesn't satisfy your meets, i suggest you to try this:
SELECT sum(`tmp`.`count`) FROM (
SELECT count(`id`) AS `count`
FROM `bde_export`
WHERE `record`.`type` = 'senttomta'
GROUP BY `user`.`id`
) `tmp`
You can do this with two selection statements.. try something like this..
SELECT SUM(s.EmailCount) as 'TotalCount', s.id
FROM
(
SELECT COUNT(id) as 'EmailCount', id
FROM
GROUP BY user.id
) AS s
GROUP BY s.EmailCount
Basically you do your initial select statement and select the sum of the initial count
Related
I have been trying to do this in many ways suggested.
Note: we do not want aggregate function or Partition since this is just a small part of whole Stored procedure and this is client requirement to not have it, so not in option and not possible duplicate of other existing answers / questions
I have a messages table, which has a column from and to, a foreign key to the user table, basically which user sends to whom at simplest. I also have other columns which are isSnoozed and snoozeAt for if the message is snoozed.
So the ordering is according to case. If messages is snoozed then consider snoozeAt time to Order or if not then consider sendAt. (right now we can ignore this condition while ordering, But I mentioned this since we cannot take simply MAX(id) )
I need to get recent most message from messages group by from user id
messages table like :
id -- to -- from -- isSnoozed -- snoozedAt -- sendAt ...
What I tried :
select * from ( select * from messages order by sendAt DESC) as TEMP GROUP BY TEMP.from
I tried many similar approaches but none worked.
I wasted many paid hours but can't find an approach which meets my exact requirement
NOTE: Please ignore typo in query if any, since I cant type in exact query table and names, So i typed in directly here
I figured this out by doing something like this, which could be explained in a simplified way:
select * from message where message.id in (
select
( select id from message where message.from = user.id order by CASE isSnoozed WHEN 0 THEN sendAt ELSE snoozeAt END DESC limit 1) as id
from user where user.id in ( select friends.`whoIsAdded` from friends where friends.`whoAdded` = myId)
) order by CASE isSnoozed WHEN 0 THEN sendAt ELSE snoozeAt END DESC
If I understand correctly, you just want the largest value in one of two columns. Assuming the values are never NULL, you can use greatest():
select m.*
from messages m
where greatest(m.sendAt, m.snoozedAt) =
(select max(greatest(m2.sendAt, m2.snoozedAt))
from messages m2
where m2.from = m.from
);
If the columns can be NULL, then you can use coalesce() to give them more reasonable values.
I'm building a web app that needs to reference data from a single view.
I know that I can't have a from clause in a sub-query in create or replace view but I was only able to come up with one version of this query that returns the result I need.
After hours of trawling through posts on here I couldn't come up with any solutions for it.
Is there someone with more smarts than I have that can re-fudge this query in such a way that all the data can be returned in a single view?
Here is the query ...
CREATE
OR REPLACE VIEW in_stock_levels AS
SELECT
*,
`in_stock` - `maximum_stock` AS levels
FROM(
SELECT
stock_levels.id AS id,
stock_levels.part_number AS part_number,
stock_levels.minimum_stock AS minimum_stock,
stock_levels.maximum_stock AS maximum_stock,
(
SELECT
COUNT(*)
FROM
automatic_transmission_jobs
WHERE
part_number = stock_levels.part_number
AND test_result != "Not Tested"
AND status != "pwa"
) AS in_stock
FROM
stock_levels
) AS check_stock
Any help is much appreciated.
Without having more information on your schema, but based on what you posted, the following query should work for your CREATE OR REPLACE VIEW...
SELECT
stock_levels.id,
stock_levels.part_number,
stock_levels.minimum_stock,
stock_levels.maximum_stock,
COUNT(automatic_transmission_jobs.id) AS in_stock,
in_stock - maximum_stock AS levels
FROM stock_levels
LEFT OUTER JOIN automatic_transmission_jobs USING (part_number)
WHERE
automatic_trasmission_jobs.test_result != 'Not Tested'
AND automatic_trasmission_jobs.status !='pwa'
GROUP BY stock_levels.id;
Do you use phpMyAdmin or MySQLWorkbench to design and test your queries?
Doing it this way works, I know it's not the most efficient,
CREATE
OR REPLACE VIEW in_stock_levels AS
SELECT
stock_levels.id AS id,
stock_levels.part_number AS part_number,
stock_levels.minimum_stock AS minimum_stock,
stock_levels.maximum_stock AS maximum_stock,
(
SELECT
COUNT(*)
FROM
automatic_transmission_jobs
WHERE
part_number = stock_levels.part_number
AND test_result != "Not Tested"
AND status != "pwa"
) AS in_stock,
(
SELECT
COUNT(*)
FROM
automatic_transmission_jobs
WHERE
part_number = stock_levels.part_number
AND test_result != "Not Tested"
AND status != "pwa"
) - stock_levels.maximum_stock AS stock_level
FROM
stock_levels
Thanks for all your help Ingo :)
Currently, I am trying to select a list of posts from people who are friends and/or are being followed by the current user. However, I am running into an issue when I add in the second statement for the following table.
SELECT * FROM login, threads WHERE login.id=threads.poster_id AND deleted=0 AND login.id
IN ((SELECT CASE WHEN second_id=? THEN first_id ELSE second_id END FROM friends WHERE first_id=? OR second_id=?)
AND (SELECT CASE WHEN follower=? THEN following ELSE follower END FROM following WHERE follower=?))
ORDER BY threads.posted DESC LIMIT 20
Just use two explicit IN conditions with the two subqueries:
SELECT *
FROM login
INNER JOIN threads
ON login.id = threads.poster_id
WHERE
deleted = 0 AND
(login.id IN (SELECT CASE WHEN second_id=? THEN first_id ELSE second_id END
FROM friends WHERE first_id=? OR second_id=?) AND
login_id IN (SELECT CASE WHEN follower=? THEN following ELSE follower END
FROM following WHERE follower=?))
ORDER BY
threads.posted
DESC LIMIT 20;
This may fix the syntax errors and even give you the output you expect, but I also feel that we may be able to clean up the query a bit more. Sample data would go a long way towards that end.
I have a table like this:
ID Severity WorkItemSK
23636 3-Minor 695119
23636 3-Minor 697309
23647 2-Major 695081
23647 2-Major 694967
In here I have several WorkItems that share the same ID. How can I get unique IDs that have the highest WorkItem?
So it would like this:
ID Severity WorkItemSK
23636 3-Minor 697309
23647 2-Major 695081
Help the noob :) Mind giving a clue what SQL commands (again I am a noob) should I use? Or an example of a query?
Thank you in advance!
Assuming that Severity can change depending on the WorkItemSK, you'll want to use the following query:
Select T.ID, T.Severity, T.WorkItemSK
From Table T
Join
(
Select ID, Max(WorkItemSK) As WorkItemSK
From Table
Group By ID
) D On T.WorkItemSK = D.WorkItemSK And T.ID = D.ID
The last Join condition of T.ID = D.ID may or may not be needed, depending on whether WorkItemSK can appear multiple times in your table.
Otherwise, you can just use this:
Select ID, Severity, Max(WorkItemSK) As WorkItemSK
From Table
Group by ID, Severity
But if you have different Severity values per ID, you'll see duplicate IDs.
Use select with GROUP BY: SELECT id,MAX(WorkItemSK) FROM table GROUP BY id;
this is my query, and I already know the problem but I dont have an idea about how to solve it:
$queryString = "SELECT * FROM `users_list` "WHERE `user_id` like (SELECT `user_id` FROM `equipments_list` WHERE `equipment_mac` like '%$searchStringMacRevised%')"
So, this is the error sometimes I get:
Subquery returns more than 1 row
I see that the problem is that if the mac address is registered more than once, it will give me more than one user ID, and when we are going to select the information, I have too much user id to generate the table. Can you guys help me to see how can I solve this problem?
Since you are just comparing to the user id directly, you could use an IN clause, such as
SELECT * FROM users_list
WHERE user_id IN
(SELECT user_id FROM equipments_list
WHERE equipment_mac like '%$searchStringMacRevised%')
This would allow you to potentially compare to multiple user ids.
If we want only 1 user id, then you may need to use the LIMIT type of query suggested in other answers.
It means that your inner select is returning more than one row , it should exactly return 1 row in order to match record for outer query
$queryString = "SELECT * FROM `users_list` "WHERE `user_id` like (SELECT `user_id` FROM `equipments_list` WHERE `equipment_mac` like '%$searchStringMacRevised%' LIMIT 1)"