I have customers on my DB with too many tickets, and these tickets have too many statuses. I want a query that could pull the cutomer's ticket number, only if the ticket with status!='Closed'. Otherwise it should return null.
Your fast response is highly appreciated.
Edit: Here is my query:
SELECT CASE WHEN custom_field_3 != 'Closed' THEN item_id ELSE NULL END AS ticketID
FROM aims_items
WHERE custom_field_22 =221226
It works now, and here is my solution:
SELECT
CASE WHEN
(
SELECT COUNT(*)
FROM aims_items
WHERE custom_field_3 != 'Closed'
AND custom_field_22 =221226
)>0
THEN
item_id
ELSE
NULL
END
AS ticketID
FROM aims_items
WHERE
custom_field_3 != 'Closed'
AND
custom_field_22 =221226
Thanks for your cooperation.
Try this query:
SELECT IF(custom_field_3!='Closed',item_id,'') as ticketID FROM `aims_items`
Remember, i have not written the exact fields that you have in your table. This is just example to show you. Refer to below link for detailed informationhttp://dev.mysql.com/doc/refman/5.0/en/control-flow-functions.html
Without table structure it is not possible to evaluate what the issue is. From your description a simple query like
SELECT item_id from aim_items WHERE custom_field_3 != 'Closed' AND custom_field_22 = 221226
Would suffice. You can then count the number of rows returned to determine result.
If, however, you require null to be returned as a result, try the following :
SELECT (SELECT item_id from aim_items WHERE custom_field_3 != 'Closed' AND custom_field_22 = 221226) AS ticketID
The above encapsulates the basic select into a subquery, to provide a null result.
Related
Gouging my brain out as I was not able to produce my desired output
I initially have this query
SELECT * FROM SilverShop_Order WHERE Code = "HBOPD" AND Status != "Unpaid"
But then,
I only want to display the records that has a NULL value for the MainReference column AND an additional condition that
if a record has more than 1 value of the same MainReference, I also want to include them
I have this improved query
SELECT * FROM SilverShop_Order WHERE Code = "HBOPD" AND Status != "Unpaid" AND Reference IN
(
SELECT Reference FROM SilverShop_Order WHERE MainReference IS NULL
)
Which was able to satisfy my 1st desired condition but my 2nd condition is that, I want to display the other records that has the same MainReference
With the picture above, naturally, 695891 will be included in the fetched data, but will not include 695891.01 because the condition must have more than 1 same MainReference
And with the example above, I'll include the 649490, 649490.01 AND 649490.02 since the MainReference was able to detect more than 1 same MainReference
Can anyone kindly help me out what should be my query please?
Thank you very much in advance!
I would try with the following query:
SELECT * FROM SilverShop_Order t1
LEFT JOIN Silvershop_Order t2 ON (t1.MainReference = t2.MainReference AND t1.ID != t2.ID)
WHERE t1.Code = "HBOPD" AND t1.Status != "Unpaid" AND
(t1.MainReference IS NULL OR t2.MainReference IS NOT NULL)
What's making this possible is that you're joining table SilverShop_Order with itself, via MainReference, but join clause also contains condition that ID of those joined records should be different. Which is kind of count, only without a count - if you got anything but NULL on t2.MainReference, it means that you have at least 2 records with same MainReference.
And with the example above, I'll include the 649490, 649490.01 AND 649490.02 since the MainReference was able to detect more than 1 same MainReference
Test this:
SELECT *
FROM table t1
JOIN ( SELECT COALESCE(MainReference, Reference) ref
FROM table t2
GROUP BY ref
HAVING COUNT(MainReference) > 1 ) t3 ON COALESCE(t1.MainReference, t1.Reference) = t3.ref
/*
UNION
SELECT *
FROM table
WHERE MainReference IS NULL
*/
I have this table.
In MySql I want select this user IF key column do not have custom row AND IF key column have custom row -> value column = yes.
This is my query, but it do not work:
where `value` = if(`key` = 'custom', 'yes', '')
you can use the following statement which select any row has key not equal 'custom' or equal 'custom' and value equal 'yes'
Select * from tableName where `key`<> 'custom' or (`key` = 'custom' and `value` = 'yes')
The specification for the query isn't clear, in that it's open to several different interpretations.
I'm thinking the question is about how to return a particular result, not really about using an IF() function in the WHERE clause. (None of the SQL below uses an IF() function in a WHERE clause.)
Here's one possible interpretation of the specification:
By "select this user", we'll take that to mean we want to return a single copy of the user_id column.
By "IF key column do not have custom row", we'll take that to mean that there does not exist such a row for this user_id, then return this user_id. If such a row does exist, then do not return the user.
By "AND IF key column have custom row -> value column = yes.", we'll take that to mean to return this user_id if there is a row for this user_id that matches the condition.
Based on that interpretation, here's an approach to satisfying the specification using conditional aggregation:
SELECT t.user_id
FROM this_table t
GROUP
BY t.user_id
HAVING SUM(1) = SUM(IF(t.key='custom',IF(t.value='yes',1,0),1))
The expression inside the SUM() aggregate gets evaluated for each row. That expression is designed to return either a 1 or a 0, depending on whether the row matches the specified conditions. The SUM() aggregate totals up those 1s and 0s. And then we compare the result to the total number of rows. That tells us if a "matching" row exists or not.
The counts are by user_id, as identified in the GROUP BY clause.
The MySQL IF() function can be replace with a more portable ANSI CASE expression
HAVING SUM(1) = SUM(CASE
WHEN t.key = 'custom'
THEN CASE
WHEN t.value = 'yes'
THEN 1
ELSE 0
END
ELSE 1
END
)
If the intent is to return all of the detail rows for a user, but excluding all user_id that meet the specification, we could use an EXISTS correlated subquery:
SELECT t.user_id
, t.key
, t.value
FROM this_table t
WHERE NOT EXISTS ( SELECT 1
FROM this_table r
WHERE r.user_id = t.user_id
AND r.key = 'custom'
)
OR EXISTS ( SELECT 1
FROM this_table q
WHERE q.user_id = t.user_id
AND q.key = 'custom'
AND q.value = 'yes'
)
Here's the simple query:
SELECT status FROM tbl_pedidos_produtos WHERE status = 4;
This, obviously, brings me only the entries whose status equals to 4, but in this manner I can't test if ALL entries have status 4. How can I do something like this?
SELECT status FROM tbl_pedidos_produtos WHERE status OF ALL = 4;
Simply, just get COUNT(*) of the rows those doesn't have status = 4
SELECT COUNT(*) FROM tbl_pedidos_produtos WHERE status != 4;
If it's greater than 0, that means you have at least one row which has status != 4.
You can do this using aggregation. Here is one way:
select (max(status) = 4 and min(status) = 4 and status is not null) as AllSameFlag
from tbl_pedidos_produtos;
Another way that might be a bit less obvious:
select (count(*) = 0) as AllSameFlag
from tbl_pedidos_produtos
where status <> 4 or status is null
If you don't have null values in there, you can do that in a single query by selecting all distinct status values, counting them, then make sure that adds up to 1. Like this:
SELECT (SELECT COUNT(DISTINCT `status`) from `tbl_pedidos_produtos`) = 1;
If you do have null values (i.e., a product without a status value for some reason), then you'll need to filter them out first.
I have a phpmyadmin console Running a voting database where users vote for person1 or person2. the entries then get put into a table with userID, and choiceID. user column is auto incrementing and choiceID is either 1 or 2 depending on what they voted for. this is all new to me and I am surprised I have made it this far. could anybody explain how to make a query that counts how many 1,s , and how many 2,s are inside my whole table at any given time? that would be very awesome.
SELECT choiceID, COUNT(userID) as number_of_votes FROM votes GROUP BY choiceID ORDER BY number_of_votes
Given a set of data like this:
Will result in this:
Here the SQL-Command. You just have to replace the tablename.
SELECT COUNT(*), choiceID FROM tablename GROUP BY choiceID;
You can use "CASE WHEN" clauses in your query:
SELECT
SUM( CASE WHEN choiceID = 1 THEN 1 ELSE 0 END ) AS choice_1,
SUM( CASE WHEN choiceID = 2 THEN 1 ELSE 0 END ) AS choice_2
FROM ...
WHERE ...
select count(*) from -tablename- where choiceID = '1'
or '2'...depending what you want :P
I have two queries:
SELECT opr, COUNT(*) FROM table WHERE field = 'YES' GROUP BY opr
SELECT opr, MAX(category) FROM table WHERE field = 'NO' GROUP BY opr
So basically in the first query I am getting the number of transactions a user makes.
In the second query I am getting a category that all of those transactions fall under. I don't want to get all categories for each transaction they made, just the Max of the category field so that I have one entry per operator.
Until now, I have been catching both result sets in separate arrays and the looping through both arrays to get the complete opr->picks->category.
This doesn't always work it sometimes associates the wrong category to the wrong operator.
Is there a way to combine these two queries into one, so that I get the operator and picks, then the MAX(category)? The issue is that the conditions for each query are different on the field column.
SELECT opr,
COUNT(CASE WHEN field = 'YES' THEN 1 END) AS number_of_transactions,
MAX(CASE WHEN field = 'NO' THEN category END) AS category
FROM table
GROUP BY opr ;
The above logic can be attained in one query by using CASE
SELECT opr, COUNT(CASE WHEN `field` = 'YES' THEN opr END) as `count`,
MAX(CASE WHEN `field` = 'NO' THEN category END) as `max`
FROM table GROUP BY opr
Just add it in:
SELECT opr,count(*), MAX(category) FROM table WHERE field = 'YES' GROUP BY opr
You might also be able to drop it from 2 queries to 1 with this:
SELECT opr,count(*), MAX(category), field FROM table GROUP BY opr, field
even faster on the processor, use a sum bool:
select
opr,
sum(field='yes') as transactions,
max(if(field='no',category, null)) as category
from table
group by opr