MySql Error "Unknown column 'inDate' in 'where clause'" - mysql

I have been trying to run this stored procedure in MySql. But its throwing an Error:
"Unknown column 'inDate' in 'where clause"
CREATE DEFINER=`root`#`localhost` PROCEDURE `Cus_Emails`(inDate varchar(10))
truncate table tt;
insert tt(CompanyName, CustomerNumber, ServicePoint, EmailAddressSequenc, EmailAddress,
CustomerBusinessUnit)
select distinct '01' CompanyName, s.CustomerNo CustomerNumber, if(d.LINE_DESC like '%/%', substring_index(d.LINE_DESC, '/', -1), '') ServicePoint, '01' EmailAddressSequenc, 'Invoice.inbox#healthscope.com.au' EmailAddress, s.ServicePoint CustomerBusinessUnit
from doc_details d join doc_refs r on d.REF_ID=r.REF_ID
Join service_points s On s.CustomerNo=Trim(substring_index(substring_index(d.LINE_DESC, '/', 1), ' ', -1))
where d.ENTRY_ID like concat(inDate, '%') and d.PAGE_NUM=1 And d.LINE_NUM in (1,2) And (d.LINE_DESC like '-%' Or d.LINE_DESC Like '0%') and d.doc_type='INV'
Union
select distinct '01' CompanyName, s.CustomerNo CustomerNumber, if(d.LINE_DESC like '%/%', substring_index(d.LINE_DESC, '/', -1), '') ServicePoint, '01' EmailAddressSequenc, 'Invoice.inbox#healthscope.com.au' EmailAddress, s.ServicePoint CustomerBusinessUnit
from doc_details d join doc_refs r on d.REF_ID=r.REF_ID
Join service_points s On s.CustomerNo=Trim(substring_index(substring_index(d.LINE_DESC, '/', 1), ' ', -1))
where d.ENTRY_ID like concat(inDate, '%') and d.PAGE_NUM=1 And d.LINE_NUM in (3) and d.doc_type='CRE';

Check the MySQL docs:
I'd guess that ....
routine_body:
Valid SQL procedure statement
means you have to enclose your statements (truncate, insert) in BEGIN END. Otherwise routine_body is only the truncate and the variable inDate is out of procedure scope.

Because, in your table, you don't have inDate column.
Check the column name again and correct it!

Related

SQL ignore parenthesis in name field for ORDER BY

I would like rows returned in a MySQL query to be sorted alphabetically by surname for which I have an SQL query like:
SELECT
id,
substring_index(name, ' ', -1) as surname
FROM
my_table
ORDER BY
surname asc
However, some names have parenthesis to denote some special circumstance such as: Laura Angel (retired)
How can I modify my SQL above to ignore the parenthesised text, to sort by surname alphabetically?
Try with nested replaces to remove the parentheses.
SELECT
id,
substring_index(name, ' ', -1) as surname
ORDER BY
REPLACE( REPLACE( surname , '(' , '') , ')' , '') ASC;
Test and modify according to you version of SQL.
Not tested.
You can use this solution:
SELECT id,
substring_index(rtrim(substring_index(name, '(', 1)), ' ', -1) as surname
FROM test.test
ORDER BY
surname asc;

Why is my OR operator not working in my WHERE clause?

I need to write a where statement that only returns names starting with A, B, C, or E. I have a WHERE clause with a less that D condition, and that seems to work fine. But my Equals E condition doesn't appear to be working.
SELECT vendor_name,
CONCAT(vendor_contact_last_name, ', ', vendor_contact_first_name) AS full_name
FROM vendors
WHERE vendor_contact_last_name < 'D'
OR vendor_contact_last_name = 'E'
ORDER BY vendor_contact_last_name, vendor_contact_first_name;
It is only returning names that start with A, B, and C.
You can use the following using LEFT:
SELECT vendor_name,
CONCAT(vendor_contact_last_name, ', ', vendor_contact_first_name) AS full_name
FROM vendors
WHERE LEFT(vendor_contact_last_name, 1) IN ('A', 'B', 'C', 'E')
ORDER BY vendor_contact_last_name, vendor_contact_first_name;
... or using RLIKE:
SELECT vendor_name,
CONCAT(vendor_contact_last_name, ', ', vendor_contact_first_name) AS full_name
FROM vendors
WHERE vendor_contact_last_name RLIKE '^[A-CE]'
ORDER BY vendor_contact_last_name, vendor_contact_first_name;
demo on dbfiddle.uk

How do I insert multiple rows int MySQL where the values of one column are from a single list of values and the others are constant

I'm using MariaDB 10.4. I have a list of values, i.e. one#email.com, two#email.com, and three#email.com (my actual list is much longer).
I would like to make an SQL insert equivalent to the following:
insert into my_table(email, foreign_key_id, timestamp) values
('one#email.com', 1, now()),
('two#email.com', 1, now()),
('three#email.com', 1, now());
While only having to write something like select email from ('one#email.com', 'two#email.com', and 'three#email.com') somewhere in the insert query without duplicating the constants/functions on each line. Can I do this in SQL without any temporary tables?
INSERT INTO my_table (email)
SELECT email
FROM JSON_TABLE( #value,
"$[*]" COLUMNS ( email VARCHAR(32) PATH "$"
)
) AS parse_JSON;
fiddle
Applicable to MySQL 8+
For MariaDB 10.2.3+ use something close to (online fiddle issues incomprehensible errors where there are clearly no errors - so I cannot test)
INSERT INTO my_table (email)
WITH RECURSIVE
cte AS ( SELECT 0 AS num, JSON_VALUE(#json, '$[0]') AS email
UNION
SELECT num + 1, JSON_VALUE(#json, CONCAT('$[', num + 1, ']'))
FROM cte
WHERE JSON_VALUE(#json, CONCAT('$[', num + 1, ']') IS NOT NULL
)
SELECT email
FROM cte;
You can use select:
insert into my_table(email, foreign_key_id, timestamp) values
select e.email, 1, now())
from (select 'one#email.com' as email union all
select 'two#email.com' union all
select 'three#email.com'
) e

How can I use a MySQL stored procedure to count and sort a pipe-delimited field?

I need to create a stored procedure that will return the total count for each unique value encountered in a pipe-delimited field, roughly in the format in figure 2. Fortunately, the values will only ever be "value1", "value2", or "value3" if that makes things any simpler. I had originally planned to output the data and sort it in PHP by exploding the data and looping through for string matches, but there are extraordinary circumstances that require me to use a stored procedure and I've not used stored procedures before.
The data I'm interpreting is in the format shown below, where the 'valuelist' field is a pipe-delimited field containing many values. I need to find out how many times the values occurred each for a given date.
(figure 1 below) What the data looks like in the database.
id, date, valuelist
1, 2017-01-01, value1|value2|value3
2, 2017-01-01, value1|value2
3, 2017-01-01, value1
So a query of select date, valuelist from db.table where id = 1; will return 2017-01-01, value1|value2|value3.
(figure 2 below) Desired count output representing how many times each value occurred on a specific date. For example, using the data from figure 1, if we ask about 2017-01-01, the output should look something like this.
value1: 3
value2: 2
value3: 1
Assuming you are converting 3|2|1 to separate rows of 3, 2, 1; MySQL (or pretty much any SQL-based RDBMS) does not lend itself well to that kind of output.
Without going into actual procedure code, in a procedure you could:
Store value in a local variable
Use a more general purpose stored function (should be easy to find one around here) to "split" the string on your delimiter |; or "loop" through your string using a "get n-th" function to split it iteratively.
Store values into a temp table
Use a SELECT on the temp table as your procedure's resultset.
Take a look at this:
Detailed as given sample data:
SELECT date,fval as allValue,count(fval) as ValueCount from
(select date,fval from
(select date,SUBSTRING_INDEX(rplace, ' ', 1)fval,SUBSTRING_INDEX(MID(rplace,8,LENGTH(rplace)), ' ', 1)sval,SUBSTRING_INDEX(MID(rplace,15,LENGTH(rplace)), ' ', 1)tval from
(select id, date, valuelist,REPLACE(valuelist,'|',' ') as rplace from
(select 1 as id, '2017-01-01' as date, 'value1|value2|value3' as valuelist union all
select 2, '2017-01-01', 'value1|value2' union all
select 3, '2017-01-01', 'value1') as a)as a) as a
union all
select date,sval from
(select date,SUBSTRING_INDEX(rplace, ' ', 1)fval,SUBSTRING_INDEX(MID(rplace,8,LENGTH(rplace)), ' ', 1)sval,SUBSTRING_INDEX(MID(rplace,15,LENGTH(rplace)), ' ', 1)tval from
(select id, date, valuelist,REPLACE(valuelist,'|',' ') as rplace from
(select 1 as id, '2017-01-01' as date, 'value1|value2|value3' as valuelist union all
select 2, '2017-01-01', 'value1|value2' union all
select 3, '2017-01-01', 'value1') as a)as a) as a
UNION all
select date,tval from
(select date,SUBSTRING_INDEX(rplace, ' ', 1)fval,SUBSTRING_INDEX(MID(rplace,8,LENGTH(rplace)), ' ', 1)sval,SUBSTRING_INDEX(MID(rplace,15,LENGTH(rplace)), ' ', 1)tval from
(select id, date, valuelist,REPLACE(valuelist,'|',' ') as rplace from
(select 1 as id, '2017-01-01' as date, 'value1|value2|value3' as valuelist union all
select 2, '2017-01-01', 'value1|value2' union all
select 3, '2017-01-01', 'value1') as a)as a) as a) as a
where fval !='' GROUP BY fval,date
final Query:
SELECT date,fval as allValue,count(fval) as ValueCount from
(select date,fval from
(select date,SUBSTRING_INDEX(rplace, ' ', 1)fval,SUBSTRING_INDEX(MID(rplace,8,LENGTH(rplace)), ' ', 1)sval,SUBSTRING_INDEX(MID(rplace,15,LENGTH(rplace)), ' ', 1)tval from
(select id, date, valuelist,REPLACE(valuelist,'|',' ') as rplace from
(select * from yourTable) as a)as a) as a
union all
select date,sval from
(select date,SUBSTRING_INDEX(rplace, ' ', 1)fval,SUBSTRING_INDEX(MID(rplace,8,LENGTH(rplace)), ' ', 1)sval,SUBSTRING_INDEX(MID(rplace,15,LENGTH(rplace)), ' ', 1)tval from
(select id, date, valuelist,REPLACE(valuelist,'|',' ') as rplace from
(select * from yourTable as a)as a) as a
UNION all
select date,tval from
(select date,SUBSTRING_INDEX(rplace, ' ', 1)fval,SUBSTRING_INDEX(MID(rplace,8,LENGTH(rplace)), ' ', 1)sval,SUBSTRING_INDEX(MID(rplace,15,LENGTH(rplace)), ' ', 1)tval from
(select id, date, valuelist,REPLACE(valuelist,'|',' ') as rplace from
(select * from yourTable) as a)as a) as a) as a
where fval !='' GROUP BY fval,date
Result:
2017-01-01 value1 3
2017-01-01 value2 2
2017-01-01 value3 1

Mysql group_concat with sums also inside

I have a table of different attributes I want to sum, and then group concatenate them into a JSON string to make it easier to send over network. Here's a simplified table:
t1
type amount
'atr1' 10
'atr2' 10
'atr1' 17
'atr3' 20
'atr3' 4
I tried something like
select concat('{',
group_concat(
(select concat('"', type, '":', sum(amount)) from t1 group by type)
),
'}')
but failed.
I want to end up with '{"atr1":27,"atr2":10,"atr3":24}'
Try this query -
SELECT CONCAT('{', GROUP_CONCAT(c1), '}') FROM (
SELECT CONCAT('"', type, '":', SUM(amount)) c1 FROM t1 GROUP BY type
) t
something like
select
group_concat(concat('"', type, '":', TheSum))
FROM
(
SELECT SUM(amount) AS TheSum,type
FROM t1
GROUP BY type
) T