MYSQL Column addition based on a condition in another Column - mysql

I am trying to add a column to the query o/p based on a condition in another column. There is a column for Enrollment_Status, if the value is 'Cancelled' or' Dis enrolled', then the new column should have a concatenated value.
I have written a query for this , but the o/p is not correct. I have tried to write a CASE WHEN query, but it is giving me an error that - "Sub-query is returning multiple rows". I am currently using an 'IF' condition for the same.
The name of the table is 'sbi' in the database.
The o/p should be the concatenation of multiple columns and one of them should have a minimum value which is a date field.
IF((sbi.enrollment_status='Cancelled' OR sbi.enrollment_status='Disenrolled'), CONCAT(HCID, enrollment_status, created_at) IN (SELECT
CONCAT(hcid, enrollment_status, MIN(created_at))
FROM
sbi
WHERE sbi.enrollment_status='Cancelled' OR sbi.enrollment_status='Disenrolled'
Group By hcid),
'--') AS 'Disenroll Date'
The expected o/p is '000M99920Cancelled2019-08-28 00:00:00'
But currently the query is generating a '0' for the same.
Attached is the image for the o/p that I have received

Related

mysql condition 'equal' shows condition 'like' attributes

select * from order_info where order_id = 48;
The mysql command show above is a very simple sentence, but the result below seems not to meet my expectation, it is more similar to like condition.
retrieval result
I don't know if the type of field may affect the result as the field 'order_id' is varchar type. When I change the condition to " where order_id = '48' ", It meets my expectation.
Can anyone tell me why is this case? Why conditon 'equal' show unexpected outcomes?
I expect the result should be empty as the table does not have the record whose order_id is equal to 48.
Consider this -
SELECT CAST('48KKKK' AS INT);
You might expect it to fail , it doesn't and the result is 48. Your query is implicitly converting order id before comparison because you are comparing to an integer so the result of your query is correct and it's a gotcha to remember..

Subquery returns more than 1 row. Using SQL select to update a different tables results

I am trying to update a column in the following table 'jobqueue' using the results from a SELECT query performed on the 'mdtinfo' table.
The column I am trying to update is called ignore and I need to set the value to 1 from its default of 0.
update jobqueue
set jobqueue.`ignore`= '1'
where (SELECT JobQueue_job_queue_id
FROM mdtinfo
WHERE product_name = 'Example')
The above query returns the following error: SQL Error (1242): Subquery returns more than 1 row.
When running the select query on it's own it returns results successfully.
In MySQL, a value of zero appearing in a WHERE clause means false.
So, UPDATE something SET col=val WHERE (SELECT colx FROM sometable) has the potential to be a valid query. If the inner SELECT gets just one row, and its colx column has the value 0, the update won't do anything. If the colx column has a nonzero value the query means UPDATE something SET col=val WHERE true. Accordingly, every row in sometable will be updated. I doubt that's what you want.
If the inner SELECT happens to return more than one row, the query isn't valid. You'll get the error 1242 you actually received.
(This business of interpreting numbers as Boolean values causes MySQL to accept some otherwise dodgy query syntax, like the syntax in your question.)
I guess you want to retrieve the job_queue_id values for the row or rows you actually want to update. So try something like this.
update jobqueue
set jobqueue.`ignore`= '1'
where jobqueue.job_queue_id IN (SELECT JobQueue_job_queue_id
FROM mdtinfo
WHERE product_name = 'Example')
I guessed you have a column jobqueue.job_queue_id. You didn't tell us what columns you have in jobqueue.
update jobqueue
set jobqueue.`ignore`= '1'
where jobqueue.`job_queue_id` IN (SELECT GROUP_CONCAT(JobQueue_job_queue_id)
FROM mdtinfo
WHERE product_name = 'Example' GROUP BY product_name)
you should write column name in where condition.

Mysql Order by is not working with the value from if clause

I have the sql query with if condition and order by the if condition value, but order by is not working properly. And I have all my value column (creditamt,debitamt) in decimal data type.
Here is my code
select batchno,fperiod,date,reference,desp,"7.00" as taxpec,sequal,
if(creditamt-debitamt='0.00','0.00',((creditamt-debitamt)*-1)) as kdebitamt
from glpost where rem4='SR'and reference="INV-A00428"
and (fperiod between '1' and '11' AND gstperiod=0)
order by kdebitamt;
The outcome is
14.53
2467.94
270.00
3000.00
Expected Result is
14.53
270.00
2467.97
3000.00
Hope that somebody can help me out. Thanks for your help.
Edit : The Original Data Form
both of the debitamt, creditamt data types are decimal(10,2)
You are ordering by a string column; use the numeric column instead
select batchno,fperiod,date,reference,desp,"7.00" as taxpec,sequal,
if(creditamt-debitamt='0.00','0.00',(creditamt-debitamt)*-1 ) as kdebitamt
from glpost where rem4='SR'and reference="INV-A00428"
and (fperiod between '1' and '11' AND gstperiod=0)
order by (creditamt-debitamt)*-1;
Note, when the column alias kdebitamt is used by the ORDER BY clause, you have used strings '0.00' forcing it to treat that column (if referenced by that alias) as a string column. However the source column is numeric, so just refer to the source column name, not the alias.
alternative, use this instead and kdebitamt is then numeric:
if(creditamt-debitamt=0,0,(creditamt-debitamt)*-1 ) as kdebitamt

Extracting last value of unique filtered column

As a beginner in MySQL I'm having some difficulties building a query. I want to extract the values of the second column (Fecha) in my table for every unique value in the first one (CodigoEst). My final goal is to know the last/most recent value of "Fecha".
My table looks like
Then I want to have the values of "Fecha" for any different value of "CodigoEst".
I have tried DISTINCT but this gives the list of unique values in CodigoEst, not the values in Fecha. I have also tried
SELECT DISTINCT `CodigoEst`,`Fecha` FROM temperatura_max ORDER BY `Fecha` DESC LIMIT 1
But this gives the last value of "Fecha" just for one value of "CodigoEst". Expected output would be something like
CodigoEst Fecha
7031 2010-10-31
8460 2012-01-15
3610 2010-12-31
where the values in "Fecha" are the most recent dates
Any suggestion would be welcome, thanks
Group by CodigoEst and select max value
SELECT CodigoEst, MAX(fetcha) mostRecent FROM temperatura_max GROUP BY CodigoEst
Use the MAX() to get the most recent data:
SELECT MAX(ColName) FROM Table
Or you can use:
SELECT LAST_INSERT_ID(ColName) FROM Table

DLAST in Access

I am using DLAST to return a specific field value for the last record. The issue I am having is that the last record isn't always the newest date record. I need to return the value of a specific field for the newest date record.
You can't depend on DLast() to return a value from the "last record" of a table unless you use a query based on the table and indicate how the rows should be ordered. From the Application.DLast Method help topic ...
NoteIf you want to return the first or last record in a set of records (a domain), you should create a query sorted as either
ascending or descending and set the TopValues property to 1.
If you want to use DLast(), create a query and use the query name as the domain argument. For example, with this as Query1 ...
SELECT ASSY
FROM L2_AOI1
ORDER BY [your date field];
... this should work as the text box's Control Source ...
=DLast("ASSY", "Query1")
However, you could use a different query which returns the most recent ASSY and use DLookup with that query. For example, with Query2 ...
SELECT TOP 1 ASSY
FROM L2_AOI1
ORDER BY [your date field] DESC;
=DLookup("ASSY", "Query2")
Either way, include an index on [your date field] to optimize performance.
You can also use DLookup directly with an SQL clause:
=DLookup("Assy", "L2_AOI1", "[YourDateField] = (Select Max([YourDateField]) From L2_AOI1)")