include an aliased field in mysql to another alias - mysql

i have a table that stores values in hour-minute float format and im pulling the data from the database while converting hour-minute float to minutes using this query.
SELECT
DailyTimelog.id,
DailyTimelog.payroll_cut_off_id,
DailyTimelog.employee_id,
DailyTimelog.work_shift_id,
DailyTimelog.employee_status,
DailyTimelog.timelog_date,
DailyTimelog.is_rest_day,
DailyTimelog.is_on_leave,
DailyTimelog.is_holiday,
DailyTimelog.is_regular_holiday,
DailyTimelog.is_special_holiday,
DailyTimelog.is_absent,
(
(SUBSTRING_INDEX(tardiness_hours, '.', 1)*60)
+(
case when (INSTR(tardiness_hours, '.') = 0)
THEN 0
ELSE SUBSTRING_INDEX(tardiness_hours, '.', -1)
END)
) as tardiness_hours,
(
(SUBSTRING_INDEX(leave_hours, '.', 1)*60)
+(
case when (INSTR(leave_hours, '.') = 0)
THEN 0
ELSE SUBSTRING_INDEX(leave_hours, '.', -1)
END)
) as leave_hours,
DailyTimelog.leave_credit_used,
(
(SUBSTRING_INDEX(normal_work_hrs, '.', 1)*60)
+(
case when (INSTR(normal_work_hrs, '.') = 0)
THEN 0
ELSE SUBSTRING_INDEX(normal_work_hrs, '.', -1)
END)
) as normal_work_hrs,
(
(SUBSTRING_INDEX(overtime_work_hrs, '.', 1)*60)
+(
case when (INSTR(overtime_work_hrs, '.') = 0)
THEN 0
ELSE SUBSTRING_INDEX(overtime_work_hrs, '.', -1)
END)
) as overtime_work_hrs,
(
(SUBSTRING_INDEX(night_premium_work_hrs, '.', 1)*60)
+(
case when (INSTR(night_premium_work_hrs, '.') = 0)
THEN 0
ELSE SUBSTRING_INDEX(night_premium_work_hrs, '.', -1)
END)
) as night_premium_work_hrs
FROM `daily_timelogs` as DailyTimelog WHERE DailyTimelog.payroll_cut_off_id = 404
when i output the data using print_f() i get something like this:
[DailyTimelog] => Array
(
[id] => 93608
[payroll_cut_off_id] => 404
[employee_id] => 126
[work_shift_id] => 11
[employee_status] => 2
[timelog_date] => 2016-03-27
[is_rest_day] => 1
[is_on_leave] =>
[is_holiday] =>
[is_regular_holiday] =>
[is_special_holiday] =>
[is_absent] =>
[leave_credit_used] => 0
)
[0] => Array
(
[tardiness_hours] => 0
[leave_hours] => 0
[normal_work_hrs] => 0
[overtime_work_hrs] => 0
[night_premium_work_hrs] => 0
)
is there a way to include index 0 to the DailyTimelogs index while still on mysql?

Related

How to parse a string and get the value after "=" character

So I have string that should contains "Object.Name" once in a row , if I see it ,I have to get the value after "=" character. If doesn't match it anywhere in the string i should move hardcoded value.
Here is example of the string:
Object.Name=ASDD||Product.Name=DSA
Product.Name=QWE||Object.Name=WSXS
Storage.Name=12345||Object.Name=WERR||Product.Name=QAZ
I know that I should use case for that but doesn't know how to proceed the string
case
when (match the string ) then (value after the "=")
else (hardcoded value)
end
In Oracle, you can use:
SELECT value,
CASE
WHEN start_pos = 0
THEN NULL
ELSE SUBSTR(
'||' || value || '||',
start_pos + LENGTH('||Object.Name='),
end_pos - start_pos - LENGTH('||Object.Name=')
)
END AS object_name
FROM (
SELECT value,
INSTR(
'||' || value || '||',
'||Object.Name='
) AS start_pos,
INSTR(
'||' || value || '||',
'||',
INSTR('||' || value || '||', '||Object.Name=')+LENGTH('||Object.Name=')
) AS end_pos
FROM table_name
)
Which, for the sample data:
CREATE TABLE table_name (value) AS
SELECT 'Object.Name=ASDD||Product.Name=DSA' FROM DUAL UNION ALL
SELECT 'Product.Name=QWE||Object.Name=WSXS' FROM DUAL UNION ALL
SELECT 'Storage.Name=12345||Object.Name=WERR||Product.Name=QAZ' FROM DUAL;
Outputs:
VALUE
OBJECT_NAME
Object.Name=ASDD||Product.Name=DSA
ASDD
Product.Name=QWE||Object.Name=WSXS
WSXS
Storage.Name=12345||Object.Name=WERR||Product.Name=QAZ
WERR
db<>fiddle here
Since you changed the tags, in MySQL:
SELECT value,
CASE
WHEN start_pos = 0
THEN NULL
ELSE SUBSTRING(
CONCAT('||', value, '||'),
start_pos + LENGTH('||Object.Name='),
end_pos - start_pos - LENGTH('||Object.Name=')
)
END AS object_name
FROM (
SELECT value,
LOCATE(
'||Object.Name=',
CONCAT('||', value, '||')
) AS start_pos,
LOCATE(
'||',
CONCAT('||', value, '||'),
LOCATE('||Object.Name=', CONCAT('||', value, '||'))
+ LENGTH('||Object.Name=')
) AS end_pos
FROM table_name
) t
db<>fiddle here

MYSQL if (string in ( 0) ) returns TRUE

The following mysql query result returns 'TRUE',
select if('testString' in ( 0 ), 'TRUE', 'FALSE') from sampleTable
Can someone explain why please ?
You can try using
select cast('testString' AS UNSIGNED) from dual ; // return 0
do the fact because the cast for 'testString' AS UNSIGNED is = 0
then
select if( 'testString' in ( 0 ), 'TRUE', 'FALSE') from DUAL ;
match for casted value the 0 in (0) and return TRUE

Aliasing a CASE statement

I would need your help to fix a problem I'm facing.. I'm using a CASE function and aliasing its result by adding "AS 'RESULT'" after its "END".
Event though the query seems to run, I'm still getting the following warnings through phpmyadmin :
Unrecognized keyword (near AS)
Unexpected token (near 'RESULT')
I have no idea on why I'm getting this...I would appreciate your help to highlight my mistake ;-)
SELECT
app_fd_List_of_components.c_component,
app_fd_List_of_orders.c_package,
app_fd_List_of_orders.c_status,
app_fd_List_of_orders.dateCreated,
app_fd_List_of_orders.c_orderId,
app_fd_List_of_orders.Id,
temptablename.datetime AS Int_Date,
temptablename2.datetime AS Prdw3_Date,
#counterStatus := IF(
app_fd_List_of_orders.c_status <> 'Deployment completed',
IF(
temptablename.datetime IS NOT NULL,
'Counting',
'Initializing'
),
'Complete'
) AS 'Counter_status',
#counter := IF(
app_fd_List_of_orders.c_status <> 'Deployment completed',
IF(
temptablename.datetime IS NOT NULL,
(
35 - DATEDIFF(
CURRENT_DATE(), temptablename.datetime)
),
'n/a'
),
DATEDIFF(
temptablename2.datetime,
temptablename.datetime
)
) AS 'Counter',
CASE (WHEN(
(#counterStatus = 'Counting') AND(#counter < 0)
) THEN "black" WHEN(
(#counterStatus = 'Counting') AND(#counter > 5)
) THEN "green" WHEN(
(#counterStatus = 'Counting') AND(-1 < #counter < 5)
) THEN "orange" WHEN(
(#counterStatus = 'Complete') AND(#counter > 35)
) THEN "black" WHEN(
(#counterStatus = 'Complete') AND(#counter < 35)
) THEN "green" ELSE "n/a")
END AS 'RESULT'
FROM
app_fd_List_of_orders
JOIN app_fd_List_of_components ON
app_fd_List_of_orders.c_component = app_fd_list_of_components.id
LEFT JOIN(
SELECT app_form_data_audit_trail.datetime,
app_form_data_audit_trail.data
FROM
app_form_data_audit_trail
WHERE
SUBSTRING_INDEX(
SUBSTRING_INDEX(
app_form_data_audit_trail.data,
'"opdetails":"',
-1
),
'"',
1
) = 'int'
) AS temptablename
ON
app_fd_list_of_orders.c_orderid = SUBSTRING_INDEX(
SUBSTRING_INDEX(
temptablename.data,
'"orderId":"',
-1
),
'"',
1
)
LEFT JOIN(
SELECT app_form_data_audit_trail.datetime,
app_form_data_audit_trail.data
FROM
app_form_data_audit_trail
WHERE
SUBSTRING_INDEX(
SUBSTRING_INDEX(
app_form_data_audit_trail.data,
'"opdetails":"',
-1
),
'"',
1
) = 'prdw3'
) AS temptablename2
ON
app_fd_list_of_orders.c_orderid = SUBSTRING_INDEX(
SUBSTRING_INDEX(
temptablename2.data,
'"orderId":"',
-1
),
'"',
1
)
Have you tried removing the parentheses around the CASE statement?
CASE
WHEN #counterStatus = 'Counting' AND #counter < 0 THEN "black"
WHEN #counterStatus = 'Counting' AND #counter > 5 THEN "green"
WHEN #counterStatus = 'Counting' AND -1 < #counter < 5 THEN "orange"
WHEN #counterStatus = 'Complete' AND #counter > 35 THEN "black"
WHEN #counterStatus = 'Complete' AND #counter < 35 THEN "green"
ELSE "n/a"
END AS 'RESULT'
EDIT: I'm also not sure this is possible:
WHEN #counterStatus = 'Counting' AND -1 < #counter < 5 THEN "orange"
It may need to be written as:
WHEN #counterStatus = 'Counting' AND -1 < #counter AND #counter < 5 THEN "orange"

sum doesn't work while I use left join and group by

I have two table: imports and orders: I have attached these respectively.
I want the followings:
1. sum amount of same product_id in imports table
2. sum of pieces of same product_id in orders table as status wise.
my query is:
SELECT `Import`.*,
SUM( case when orders.status = "sold" THEN orders.pieces else 0 end) as total_sell,
SUM( case when orders.status = "No contact" THEN orders.pieces else 0 end) as no_contact,
SUM( case when orders.status = "confirmed" THEN orders.pieces else 0 end) as confirmed,
SUM( case when orders.status = "canceled" THEN orders.pieces else 0 end) as canceled
FROM `amrajegeachi`.`imports` AS `Import`
LEFT JOIN `orders`
ON `Import`.`product_id` = `orders`.`product_id`
WHERE 1 = 1
GROUP BY `Import`.`id`
and result for this query:
Array
(
[0] => Array
(
[Import] => Array
(
[id] => 1
[category_id] => 2
[product_id] => 2
[amount] => 50
[cost] => 8320
[comment] => transportation and others cost: 100
[created] => 2015-06-23 19:21:10
)
[0] => Array
(
[total_sell] => 10
[no_contact] => 1
[confirmed] => 2
[canceled] => 0
)
)
[1] => Array
(
[Import] => Array
(
[id] => 2
[category_id] => 2
[product_id] => 2
[amount] => 15
[cost] => 3000
[comment] =>
[created] => 2015-06-22 18:10:36
)
[0] => Array
(
[total_sell] => 10
[no_contact] => 1
[confirmed] => 2
[canceled] => 0
)
)
[2] => Array
(
[Import] => Array
(
[id] => 3
[category_id] => 2
[product_id] => 1
[amount] => 15
[cost] => 2000
[comment] =>
[created] => 2015-06-23 19:20:15
)
[0] => Array
(
[total_sell] => 10
[no_contact] => 0
[confirmed] => 0
[canceled] => 0
)
)
)
My expected result is:
Array
(
[0] => Array
(
[Import] => Array
(
[id] => 1
[category_id] => 2
[product_id] => 2
[amount] => 65
[cost] => 8320
[comment] => transportation and others cost: 100
[created] => 2015-06-23 19:21:10
)
[0] => Array
(
[total_sell] => 10
[no_contact] => 1
[confirmed] => 2
[canceled] => 0
)
)
[2] => Array
(
[Import] => Array
(
[id] => 3
[category_id] => 2
[product_id] => 1
[amount] => 15
[cost] => 2000
[comment] =>
[created] => 2015-06-23 19:20:15
)
[0] => Array
(
[total_sell] => 10
[no_contact] => 0
[confirmed] => 0
[canceled] => 0
)
)
)
How can I do this? I tried in different way such as:
SELECT `Import`.*, SUM(`Import`.`amount`) as total_import,
SUM( case when orders.status = "sold" THEN orders.pieces else 0 end) as total_sell,
SUM( case when orders.status = "No contact" THEN orders.pieces else 0 end) as no_contact,
SUM( case when orders.status = "confirmed" THEN orders.pieces else 0 end) as confirmed,
SUM( case when orders.status = "canceled" THEN orders.pieces else 0 end) as canceled
FROM `amrajegeachi`.`imports` AS `Import`
LEFT JOIN `orders`
ON `Import`.`product_id` = `orders`.`product_id`
WHERE 1 = 1
GROUP BY `Import`.`id`
But no luck :'(
I am guessing that you want information about each product. If so, you can do this in a few ways. Here is a union all method:
SELECT product_id, sum(amount) as total_import,
sum( case when status = 'sold' THEN pieces else 0 end) as total_sell,
sum( case when status = 'No contact' THEN pieces else 0 end) as no_contact,
sum( case when status = 'confirmed' THEN pieces else 0 end) as confirmed,
sum( case when status = 'canceled' THEN pieces else 0 end) as canceled
from ((select i.product_id, amount, NULL as status, NULL as pieces
from `amrajegeachi`.`imports` i
) union all
(select o.product_id, NULL, o.status, o.pieces
from `orders` o
)
) io
group by product_id;
SELECT `Import`.*, SUM(`Import`.`amount`) as total_import,
SUM( case when orders.status = "sold" THEN orders.pieces else 0 end) as total_sell,
SUM( case when orders.status = "No contact" THEN orders.pieces else 0 end) as no_contact,
SUM( case when orders.status = "confirmed" THEN orders.pieces else 0 end) as confirmed,
SUM( case when orders.status = "canceled" THEN orders.pieces else 0 end) as canceled
FROM `amrajegeachi`.`imports` AS `Import`
LEFT JOIN `orders`
ON `Import`.`product_id` = `orders`.`product_id`
WHERE 1 = 1
GROUP BY `Import`.`id

Create multiple categories with parent, child and grandchild

I want to create multiple categories with parent, child and grandchild.
And can order by ordering field.
id | parent_id | name | ordering
--------------------------------------
1 | 0 | Men | 1
2 | 0 | Women | 2
3 | 1 | Shoes | 3
4 | 2 | Watches | 4
5 | 1 | Pants | 5
6 | 3 | Sport | 6
7 | 3 | Casual | 7
8 | 2 | Book | 8
I want this result
Men
Shoes
Sport
Casual
Pants
Women
Watches
Book
Array out put
Array
(
[0] => Array
(
[id] => 1
[parent_id] => 0
[name] => Men
[ordering] => 1
[level] => parent //NEEDED
)
[1] => Array
(
[id] => 2
[parent_id] => 0
[name] => Women
[ordering] => 2
[level] => parent //NEEDED
)
[2] => Array
(
[id] => 3
[parent_id] => 1
[name] => Shoes
[ordering] => 3
[level] => child //NEEDED
)
[3] => Array
(
[id] => 4
[parent_id] => 2
[name] => Watches
[ordering] => 4
[level] => child //NEEDED
)
[4] => Array
(
[id] => 5
[parent_id] => 1
[name] => Pants
[ordering] => 5
[level] => child //NEEDED
)
[5] => Array
(
[id] => 6
[parent_id] => 3
[name] => Sport
[ordering] => 6
[level] => grandchild //NEEDED
)
[6] => Array
(
[id] => 7
[parent_id] => 3
[name] => Casual
[ordering] => 7
[level] => grandchild //NEEDED
)
[7] => Array
(
[id] => 8
[parent_id] => 2
[name] => Book
[ordering] => 8
[level] => child //NEEDED
)
)
This could be a solution to get your level information:
SELECT CASE WHEN parent_id = 0 THEN 'Parent'
ELSE CASE WHEN (SELECT T2.parent_id
FROM tab T2
WHERE T2.id = T.parent_id) = 0 THEN 'Child'
ELSE 'Grandchild'
END
END AS level
FROM tab T
Fiddle here
To put the content of this SQL into an array you need a server side language like PHP, since you're not using mongoDB or similar.
Assuming your table is called "items", you could do the following recursive query to generate names containing the parent and the ordering attribute, then order by the generated name and add spaces before the name according to the level:
;WITH itemsHierarchy (id, parent_id, name, generatedname, level)
AS
(
SELECT
i.id,
i.parent_id,
i.name,
CAST(i.name AS varchar(MAX)) as generatedname,
1 as level
FROM items i
WHERE i.parent_id = 0
UNION ALL
SELECT
i.id,
i.parent_id,
i.name,
CAST(ih.generatedname + '_' + CAST(i.ordering as varchar(2))+ '_' + i.name AS varchar(MAX)),
ih.level + 1
FROM items i
INNER JOIN itemsHierarchy ih ON i.parent_id = ih.id
)
SELECT REPLICATE(' ', level) + name
FROM itemsHierarchy
ORDER BY generatedname
Result:
Men
Shoes
Sport
Casual
Pants
Women
Watches
Book
Finally i found good answer
ref : Parent child mysql
select t0.*,
concat(
case coalesce(t4.parent_id, 0)
when 0 then ''
else concat(cast(t4.parent_id as char), '\\')
end,
case coalesce(t3.parent_id, 0)
when 0 then ''
else concat(cast(t3.parent_id as char), '\\')
end,
case coalesce(t2.parent_id, 0)
when 0 then ''
else concat(cast(t2.parent_id as char), '\\')
end,
case coalesce(t1.parent_id, 0)
when 0 then ''
else concat(cast(t1.parent_id as char), '\\')
end,
case coalesce(t0.parent_id, 0)
when 0 then ''
else concat(cast(t0.parent_id as char), '\\')
end,
cast(t0.id as char)
) as path
from mytable t0
left join mytable t1 on t0.parent_id = t1.Id
left join mytable t2 on t1.parent_id = t2.Id
left join mytable t3 on t2.parent_id = t3.Id
left join mytable t4 on t3.parent_id = t4.Id
order by
concat(
case coalesce(t4.parent_id, 0)
when 0 then ''
else concat(cast(t4.parent_id as char), '\\')
end,
case coalesce(t3.parent_id, 0)
when 0 then ''
else concat(cast(t3.parent_id as char), '\\')
end,
case coalesce(t2.parent_id, 0)
when 0 then ''
else concat(cast(t2.parent_id as char), '\\')
end,
case coalesce(t1.parent_id, 0)
when 0 then ''
else concat(cast(t1.parent_id as char), '\\')
end,
case coalesce(t0.parent_id, 0)
when 0 then ''
else concat(cast(t0.parent_id as char), '\\')
end,
cast(t0.id as char)
)