I have the following code, if i run the code as an sql script it works but if i want to create a view from it i got this error: 1166 - Incorrect Column Name ''
select `c`.`package_id` AS `package_id`,
`c`.`student_id` AS `student_id`,
`bs`.`name` AS `stud_name`,
`c`.`payed_date` AS `payed_date`,
(case `c`.`type` when 'e' then 'Vizsgadíj' when 'c' then 'Tanfolyam díj' else '' end) AS `name`,
`c`.`course_price` AS `price`,
`c`.`pay_form` AS `pay_form`,
`c`.`venue_id` AS `venue_id`
from (`bma_student_pays` `c`
join `bma_students` `bs` on((`bs`.`id` = `c`.`student_id`)))
union all
select '0' AS `0`,'0' AS `0`,'' AS ``,`e`.`making_date` AS `making_date`,`e`.`name` AS `name`,`e`.`price` AS `price`,`e`.`type` AS `type`,`e`.`venue_id` AS `venue_id`
from `bma_extra_makings` `e`
union all
select '','','',i.inv_due,i.inv_name,i.inv_amount,i.`mode`,i.venue_id
from bma_invoices i
where i.inv_type='K'
Any help would really good.
Thanks!
In your second query (first UNION ALL), you have as your third column:
'' AS ``
This is not valid - remove the AS clause and all should be well.
Related
I have a problem with MySQL creating a counter variable SOrder based on a set order defined by order by. It was working in MySQL 5.1 but currently not in 5.7. Original query in 5.1 had the subquery order by and was acceptable to the standards back then. Under 5.7 with only_full_group_by mode off, I added the order by to the outside but even when the order by within the subquery was commented out (or ignored according to SQL standards), it output SOrder as if it's random but not based on Column Carrier, PRIOR, ShipMethod, SingleModel.
SELECT
Carrier,
PRIOR,
ShipMethod,
GroupOrder,
#row_no:=IF(#prev_val = ANY_VALUE(t.SingleModel),
#row_no,
#row_no + 1) AS 'SOrder',
SingleModel,
COUNT(t.SingleModel) AS 'SingleModelTotQty',
#prev_val:=ANY_VALUE(t.SingleModel)
FROM
(SELECT
CASE
WHEN ANY_VALUE(W.hdr_user_defined_field18) LIKE '%Fedex%' THEN 'FEDEX'
ELSE 'UPS'
END AS 'Carrier',
CASE
WHEN
ANY_VALUE(W.hdr_user_defined_field20) IN ('16' , '26', '45', '88', '96')
OR ANY_VALUE(W.hdr_user_defined_field20) IN ('19' , '21', '22', '23', '24', '44', '76', '77', '78')
THEN 'PRIOR'
ELSE 'NORM'
END AS 'PRIOR',
CASE
WHEN
ANY_VALUE(W.hdr_user_defined_field20) IN ('88' , '96', '80', '97')
OR ANY_VALUE(W.hdr_user_defined_field20) IN ('76' , '77', '78', '79')
OR ANY_VALUE(W.`Ship-To Location`) != ''
THEN 'S2S'
ELSE 'S2H'
END AS 'ShipMethod',
CASE
WHEN SUM(Quantity) = 1 THEN 'Single'
WHEN
SUM(Quantity) > 1 AND COUNT(`PO Number`) = 1
THEN 'Multiple'
WHEN COUNT(`PO Number`) > 1 THEN 'Mixed'
END AS 'GroupOrder',
CASE
WHEN SUM(Quantity) = 1 THEN `Supplier Item Nbr`
WHEN SUM(Quantity) > 1
AND COUNT(`PO Number`) = 1
THEN ' '
WHEN COUNT(`PO Number`) > 1 THEN ' '
END AS 'SingleModel',
`PO Number` AS 'PONUM',
CASE
WHEN SUM(Quantity) = 1 THEN ' '
ELSE SUM(Quantity)
END AS 'QtyPerMulPO'
FROM
dropship.Walmart_FullPOs W
WHERE
ProcessedDate = CURDATE() - 5
GROUP BY `PO Number`
**ORDER BY Carrier , PRIOR DESC , ShipMethod , GroupOrder DESC , SingleModel DESC**
) t,
(SELECT #row_no:=0) x,
(SELECT #prev_val:='') y
WHERE
GroupOrder = 'Single'
GROUP BY Carrier , PRIOR , ShipMethod , SingleModel
ORDER BY t.Carrier , t.PRIOR DESC , t.ShipMethod , t.GroupOrder DESC , t.SingleModel DESC
Result
There is a problem with your sql_mode.
As of MySQL 5.7.x, the default sql mode includes ONLY_FULL_GROUP_BY. (Before 5.7.5, MySQL does not detect functional dependency and ONLY_FULL_GROUP_BY is not enabled by default).
ONLY_FULL_GROUP_BY: Non-deterministic grouping queries will be rejected
For more details check the documentation of sql_mode
Method 1:
Check default value of sql_mode:
SELECT ##sql_mode
Remove ONLY_FULL_GROUP_BY from console by executing below query:
SET GLOBAL sql_mode=(SELECT REPLACE(##sql_mode,'ONLY_FULL_GROUP_BY',''));
Method 2:
Access phpmyadmin for editing your sql_mode
Login on phpmyadmin and open localhost
Top on Variables present on the top in menu items and search out for sql mode
Click on edit button to remove ONLY_FULL_GROUP_BY and save
Is there any difference between:
CONCAT_WS('', column)=''
AND
column is null OR column=0 *(and optionally 'OR column="" ')*
Is one of them better/faster...?
SELECT my_fields FROM my_table
WHERE my_terms_clause='anything' AND CONCAT_WS( '', nb_check ) = ''
OR
SELECT my_fields FROM my_table
WHERE my_terms_clause='anything' AND (p.nb_check is null OR p.nb_check = 0)
I usually use "column is null OR column=0", but I just want "expert's tips".
You should definitely use:
where col is null or column = 0
First, the intention of the code is much clearer. Second, the function call prevents the optimizer from using an index. To be honest,the or also makes it hard for the optimizer to use an index.
Probably the most efficient way to write the query is using union all:
SELECT my_fields
FROM my_table p
WHERE my_terms_clause = 'anything' AND p.nb_check is null
UNION ALL
SELECT my_fields
FROM my_table p
WHERE my_terms_clause = 'anything' AND p.nb_check = 0;
This can take advantage of an index on my_table(my_terms_clause, nb_check).
If a column holds the value of 0, then concat_ws() with empty string as separator will return '0', not '', so the 2 expressions are not equal. If you need to check for null or 0, then better use that version, that actually checks this condition.
This is my SQL query
DECLARE #strManualRefundIdList AS VARCHAR(MAX)
SELECT #strManualRefundIdList = COALESCE(#strManualRefundIdList + '|', '') + CONVERT(VARCHAR(MAX), ManualRefund_strReasonCode)
FROM tblManualRefunds WHERE ManualRefund_lngId IN ( 20 ,21 ,22 )
SELECT #strManualRefundIdList;
This gives like pivot, all the rows concatenated in a single row.
The Same i need to convert into MySql Query.
I tried like this
SELECT
CONCAT(COALESCE(CONCAT(v_strManualRefundIdList, '|'), '') , ( ManualRefund_lngId))
INTO
v_strManualRefundIdList
FROM tblManualRefunds
WHERE Trans_lngId IN ( 20 ,21 ,22 ) ;
But it throws error as Error Code: 1172. Result consisted of more than one row
How to translate that query. I am new to database.
Please help me in figuring out this.
UPDATE :
The way i found out was assigning into a cursor and loop through and concatenate it.
But is that the only way ? or any better way is available ?
SELECT group_concat(ManualRefund_strReasonCode SEPARATOR '|')
FROM tblManualRefunds
WHERE manualRefund_lngId in (20,21,22)
SQL Fiddle
As per xQbert Comment i tried this
SELECT
GROUP_CONCAT(COALESCE(CONCAT('shan', '|'), '') , ( ManualRefund_lngId))
INTO
#v_strManualRefundIdList
FROM tblManualRefunds ;
Its working fine.
Thanks Man.. !!!
Good Morning!
I am trying to combine two queries to make a table. (Please see code below)
`CREATE TABLE Layer_Loss
(
dYear INT NOT NULL,
EventNum INT NOT NULL,
Loss INT NULL,
Rec_L1 BIGINT NULL,
Rec_L2 BIGINT NULL,
Rec_L3 BIGINT NULL,
Cap_CML_L1 BIGINT NULL,
Cap_CML_L2 BIGINT NULL,
Cap_CML_L3 BIGINT NULL,
)
INSERT INTO Layer_Loss (dYear,EventNum, Loss, Rec_L1, Rec_L2, Rec_L3, Capped_CML_L1, Capped_CML_L2, Capped_CML_L3)
WITH c AS (SELECT Row_number() OVER (ORDER BY dYear) AS rownum,*
FROM Layer_Loss_Capped2)
SELECT *
FROM
(
SELECT dYear, ROW_NUMBER() OVER (Partition by dYear Order by dYear) as Event_Number, Loss
, 'Recovery_L1'=CASE
WHEN Loss<10000000 THEN 0
WHEN Loss<30000000 THEN 20000000-(30000000-Loss)
ELSE 20000000
END
, 'Recovery_L2'=CASE
WHEN Loss<30000000 THEN 0
WHEN Loss<60000000 THEN 30000000-(60000000-Loss)
ELSE 30000000
END
, 'Recovery_L3'=CASE
WHEN Loss<60000000 THEN 0
WHEN Loss<100000000 THEN 40000000-(100000000-Loss)
ELSE 40000000
END
, (SELECT *, 'Capped_CML_L1'=CASE
WHEN d.CML_L1>40000000 THEN 4000000
ELSE d.CML_L1
END
, (SELECT *, 'Capped_CML_L2'=CASE
WHEN d.CML_L2>60000000 THEN 6000000
ELSE d.CML_L1
END
, (SELECT *, 'Capped_CML_L3'=CASE
WHEN d.CML_L1>80000000 THEN 8000000
ELSE d.CML_L1
END
FROM
(
SELECT a.dYear, a.EventNum, a.Loss, a.Rec_L1, SUM(b.Rec_L1) AS CML_L1, SUM(b.Rec_L2) AS CML_L2, SUM(b.Rec_L3) as CML_L3
FROM c a
LEFT JOIN c b ON a.dYear = b.dYear AND b.rownum <= a.rownum
GROUP BY a.dYear, a.rownum, a.EventNum, a.Rec_L1, a.Loss
) AS d
) AS e
FROM ['04_AIR_StdHU_DS_noSS_ByTerr$']
) AS a
DROP TABLE Layer_Loss`
I have it so that the query about 'Recovery_L1', 'Recovery_L2', and 'Recovery_L3' are about of table "Layer_Loss", which I've called "Rec_L1", "Rec_L2", and "Rec_L3". When I try to add the query that leads to "Capped_CML_L1", "Capped_CML_L2", and "Capped_CML_L3" I get the following error:
"Msg 156, Level 15, State 1, Line 14
Incorrect syntax near the keyword 'WITH'.
Msg 319, Level 15, State 1, Line 14
Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon."
I have tried moving 'WITH' clause around but end up with the same result.
Also, this is not my end result. My next step would be to subtract the current row from the previous row from the columns "Capped_CML_L1", "Capped_CML_L2", and "Capped_CML_L3" into a column called "Inc_Rec_L1", "Inc_Rec_L2", and "Inc_Rec_L3". I was thinking about using a cursor, but I have never used one before, so if you have any suggestions on this, that would be great too!
Thank you for your help!
EDIT:
`CREATE TABLE Layer_Loss
(
dYear INT NOT NULL,
EventNum INT NOT NULL,
Loss INT NULL,
Rec_L1 BIGINT NULL,
Rec_L2 BIGINT NULL,
Rec_L3 BIGINT NULL,
Cap_CML_L1 BIGINT NULL,
Cap_CML_L2 BIGINT NULL,
Cap_CML_L3 BIGINT NULL,
)
;WITH c AS (SELECT Row_number() OVER (ORDER BY dYear) AS rownum,*
FROM Layer_Loss_Capped2)
INSERT INTO Layer_Loss (dYear,EventNum, Loss, Rec_L1, Rec_L2, Rec_L3, Capped_CML_L1, Capped_CML_L2, Capped_CML_L3)
SELECT *
FROM
(
SELECT dYear, ROW_NUMBER() OVER (Partition by dYear Order by dYear) as Event_Number, Loss
, 'Recovery_L1'=CASE
WHEN Loss<10000000 THEN 0
WHEN Loss<30000000 THEN 20000000-(30000000-Loss)
ELSE 20000000
END
, 'Recovery_L2'=CASE
WHEN Loss<30000000 THEN 0
WHEN Loss<60000000 THEN 30000000-(60000000-Loss)
ELSE 30000000
END
, 'Recovery_L3'=CASE
WHEN Loss<60000000 THEN 0
WHEN Loss<100000000 THEN 40000000-(100000000-Loss)
ELSE 40000000
END
, (SELECT *, 'Capped_CML_L1'=CASE
WHEN d.CML_L1>40000000 THEN 4000000
ELSE d.CML_L1
END
, (SELECT *, 'Capped_CML_L2'=CASE
WHEN d.CML_L2>60000000 THEN 6000000
ELSE d.CML_L1
END
, (SELECT *, 'Capped_CML_L3'=CASE
WHEN d.CML_L1>80000000 THEN 8000000
ELSE d.CML_L1
END
FROM
(
SELECT a.dYear, a.EventNum, a.Loss, a.Rec_L1, SUM(b.Rec_L1) AS CML_L1, SUM(b.Rec_L2) AS CML_L2, SUM(b.Rec_L3) as CML_L3
FROM c a
LEFT JOIN c b ON a.dYear = b.dYear AND b.rownum <= a.rownum
GROUP BY a.dYear, a.rownum, a.EventNum, a.Rec_L1, a.Loss
) AS d
FROM ['04_AIR_StdHU_DS_noSS_ByTerr$']
) AS e
) AS f
) AS g
) AS a
DROP TABLE Layer_Loss`
When I put in the above edited code I get error:
Msg 156, Level 15, State 1, Line 58
Incorrect syntax near the keyword 'FROM'.
I would like to be able to reference Capped_CML_L1, Capped_CML_L2, and Capped_CML_L3 in another query or table or cursor later on. I wanted it to be under just 'e' but I'm not sure how with the parentheses
WITH must be separated from any preceding command by a ;.
When a CTE is used in a statement that is part of a batch, the statement before it must be followed by a semicolon.
Also, it must be the first part of the entire statement it's a part of, whether that be a plain SELECT or an INSERT. Try:
/* CREATE TABLE */
;WITH c AS (SELECT Row_number() OVER (ORDER BY dYear) AS rownum,*
FROM Layer_Loss_Capped2)
INSERT INTO Layer_Loss (dYear,EventNum, Loss, Rec_L1, Rec_L2, Rec_L3,
Capped_CML_L1, Capped_CML_L2, Capped_CML_L3)
SELECT *
FROM
(
SELECT dYear, ROW_NUMBER() OVER ...
(Have also moved the WITH before the INSERT, having realised what was being attempted)
See also Transact SQL Syntax conventions:
; Transact-SQL statement terminator.Although the semicolon is not required for most statements in this version of SQL Server, it will be required in a future version.
One of the main reasons for this is that there is a pre-existing use for the keyword WITH that modifies SELECT statements. By insisting on the ;, it makes the parse a lot easier.
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))