I have a table similar to this:
-----------------------------------------------------------------------
id | project_id | dateValue | textValue | key
-----------------------------------------------------------------------
1 | 1 | 2018-10-25 | NULL | closing_date
-----------------------------------------------------------------------
2 | 2 | 2018-10-26 | NULL | listing_date
-----------------------------------------------------------------------
3 | 3 | 2018-10-27 | NULL | closing_date
-----------------------------------------------------------------------
4 | 1 | NULL | Pending | contract_status
-----------------------------------------------------------------------
5 | 2 | NULL | Active | contract_status
-----------------------------------------------------------------------
6 | 3 | NULL | Pending acc. | contract_status
-----------------------------------------------------------------------
I cannot get this to return any results.. Any suggestions? I need it to return project_ids "1 & 3"
SELECT t.*
FROM Table t
WHERE ((t.dateValue BETWEEN '2018-10-24' AND '2018-10-27') AND (t.key = 'contract_status' AND t.textValue IN ('Pending','Pending acc.'))
GROUP BY t.project_id
I've tried this variation as well:
SELECT t.*
FROM Table t
WHERE ((t.dateValue BETWEEN '2018-10-24' AND '2018-10-27') AND (t.key IN ('contract_status', 'closing_date') AND t.textValue IN ('Pending','Pending acc.'))
GROUP BY t.project_id
Thanks!
Your DB schema is weird and goal is not clear.
Here is something you can use to understand why you are not getting any result:
http://sqlfiddle.com/#!9/701a65/3
SELECT t.project_id,
MAX(t.dateValue),
MAX(t.textValue)
FROM t
WHERE (t.dateValue BETWEEN '2018-10-24' AND '2018-10-27'
AND (t.key = 'closing_date'))
OR t.textValue IN ('Pending','Pending acc.')
GROUP BY t.project_id
But you should definitely redesign your DB and read more about relational databases.
Your main problem is your WHERE
clause.
WHERE ((t.dateValue BETWEEN '2018-10-24' AND '2018-10-27')
AND (t.key = 'contract_status'
AND t.textValue IN ('Pending','Pending acc.'))
You use AND
everywhere that means you want to find all records which has for example dateValue
AND textValue
- but in your raw data there is no such records at all. :-)
Since your dataset does not cover all the conditions in one single row. You may use conditional aggregation with Having
clause:
SELECT t.project_id,
SUM(IF(t.dateValue BETWEEN '2018-10-24'
AND '2018-10-27',
1, 0)) AS date_check,
SUM(IF(t.key = 'contract_status' AND
t.textValue IN ('Pending','Pending acc.'),
1, 0)) AS key_check
FROM Table t
GROUP BY t.project_id
HAVING date_check > 0 AND
key_check > 0
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.