issue while trying to insert one table into another one - mysql

I have a mysql table of 1.5 million record in a temproary table that I want to insert it into the main table with an inner join.
My code works but it stops at 103,613 records. IT does not keep going. Why would it stops? why it is not going all the way to the end?
This is my current code
INSERT INTO phone_calls(next_attempt, created_on, modified_on, status, call_subject,
account_number, call_code, last_attempt_on, total_attempts, account_id
,team_id
,campaign_id
,call_code_id
,result_code
,result_code_id
,time_zone_id
,trigger_on
,first_attempt_on
,first_attempt_by
,last_attempt_by
,modified_by
,client_id
,last_call_id
,call_notes
,owner_id
,industry_id
)
SELECT
CASE WHEN next_attempt IS NULL OR next_attempt = '' THEN STR_TO_DATE(replace(t.next_attempt,'/',','),'%m,%d,%Y %T') END as next_attempt,
CASE WHEN t.created_on IS NULL OR t.created_on = '' THEN '0000-00-00 00:00:00' ELSE STR_TO_DATE(replace(t.created_on,'/',','),'%m,%d,%Y %T') END as created_on,
CASE WHEN t.modified_on IS NULL OR t.modified_on = '' THEN '0000-00-00 00:00:00' ELSE STR_TO_DATE(replace(t.modified_on,'/',','),'%m,%d,%Y %T') END AS modified_on,
CONVERT( CASE WHEN t.status IS NULL OR t.status = '' THEN 0 ELSE t.status END, UNSIGNED INTEGER) AS status,
LEFT(IFNULL(t.call_subject, ''), 100),
t.account_number,
CONVERT( CASE WHEN t.callcode IS NULL OR t.callcode = '' THEN 0 ELSE t.callcode END , UNSIGNED INTEGER) AS callcode, STR_TO_DATE(replace(t.last_attempt_on,'/',','),'%m,%d,%Y %T') as last_attempt_on,
CONVERT( CASE WHEN t.New_Attempts IS NULL OR t.New_Attempts = '' THEN 0 ELSE t.New_Attempts END , UNSIGNED INTEGER) AS New_Attempts,
a.account_id
,0
,0
,0
,0
,0
,0
, '0000-00-00 00:00:00'
, '0000-00-00 00:00:00'
,1
,1
,1
,1
,0
,'IMPORTED FROM CRM'
,1
,1
FROM tmp_table_for_rdi_cms AS t
INNER JOIN accounts AS a ON a.account_number = t.account_number LIMIT 9999999999;
these 2 fields are indexed so it runs fast
a.account_number
t.account_number
Now, I know i am inserting no value for some fields that has a type if unsigned integer but that is okay as i will be updating this at later time.
How can i execute this INSERT INTO query without losing any record?

The "doesnt have a default value" problem is that the source table has nulls for columns on the new table that are defined S NOT NULL and which are not defined with a default value.
You have three choices to fix this:
Define the target columns as null able (leave out the NOT NULL)
Define the target columns with a default value, ie NOT NULL DEFAULT 'some value'
Provide a value if the column is null, ie SELECT ifnull(source_column, 'some value)
The Truncated incorrect INTEGER value problems and you are selecting from a char value and putting it in an int column, but some vyes are blank, so you need to handle that:
select if(source_column = '', 0, source_column)
The Data truncated for column problem is there because the source value is larger/longer than the target column can accommodate. To fix, define your target column to be larger (bigint instead of int) or longer (eg text or varchar(80) instead of varchar(20))

Related

oracle replace '0' with 'N/A'

I would like to replace data in column RANDOM that equal to '0', NULL, 'NONE', 'NA' WITH 'N/A'.
I used nested REPLACE as below:
SELECT IDNO,
REPLACE (
REPLACE (
REPLACE(
REPLACE(RANDOM, 'NONE|NA', 'N/A'),
'0','N/A'),
'-',''),
NULL, 'N/A') AS NUMBER
FROM TABLE1
It does replace data '0' with 'N/A'. However, it does replace other data that contain 0 in that too. Example: 04110 change to N/A411N/A
I want to replace the one that exactly have value '0' only.
SELECT IDNO,
REPLACE (
REPLACE (
REPLACE(
REPLACE(RANDOM, 'NONE|NA', 'N/A'),
'/0','N/A'),
'-',''),
NULL, 'N/A') AS NUMBER
FROM TABLE1
When I do this (add \ in front of 0) it does not change any of the 0 to 'N/A'
How do I replace data with the exact value '0' with 'N/A' then?
Thank you in advance. :)
... RANDOM that equal to '0', NULL, 'NONE', 'NA' WITH 'N/A'
"Equal" means that there's nothing else in that column. In that case, use CASE:
select idno,
case when random in ('0', 'NONE', 'NA') or radnom is NULL then 'N/A'
else random
end as result
from table1
Hint: Check out IF function of your sql server. Try this:
SELECT
IDNO,
IF(Random IS NULL or Random = '0' or Random='NONE' or Random = 'NA', 'N/A', Random)
FROM
TABLE1

Stored procedure WHERE dynamically checking if param NAME is null OR has value

What is the most efficient way to create a dynamic sp where im checking the param NAME either its null or has value.
my GOAL here is to select the specific name and their datas else just display all name with datas if param NAME is null or has no value submitted.
Here's my 2 solution:
option 1
IF (SELECT call_transactions.`called_name` IS NULL FROM call_transactions) THEN
SELECT a.`called_name` , DATE_FORMAT(a.`start_datetime`,"%m/%d/%Y %T") AS start_datetime , DATE_FORMAT(a.`end_datetime`,"%m/%d/%Y %T") AS end_datetime, SEC_TO_TIME(a.`duration`) AS duration
FROM call_transactions a
WHERE a.`user_id` = pUSERID AND a.duration <> 0 AND DATE_FORMAT(a.start_datetime , "%m/%d/%Y") BETWEEN pSTART AND pEND
ORDER BY a.start_datetime DESC;
ELSE
SELECT a.`called_name` , DATE_FORMAT(a.`start_datetime`,"%m/%d/%Y %T") AS start_datetime , DATE_FORMAT(a.`end_datetime`,"%m/%d/%Y %T") AS end_datetime, SEC_TO_TIME(a.`duration`) AS duration
FROM call_transactions a
WHERE a.`user_id` = pUSERID AND a.called_name = pNAME AND a.duration <> 0 AND DATE_FORMAT(a.start_datetime , "%m/%d/%Y") BETWEEN pSTART AND pEND
ORDER BY a.start_datetime DESC;
END IF;
option 2
SELECT a.`called_name` , DATE_FORMAT(a.`start_datetime`,"%m/%d/%Y %T") AS start_datetime , DATE_FORMAT(a.`end_datetime`,"%m/%d/%Y %T") AS end_datetime, SEC_TO_TIME(a.`duration`) AS duration
FROM call_transactions a
WHERE a.`user_id` = pUSERID AND (a.called_name = pNAME OR pNAME = '') AND a.duration <> 0 AND DATE_FORMAT(a.start_datetime , "%m/%d/%Y") BETWEEN pSTART AND pEND
ORDER BY a.start_datetime DESC;
Thank you in advance for the enlightenment guys. Just a curious kid here
Use your 2nd option, but test for the correct value.
WHERE a.`user_id` = pUSERID
AND (pNAME IS NULL OR a.called_name = pNAME)
AND a.duration <> 0 AND DATE_FORMAT(a.start_datetime , "%m/%d/%Y") BETWEEN pSTART AND pEND
In the second condition, note that I reversed the terms.
This is not strictly necessary, because the optimizer should prune away whichever condition is unnecessary, but this makes your short-circuit intentions clear.
Note also that NULL is not equivalent to empty string, and nothing is equal to NULL, so IS NULL should be used if NULL is what you are matching against.
I don't know what you're doing with DATE_FORMAT() here, but don't do that.
Use correct datetimes everywhere, or use STR_TO_DATE() to convert the parameters, not DATE_FORMAT() against the column. You can't compare mm/dd/yyyy "between" two values. That doesn't make sense. 12/09/2010 is "between" 12/08/2017 and 12/10/2017 because your code is comparing strings, lexcally, not comparing dates.
As a rule, you should never use a column as an argument to function in WHERE because this defeats any indexes on the column and forces a full scan.

Concatenating fields in select clause JOOQ

I'm working in a query using JOOQ and I'm trying to output a column as a
concatenation (space separated) of other fields extracted in the same query.
Getting into detail, with the next code I try to create a select statement with a column called fullAdress by grouping all the address lines contained in the address table. So, for each field, if it's not null or empty it will be concatenated to the result (actually no space is being added).
#Override
protected List<Field<?>> selectCustomFields() {
List<Field<?>> customSelect = new ArrayList<Field<?>>();
// Fields to use in the concatenation
Field<?> field1 = field("addr.AddressLine1"), field2 = field("addr.AddressLine2"),field3 = field("addr.AddressLine3"),
field4 = field("addr.AddressLine4"), field5 = field("addr.PostalCode"), field6 = field("addr.City"),
field7 = field("addr.State"), field8 = field("addr.County"), field9 = field("addr.Country");
// Create non null/empty conditions
Condition condLine1 = field1.isNotNull().and(field1.length().ne(0));
Condition condLine2 = field2.isNotNull().and(field2.length().ne(0));
Condition condLine3 = field3.isNotNull().and(field3.length().ne(0));
Condition condLine4 = field4.isNotNull().and(field4.length().ne(0));
Condition condLine5 = field5.isNotNull().and(field5.length().ne(0));
Condition condLine6 = field6.isNotNull().and(field6.length().ne(0));
Condition condLine7 = field7.isNotNull().and(field7.length().ne(0));
Condition condLine8 = field8.isNotNull().and(field8.length().ne(0));
Condition condLine9 = field9.isNotNull().and(field9.length().ne(0));
// Concat address lines when meets condition
customSelect.add(concat(DSL.when(condLine1, field1),
DSL.when(condLine2, field2),
DSL.when(condLine3, field3),
DSL.when(condLine4, field4),
DSL.when(condLine5, field5),
DSL.when(condLine6, field6),
DSL.when(condLine7, field7),
DSL.when(condLine8, field8),
DSL.when(condLine9, field9))
.as("fullAddress"));
return customSelect;
}
JOOQ will generate the next from the previous select statement, which is giving a null value and not concatenating the fields correctly.
select
concat(
cast(case when (
addr.AddressLine1 is not null
and char_length(cast(addr.AddressLine1 as char)) <> 0
) then addr.AddressLine1 end as char),
cast(case when (
addr.AddressLine2 is not null
and char_length(cast(addr.AddressLine2 as char)) <> 0
) then addr.AddressLine2 end as char),
cast(case when (
addr.AddressLine3 is not null
and char_length(cast(addr.AddressLine3 as char)) <> 0
) then addr.AddressLine3 end as char),
cast(case when (
addr.AddressLine4 is not null
and char_length(cast(addr.AddressLine4 as char)) <> 0
) then addr.AddressLine4 end as char),
cast(case when (
addr.PostalCode is not null
and char_length(cast(addr.PostalCode as char)) <> 0
) then addr.PostalCode end as char),
cast(case when (
addr.City is not null
and char_length(cast(addr.City as char)) <> 0
) then addr.City end as char),
cast(case when (
addr.State is not null
and char_length(cast(addr.State as char)) <> 0
) then addr.State end as char),
cast(case when (
addr.County is not null
and char_length(cast(addr.County as char)) <> 0
) then addr.County end as char),
cast(case when (
addr.Country is not null
and char_length(cast(addr.Country as char)) <> 0
) then addr.Country end as char)) as `fullAddress`
from Address as `addr`
....
My questions are,
how should I create my select statement correctly?
how can I best add the space separator?
is there any better alternative to JOOQ ( when = case ) condition clause?
how should I create my select statement correctly?
You forgot the CASE .. ELSE part, or otherwise() in jOOQ:
// Concat address lines when meets condition
customSelect.add(concat(DSL.when(condLine1, field1).otherwise(""),
DSL.when(condLine2, field2).otherwise(""),
DSL.when(condLine3, field3).otherwise(""),
DSL.when(condLine4, field4).otherwise(""),
DSL.when(condLine5, field5).otherwise(""),
DSL.when(condLine6, field6).otherwise(""),
DSL.when(condLine7, field7).otherwise(""),
DSL.when(condLine8, field8).otherwise(""),
DSL.when(condLine9, field9).otherwise(""))
.as("fullAddress"));
how can I best add the space separator?
If you want an additional space separator between your address parts, you could write:
// Concat address lines when meets condition
customSelect.add(concat(DSL.when(condLine1, field1.concat(" ")).otherwise(""),
DSL.when(condLine2, field2.concat(" ")).otherwise(""),
DSL.when(condLine3, field3.concat(" ")).otherwise(""),
DSL.when(condLine4, field4.concat(" ")).otherwise(""),
DSL.when(condLine5, field5.concat(" ")).otherwise(""),
DSL.when(condLine6, field6.concat(" ")).otherwise(""),
DSL.when(condLine7, field7.concat(" ")).otherwise(""),
DSL.when(condLine8, field8.concat(" ")).otherwise(""),
DSL.when(condLine9, field9.concat(" ")).otherwise("")).trim()
.as("fullAddress"));
is there any better alternative to JOOQ ( when = case ) condition clause?
I think the approach is sound. Of course, you probably shouldn't repeat all that logic all the time, but create a loop of the sort:
List<Field<String>> list = new ArrayList<>();
for (int i = 0; i < fields.size(); i++) {
list.add(DSL.when(conditions.get(i), (Field) fields.get(i)).otherwise(""));
}
customSelect.add(concat(list.toArray(new Field[0])).trim().as("fullAddress"));

Unintended behavior: Subtraction between null values results in '0'

When either one of the field is NULL, I want the returned value to be NULL as well. I have also tried reversing the logic: is not null. Still the same results.
MySQL code:
(case
when
((`creative_stg_sample_tracking_raw`.`total_samples_received` is not null)
and (`creative_stg_sample_tracking_raw`.`total_samples_forecasted` is not null))
then
(cast(`creative_stg_sample_tracking_raw`.`total_samples_received`
as signed) - cast(`creative_stg_sample_tracking_raw`.`total_samples_forecasted`
as signed))
else NULL
end) AS `received_forecasted_dif`
Screenshot:
Your code should be working, but you don't need the case. Whenever one of the values is NULL, the expression should be NULL:
(cast(`creative_stg_sample_tracking_raw`.`total_samples_received` as signed) -
cast(`creative_stg_sample_tracking_raw`.`total_samples_forecasted` as signed))
) AS `received_forecasted_dif`
I wonder if your problem is that the value is actually 'NULL' rather than NULL. That is, a string value rather than a real NULL. MySQL will treat the string as 0 in the arithmetic.
You can fix this by doing:
(case when `creative_stg_sample_tracking_raw`.`total_samples_received` <> 'NULL' and
`creative_stg_sample_tracking_raw`.`total_samples_forecasted` <> 'NULL'
then (cast(`creative_stg_sample_tracking_raw`.`total_samples_received` as signed) -
cast(`creative_stg_sample_tracking_raw`.`total_samples_forecasted` as signed))
)
else NULL
end) AS `received_forecasted_dif`

Select works, but can't update rows with a string of all spaces -- why?

This query
SELECT count(data_id)
FROM cdiac_data_AL
WHERE (data_id >= 1 and data_id <= 30437)
AND (TMIN_flags = '')
returns 844 records, but update query affects 0 records:
UPDATE cdiac_data_AL set TMIN_flags=' '
WHERE (data_id >= 1 and data_id <= 30437)
AND ( TMIN_flags = '' )
What am I missing?
TMIN_flags is:
char(3) ascii_general_ci, allow null, default NULL
It's because the CHAR data type does not save trailing spaces.
You should change TMIN_flags data type to binary