Hope you guys can help as this has been bugging me a couple of days now. I'm trying to get the total number of unique rows from a table. For instance, the data in the table looks like this;
user_1 | user_2 | date_added | date_removed
--------|--------|---------------------|---------------------
1 | 2 | 2016-09-20 15:51:45 | 2016-09-24 09:15:32
1 | 3 | 2016-09-21 10:16:03 | 2016-09-29 00:46:44
6 | 1 | 2016-09-23 05:48:59 | 0000-00-00 00:00:00
1 | 3 | 2016-09-30 09:57:16 | 0000-00-00 00:00:00
What I want to find is the total number of rows that a user_id appears in (either column user_1 or column user_2), where there is also only a single entry for that pairing of users (user pairings will always be in the same columns), and date_removed = 0000-00-00 00:00:00.
So if I search the table above for user_id '1', the total would be '1'. (Only 3rd line matches search critera);
where a search for user_id '2' would equal '0';
user_id of '3' would equal '0';
user_id of '6' would equal '1';
Hope that makes sense. Any help or pointers will be much appreciated.
---- EDIT ----
This is the query i have so far, i've tried several ways inc joins etc, but this has got me closest so far, and its not the cleanest way im sure!
SELECT COUNT(*) AS `count`
FROM `table`
WHERE (`user_1` = '1' OR `user_2` = '1')
GROUP BY `user_1`, `user_2`
HAVING `count` < '2' AND MAX(`date_removed`) < '0000-00-00 00:00:01'
However, when as more data appears in the table, the result of the query looks like this:
count
-----
1
1
1
Where I want it to show:
count
-----
3
There's more than one way to achieve this.
I'm sending an option, creating a view to make it easier, but if you don't want to create a view, you can use its code in a subquery.
Create a view to union all possible combinations of user1, user2
create view result as
select user1, user2, date_removed from test
union all
select user2, user1, date_removed from test
Create a query
select r.user1, r.user2
from result r
where
r.date_removed = '0000-00-00 00:00:00'
and r.user2 not in (select r2.user2
from result r2
where r2.date_removed <> '0000-00-00 00:00:00')
Hope it helps you to solve it.
Related
I have a mysql table like this
+----------+----------+-------------------+
| entrykey | user_key | validfrom |
+----------+----------+-------------------+
| 1 | 3 | 2016-4-1 0:0:0.0 |
| 2 | 3 | 2016-12-1 0:0:0.0 |
| 3 | 2 | 2016-12-1 0:0:0.0 |
| 4 | 2 | 2016-3-1 0:0:0.0 |
+----------+----------+-------------------+
now I am trying to get only the row for each user where the validfrom is the newest. So I am doing a query like this:
SELECT entrykey, user_key, max(validfrom)
FROM table
Group by user_key;
It is working fine for almost all of my data, just this two examples I posted here in the table select the wrong row which is older. So for user_key 3 it selects entrykey 1 and for the user_key 2 it selects entrykey 4.
What am I doing wrong?
I guess the validform should be 2016-03-01 instead of 2016-3-01 because it is not converted into date before compare.
I am totally agree with the point made in the accepted answer (+1 for that). But, even if op somehow convert validfrom from string to DateTime, his attempt won't give him the desired result.
Let's examine the query given in question:
SELECT entrykey, user_key, MAX(STR_TO_DATE(validfrom, '%Y-%c-%e')) as date1
FROM table
Group by user_key;
Now, this query will return user_key with maximum value of validfrom for that particular user_key. But the entrykey won't be the entrykey with max validfrom. (Check the first result in demo link)
In order to achieve above task, following query will work just fine!
SELECT t1.entrykey, t1.user_key, t2.maxdate as MaxDate
FROM t t1
inner join (select user_key,MAX(STR_TO_DATE(validfrom, '%Y-%c-%e')) as maxdate
from t
Group by user_key
) t2
on t1.user_key = t2.user_key and t1.validfrom = t2.maxdate;
Click here for Demo with DateTime as datatype of validfrom
Click here for Demo with STR_TO_DATE() function
Hope it helps!
I was searching for querys but i cant find an answer that helps me or if exit a similar question.
i need to get the info of the customers that made their last purchase between two dates
+--------+------------+------------+
| client | amt | date |
+--------+------------+------------+
| 1 | 2440.9100 | 2014-02-05 |
| 1 | 21640.4600 | 2014-03-11 |
| 2 | 6782.5000 | 2014-03-12 |
| 2 | 1324.6600 | 2014-05-28 |
+--------+------------+------------+
for example if i want to know all the cust who make the last purchase between
2014-02-11 and 2014-03-16, in that case the result must be
+--------+------------+------------+
| client | amt | date |
+--------+------------+------------+
| 1 | 21640.4600 | 2014-03-11 |
+--------+------------+------------+
cant be the client number 2 cause have a purchease on 2014-05-28,
i try to make a
SELECT MAX(date)
FROM table
GROUP BY client
but that only get the max of all dates,
i dont know if exist a function or something that can help, thanks.
well i dont know how to mark this question as resolved but this work for me
to complete the original query
SELECT client, MAX(date)
FROM table
GROUP BY client
HAVING MAX(date) BETWEEN date1 AND date2
thanks to all that took a minute to help me with my problem,
special thanks to Ollie Jones and Peter Pei Guo
Something in this format, replace date1 and date 2 with the real values.
SELECT client, max(date)
from table
group by client
having max(date) between date1 AND date2
There is more than one way to do this. Here is one of them.
select * from
(
select client, max(date) maxdate
from table
group by client ) temp
where maxdate between '2014-02-11' and '2014-03-06'
This will allow you to grab the amount column of the applicable rows as well:
select t.*
from tbl t
join (select client, max(date) as last_date
from tbl
group by client
having max(date) between date1 and date2) v
on t.client = v.client
and t.date = v.last_date
I had to change the field "Date" to "TheDate" since date is a reserved word. I assume you are using SQL? My table name is Table1. You need to group records:
SELECT Table1.Client, Sum(Table1.Amt) AS SumOfAmt, Table1.TheDate
FROM Table1
GROUP BY Table1.Client, Table1.TheDate
HAVING (((Table1.TheDate) Between #2/11/2014# And #3/16/2014#));
Query Results:
Client SumOfAmt TheDate
1 21640 03/11/14
2 6792 03/12/14
You may want to get yourself a copy of MS Access. You can generate SQL statements using their query builder which I used to generate this SQL. When I make a post here I will always test it first to make sure it works! I have never written even 1 line of SQL code, but have executed thousands of them from within MS Access.
Good luck,
Dan
Good day,
I have a MySQL table which has some duplicate rows that have to be removed while adding a value from one column in the duplicated rows to the original.
The problem was caused when another column had the wrong values and that is now fixed but it left the balances split among different rows which have to be added together. The newer rows that were added must then be removed.
In this example, the userid column determines if they are duplicates (or triplicates). userid 6 is duplicated and userid 3 is triplicated.
As an example for userid 3 it has to add up all balances from rows 3, 11 and 13 and has to put that total into row 3 and then remove rows 11 and 13. The balance columns of both of those have to be added together into the original, lower ID row and the newer, higher ID rows must be removed.
ID | balance | userid
---------------------
1 | 10 | 1
2 | 15 | 2
3 | 300 | 3
4 | 80 | 4
5 | 0 | 5
6 | 65 | 6
7 | 178 | 7
8 | 201 | 8
9 | 92 | 9
10 | 0 | 10
11 | 140 | 3
12 | 46 | 6
13 | 30 | 3
I hope that is clear enough and that I have provided enough info. Thanks =)
Two steps.
1. Update:
UPDATE
tableX AS t
JOIN
( SELECT userid
, MIN(id) AS min_id
, SUM(balance) AS sum_balance
FROM tableX
GROUP BY userid
) AS c
ON t.userid = c.userid
SET
t.balance = CASE WHEN t.id = c.min_id
THEN c.sum_balance
ELSE 0
END ;
2. Remove the extra rows:
DELETE t
FROM
tableX AS t
JOIN
( SELECT userid
, MIN(id) AS min_id
FROM tableX
GROUP BY userid
) AS c
ON t.userid = c.userid
AND t.id > c.min_id
WHERE
t.balance = 0 ;
Once you have this solved, it would be good to add a UNIQUE constraint on userid as it seems you want to be storing the balance for each user here. That will avoid any duplicates in the future. You could also remove the (useless?) id column.
SELECT SUM(balance)
FROM your_table
GROUP BY userid
Should work, but the comment saying fix the table is really the best approach.
You can create a table with the same structure and transfer the data to it with this query
insert into newPriceTable(id, userid, balance)
select u.id, p.userid, sum(balance) as summation
from price p
join (
select userid, min(id) as id from price group by userid
) u ON p.userid = u.userid
group by p.userid
Play around the query here: http://sqlfiddle.com/#!2/4bb58/2
Work is mainly done in MSSQL but you should be able to convert the syntax.
Using a GROUP BY UserID you can SUM() the Balance, join that back to your main table to update the balance across all the duplicates. Finally you can use RANK() to order your duplicate Userids and preserve only the earliest values.
I'd select all this into a new table and if it looks good, deprecate your old table and rename then new one.
http://sqlfiddle.com/#!3/068ee/2
table: chat_thread
+-----------+----------+--------------------+
| id | title | date |
+-----------+----------+--------------------+
| 1 | Thread 1 | 2012-02-16 01:12:40|
| 2 | Thread 2 | 2012-02-17 02:32:44|
+-----------+----------+--------------------+
table: chat_comment
+-----------+----------+--------------------+
| id | t_id | date |
+-----------+----------+--------------------+
| 1 | 1 | 2012-02-19 19:45:32|
| 2 | 1 | 2012-02-15 22:29:20|
+-----------+----------+--------------------+
Here's my situation, I creating a basic forum with threads and comments. I want to order by the last comment date, and if there is no comments, then the thread start date. The problem I'm having is trying to echo out the last reply date in my while loop.
mysql_query("
SELECT *,
chat_comment.date AS commentDate,
chat_thread.date AS threadDate
FROM chat_thread
LEFT JOIN chat_comment ON chat_thread.id = chat_comment.t_id
GROUP BY chat_thread.id
ORDER BY commentDate DESC, threadDate DESC");
My issue isn't the correct order of the threads, but the comment dates. In my tables above, if I'm trying to echo out the date, I'm currently getting 2012-02-15 22:29:20 instead of the more recent 2012-02-19 19:45:32.
Is there something I'm doing wrong?
I think it has something to do with the GROUP BY, because if I change it to GROUP BY chat_comment.date then the dates in the while loop are accurate, but there are duplicates, because I am not grouping by the chat_thread.id
Instead of selecting the last chat_comment.date, you are selecting an arbitrary one. (See MySQL Reference Manual, ยง11.6.3: GROUP BY and HAVING with Hidden Columns for a detailed explanation of this.) You need to use MAX(chat_comment.date) instead of just chat_comment.date.
You have to use MAX() if you want a maximum value, and then you have to GROUP by all the other columns, like so:
mysql_query("
SELECT chat_thread.id, chat_thread.title, chat_thread.date,
MAX(chat_comment.date) AS commentDate
FROM chat_thread
LEFT JOIN chat_comment ON chat_thread.id = chat_comment.t_id
GROUP BY chat_thread.id, chat_thread.title, chat_thread.date
ORDER BY MAX(chat_comment.date) DESC, chat_thread.date DESC");
today i need your help to get an specific sql select query.
i have following table:
and after a specific query regarding a specific id (in this case id 1) i wanna have a result like this:
user_id (an alias for the id_sender/id_recipient), date (maybe a max function, cause i wanna have the latest date to group), messages (a count function to the messages):
10 | 2012-01-14 09:10:05 | 4
11 | 2012-01-13 13:52:49 | 1
13 | 2012-01-13 20:01:17 | 1
14 | 2012-01-14 09:20:17 | 1
i tryed a lot but dont get the exact results - so my approach was something like this:
SELECT `id_recipient`, `id_sender`, MAX(`date`) AS `date`, COUNT(*) AS `messages` FROM `table` WHERE `id_recipient` = 1 OR `id_sender` = 1 GROUP BY `id_recipient`, `id_sender`
but then i get this result:
its not so bad but as u can see the 4th line should be included in the results of the first one.
i hope u got me. feel free to ask if smth is not clear.
thanks in advance,
greetings
Ok, so since we know the value for id_recipient, we can use some math to trick SQL into getting this nasty query done.
Let n be the id value of the person of interest.
We know that the pairing of id_recipient and id_sender will ALWAYS include the user with id value n. Based on the where clause.
Therefore, id_recipient + id_sender == n + id_otherPerson is true.
The resulting query will be very similar to this.
(It's been a while, but I don't think I have any syntax problems)
SELECT (`id_recipient` + `id_sender` - n) AS `id_otherPerson`,
MAX(`date`) AS `date`, COUNT(*) AS `messages`
FROM `table`
WHERE `id_recipient` = n XOR `id_sender` = n
GROUP BY `id_otherPerson`;
Edit: I've changed it to an XOR, so if person n messages person n, it won't cause all values to be incremented by the number of times n has messaged themself.
What about this?
SELECT user_id, MAX(date), COUNT(*)
FROM (
SELECT id_recipient AS 'user_id', date
FROM table
WHERE id_recipient <> 1 AND id_sender = 1
UNION
SELECT id_sender AS 'user_id', date
FROM table
WHERE id_recipient = 1 AND id_sender <> 1
) AS tbl
GROUP BY user_id
It assumes you want to use id_recipient if the id_sender is 1 and id_sender if id_recipient is 1.
I believe the output you want should be as below
10 | 2012-01-13 20:01:17 | 3
11 | 2012-01-13 13:52:49 | 1
13 | 2012-01-13 20:01:17 | 1
I'm saying as you are mixing id_recipient and id_sender