I am trying to add a FORMAT to my query, but when I run my query I get an error.
Here is my query:
SELECT CONCAT('€ ', COALESCE(SUM(r), 0) - COALESCE(SUM(l), 0)) as totaal
FROM trans
WHERE user_id = 1
AND k_id= 110
GROUP
BY user_id
LIMIT 0,1
I tried multiple statements:
(FORMAT(r)) and (FORMAT(l)) gives error
FORMAT(SUM(r), 0) - FORMAT(SUM(l), 0) gives wrong response
CONCAT(FORMAT('€ ', COALESCE(SUM(r), 0) - COALESCE(SUM(l), 0))) gives error
FORMAT(CONCAT('€ ', COALESCE(SUM(r), 0) - COALESCE(SUM(l), 0))) gives also error
Can someone help me with this statement?
format(X,D) takes a number as the first parameter and the decimal places as the second parameter so it should be put where the result is a number and provided with a number of decimal places:
CONCAT('€ ', FORMAT(COALESCE(SUM(r), 0) - COALESCE(SUM(l), 0),0))
Related
I'm using MySql V5.7. I have a table PatientAppointment. I need to GROUP BY CDRId which is a column in the same table. Here's the query:
SELECT AppointmentDateTime,
duration,
minutes,
#prev_month := BillingMonth BillingMonth,
#prev_total := total total
FROM (select AppointmentDateTime,
duration,
#cur_dur := ((case when duration like '% hour%' then substring_index(duration, ' hour', 1) * 60 else 0 end) +
(case when duration like '%min%' then substring_index(substring_index(duration, ' min', 1), ' ', -1) + 0 else 0 end)) as minutes,
CASE WHEN #year_month = date_format(AppointmentDateTime, '%Y-%m')
THEN #cum_sum := #cum_sum + #cur_dur
ELSE #cum_sum := #cur_dur
END total,
#year_month := date_format(AppointmentDateTime, '%Y-%m') BillingMonth
from PatientAppointment, (SELECT #year_month:='', #cum_sum:=0, #cur_dur:=0) variables
GROUP BY CDRId <------ ERROR
ORDER BY AppointmentDateTime) subquery,
(SELECT #prev_month:=0, #prev_total:=0) variable
ORDER BY AppointmentDateTime, total
Here is a working db<>fiddle.
Please help me. I want the entire result set to be grouped by CDRId
If we cannot use GROUP BY clause here, can we do it some logic changes here.
This is the type of grouping I want. See this:
I'm getting this error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server v...
Unfortunately, SQL has no way of "merging table cells" like you show in your screenshot. SQL is not like a spreadsheet.
You must fetch the rows and then work out in application code how you want to group them.
(You asked me on another thread to come here and give an answer.)
I have taken reference from the internet about one user-defined function to locate 'nth occurrence of a string to do the sort column name in the database. I am using MySQL 5.5 version, not the latest version. Here is my sample database link https://dbfiddle.uk/?rdbms=mysql_5.5&fiddle=bcb32a6b47d0d5b061fd401d0888bdc3
My problem is I want to sort column name in the database follow the prefix number, but I am using below the SQL query, it doesn't work.
select t.id,t.name
from
(
select t.*, cast((case when col1_col2_ref > 0
then
substring_index(modified_name,'-',1)
else
modified_name
end
) as unsigned) col1
, cast((case when col1_col2_ref > 0
and col3_ref > 0
then
substr(modified_name,(col1_col2_ref + 1),(col3_ref - (col1_col2_ref + 1)))
when col1_col2_ref > 0
then
substr(modified_name,(col1_col2_ref + 1))
end) as unsigned) col2
, cast((case when col3_ref > 0
and col4_ref > 0
then
substr(modified_name,(col3_ref + 1),(col4_ref - (col3_ref + 1)))
when col3_ref > 0
then
substr(modified_name,(col3_ref + 1))
end) as unsigned) col3
, cast((case when col4_ref > 0
then
substr(modified_name,(col4_ref + 1))
end) as unsigned) col4
from
(
select t.*,substring_index(name,' ',1) modified_name
,locate('-',name,1) col1_col2_ref
,locate('/',name,1) col3_ref
,locate('/',name,locate('/',name,1)+1) col4_ref
from filing_code_management t
) t
) t
order by col1,col2,col3,col4
It shows me below the result, it cannot sort properly.
Output 1
Actually I want the output sample like below:
Output 2
Output 3
This is before I can sort the column name link, https://dbfiddle.uk/?rdbms=mysql_5.5&fiddle=6b12a4d42359cb30f27a5bfb9d0c8210. After I am inserted into new data, it cannot work for me. Maybe an example in new data like this error (R)100-6-2-2 Mesyuarat Majlis Kerajaan Negeri (MMKN) JKK if I put () in front. Or in new data like this error 100-1-1 Penggubalan/Penyediaan/Pindaan Undang-Undang/Peraturan if I put / in between the word.
Hope someone can guide me to solve this problem. Thanks.
You should be able to adapt the following code to your needs (tested at your DB Fiddle!). I've used the file_name column instead of the name column to slightly simplify building the sort fields, as it seems the file name is always repeated in the first part of the name field anyway.
This would be quite a bit simpler using regular expression support, but I note that the version of MySQL you are using doesn't have this feature (I think it arrives in SQL 8.0, if I'm not mistaken).
SELECT id,
num_hyphens,
CAST(SUBSTRING_INDEX(CONCAT(file_name_adj,'-'), '-', 1) AS UNSIGNED) AS sort1,
CAST(CASE WHEN num_hyphens = 0
THEN '0'
ELSE SUBSTRING_INDEX(SUBSTRING_INDEX(file_name_adj,'-', 2), '-',-1)
END AS UNSIGNED) AS sort2,
CAST(CASE WHEN num_hyphens <= 1
THEN '0'
ELSE SUBSTRING_INDEX(SUBSTRING_INDEX(file_name_adj,'-', 3), '-',-1)
END AS UNSIGNED) AS sort3,
CAST(CASE WHEN num_hyphens <= 2
THEN '0'
ELSE SUBSTRING_INDEX(file_name_adj, '-', -1)
END AS UNSIGNED) AS sort4,
file_name,
name
FROM (
SELECT id, name, MID(file_name, instr(file_name, ')') + 1) AS file_name_adj, file_name,
LENGTH(file_name) - LENGTH(REPLACE(file_name, '-', '')) AS num_hyphens
FROM filing_code_management
) t1
ORDER BY sort1, sort2, sort3, sort4
I have a table app_form_data_audit_trail which I need to join to another table app_fd_listofcomponents.
-The foreign key needs first to be extracted from a column collating some other data before getting the 1st table joined with the 2nd one.
-Some records from the first table needs to be filtered out.
I made the following sql query but getting error. I created a sqlfiddle here for troubleshooting purpose..
Thanks for your help :-)
SELECT DATETIME
,username
,CASE
WHEN locate('operation', DATA) > 0
THEN substring_index(substring_index(DATA, '"operation":"', - 1), '"', 1)
ELSE 0
END AS operation
,CASE
WHEN locate('operation', DATA) > 0
THEN substring_index(substring_index(DATA, '"opdetails":"', - 1), '"', 1)
ELSE 0
END AS opdetails
,CASE
WHEN locate('operation', DATA) > 0
THEN substring_index(substring_index(DATA, '"componentName":"', - 1), '"', 1)
ELSE 0
END AS componentid
,CASE
WHEN locate('operation', DATA) > 0
THEN substring_index(substring_index(DATA, '"package":"', - 1), '"', 1)
ELSE 0
END AS package
FROM app_form_data_audit_trail
WHERE DATA LIKE "%Operation%"
INNER JOIN app_fd_listofcomponents ON app_form_data_audit_trail.componentid = app_fd_listofcomponents.id
FROM app_form_data_audit_trail
INNER JOIN app_fd_listofcomponents ON app_form_data_audit_trail.componentid = app_fd_listofcomponents.id
WHERE DATA LIKE "%Operation%"
Thanks to all, Finally I came to write the query as follows and it works !
SELECT
app_form_data_audit_trail.datetime,
app_form_data_audit_trail.username,
app_form_data_audit_trail.data,
app_fd_listofcomponents.c_component,
substring_index(substring_index(app_form_data_audit_trail.data,'"operation":"',-1),'"',1) AS operation,
substring_index(substring_index(app_form_data_audit_trail.data,'"opdetails":"',-1),'"',1) AS opdetails,
substring_index(substring_index(app_form_data_audit_trail.data,'"package":"',-1),'"',1) AS package
FROM app_form_data_audit_trail
JOIN app_fd_listofcomponents
ON app_form_data_audit_trail.data LIKE CONCAT ('%',app_fd_listofcomponents.id,'%')
I need a single query which gives two middle characters of even string and one middle character of odd string.
Currently i am using this code but it is giving error.
SELECT S_name, MID(S_name, LENGTH(S_name)/2,1) WHERE (LENGTH(S_name) %2) = 1 OR/AND SELECT S_Name, MID(S_name,LENGTH(S_name)/2,2) WHERE (LENGTH(S_name)%2)=0 FROM Student;
I have also tried this code but it is returning empty view/table.
SELECT S_name FROM Student WHERE ((LENGTH(S_name) %2) = 1 AND SUBSTRING(S_name, LENGTH(S_name)/2+1, 1)) OR ((LENGTH(S_name) %2) = 0 AND SUBSTRING(S_name, LENGTH(S_name)/2-1, 2))
Please Just take a look at it and point out my mistake.
You want to use case in the select clause:
select s_name,
(case when length(s_name) % 2 = 0 then substring(s_name, length(s_name)/2, 2)
else substring(s_name, 1 + length(s_name) / 2, 1)
end)
from student;
I am new to SQL and having difficulty getting information out of my database, perhaps someone can provide guidance. I have
Table: students
Columns:
name, notes, test_1_score, test_2_score, test_3_score, test_4_score, test_5_score,
test_6_score, test_7_score, test_8_score, test_9_score, test_10_score
I can get the below code to run without the points_achieved in the GROUP BY but that is what I want to sort on. Also, I could not get the calculation to work without the IsNull added.
SELECT
name,
notes,
SUM (IsNull (test_1_score, 0) + IsNull (test_2_score, 0) + IsNull (test_3_score, 0) + IsNull (test_4_score, 0) + IsNull (test_5_score, 0) +IsNull ( test_6_score, 0) + IsNull (test_7_score, 0) + IsNull (test_8_score, 0) + IsNull (test_9_score, 0) + IsNull (test_10_score, 0)) AS points_acheived
FROM students
GROUP BY
points_achieved, name, notes;
Ultimately, I would like a simple answer to show:
name points_achieved (add a count of # tests completed)…….notes
Any help is greatly appreciated, thank you.
Try this:
SELECT
name,
notes,
SUM (IsNull (test_1_score, 0) + IsNull (test_2_score, 0) + IsNull (test_3_score, 0) + IsNull (test_4_score, 0) + IsNull (test_5_score, 0) +IsNull ( test_6_score, 0) + IsNull (test_7_score, 0) + IsNull (test_8_score, 0) + IsNull (test_9_score, 0) + IsNull (test_10_score, 0)) AS points_acheived
FROM students
GROUP BY
points_achieved, name, notes
order by 3;
You can take advantage of the fact that various boolean operators in SQL have the value 1 or 0 for true or false.
So the expression test_1_score IS NOT NULL will have the value 1 if that column has a valid value.
Use this query:
SELECT name, notes,
SUM(IsNull(test_1_score,0) + /* etc */) AS points_achieved
SUM((test_1_score IS NOT NULL) + (test_2_score IS NOT NULL) + /*etc*/) AS tests_taken
FROM students
GROUP BY name, notes