2

I have the following table:

week status
------- ------
102014 1
102014 1
102014 1
112014 2
112014 2
112014 2
122014 2
122014 0
122014 0
132014 1
132014 1
132014 1
142014 2
142014 2
142014 2

How can I get 122014 which has different values on status?

asked Mar 24, 2015 at 19:59
1
  • select week from tablename group by week where min(status) < max(status); Commented Mar 25, 2015 at 0:36

2 Answers 2

3
SELECT week, GROUP_CONCAT(DISTINCT status)
 FROM tbl
 GROUP BY week
 HAVING COUNT(DISTINCT status) > 1;
answered Mar 25, 2015 at 18:17
0
0

I'm not entirely sure I understand your question but I'm assuming you want to select distinct values when week=122014. The keyword being DISTINCT. Here's what your query would look like assuming your table was called status_report:

mysql> select distinct(week),status from status_report where week=122014;
+--------+--------+
| week | status |
+--------+--------+
| 122014 | 2 |
| 122014 | 0 |
+--------+--------+
2 rows in set (0.00 sec)
answered Mar 24, 2015 at 20:36
2
  • The problem is that I do not know the week beforehand. If you remove the where condition in your query, I would also get the other week values. Commented Mar 24, 2015 at 20:38
  • Yes, removing the where condition would give you distinct rows for all possible combinations of (week, status). What is your use case? Maybe you could add that information (how you are using this query) to your question? Commented Mar 24, 2015 at 20:40

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.