I have table having few number of columns wherein one column is for Execution number (exe_number) which stores the data in varchar(100).
select*from exedetails;
+------------+------------+------------+-------------+
| exe_number | pass_count | fail_count | error_count |
+------------+------------+------------+-------------+
| 02Aug_E1 | 98 | 9 | 0 |
| 31Jul_E1 | 94 | 8 | 1 |
| 30Jul_E2 | 76 | 9 | 3 |
| 01Aug_E2 | 98 | 7 | 0 |
| 02Aug_E2 | 76 | 8 | 2 |
| 30Jul_E1 | 98 | 12 | 9 |
| 31Jul_E2 | 91 | 6 | 1 |
| 01Aug_E1 | 67 | 14 | 2 |
+------------+------------+------------+-------------+
8 rows in set (0.00 sec)
describe exedetails;
+-------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+-------+
| exe_number | varchar(100) | YES | | NULL | |
| pass_count | int | YES | | NULL | |
| fail_count | int | YES | | NULL | |
| error_count | int | YES | | NULL | |
+-------------+--------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
Here 30/31/01/02 are a date, Aug/Jul is a month and E1/E2 is Execution iteration (This is for my simplicity).
Lets consider present date is
'02 August 2022'
So I want to write a query in such a way that all records starting with
02Aug_
should be at the top of table.
Means It should get arranged according to date so that present day record should be at the top.
Query I wrote for this is not giving desired result:
select exe_number from exedetails order by exe_number desc;
+------------+
| exe_number |
+------------+
| 31Jul_E2 |
| 31Jul_E1 |
| 30Jul_E2 |
| 30Jul_E1 |
| 02Aug_E2 |
| 02Aug_E1 |
| 01Aug_E2 |
| 01Aug_E1 |
+------------+
8 rows in set (0.00 sec)
select exe_number from exedetails order by exe_number asc;
+------------+
| exe_number |
+------------+
| 01Aug_E1 |
| 01Aug_E2 |
| 02Aug_E1 |
| 02Aug_E2 |
| 30Jul_E1 |
| 30Jul_E2 |
| 31Jul_E1 |
| 31Jul_E2 |
+------------+
8 rows in set (0.00 sec)
Expected Result :
+------------+
| exe_number |
+------------+
| 02Aug_E1 |
| 02Aug_E2 |
| 01Aug_E1 |
| 01Aug_E2 |
| 31Jul_E1 |
| 31Jul_E2 |
| 30Jul_E1 |
| 30Jul_E2 |
+------------+
Is there any way in MySQL/PostgreSQL by which I can get my desired solution?
Just convert the "date part" to a proper date value, then you can sort by it:
select *
from exedetails
order by to_date(left(exe_number, 5), 'ddmon') desc,
right(exe_number, 2)
Note that to_date() working with a month name depends on your locale settings and the values in the column.
Online example for Postgres
Related
I need to INSERT or UPDATE data in a table using data from other tables; I understand the basic
insert into table (a,b,c)
select h, i, j
from otherTable
where........
My challenge comes from the fact that the data is spread across multiple tables and in one of the tables the data is metadata stored in rows, not columns. Therefore I need to use JOIN and possible UNION to get what is needed.
Unfortunately after trying everything I read in both the Maria manual, on the Maria forum and on Stack overflow I can not get it to work.
Here is what I am attempting to do:
insert data into dbc_jot_groupmembers in the following fields using source data as shown:
jot_grpid = dbc_bp_groups_members.group_id
jot_bbmemid = dbc_bp_groups_members.user_id
jot_grpmemname = dbc_bp_xprofile_data.value where field_id=3
jot_grpmemnum = dbc_bp_xprofile_data.value where field_id=4
I need the final result to look like this:
select * from dbc_jot_groupmembers;
+--------------+-----------+----------------+---------------+---------------------+-------------+
| jot_grpmemid | jot_grpid | jot_grpmemname | jot_grpmemnum | jot_grpmemts | jot_bbmemid |
+--------------+-----------+----------------+---------------+---------------------+-------------+
| 1 | 17 | hutchdad | +17047047045 | 2021-06-15 14:56:19 | 14 |
| 2 | 24 | hutchdad | +17047047045 | 2021-06-15 19:49:58 | 14 |
| 3 | 25 | hutchdad | +17047047045 | 2021-06-15 19:49:58 | 14 |
| 4 | 17 | hutchmom | +17773274355 | 2021-06-15 19:49:58 | 15 |
| 5 | 24 | hutchmom | +17773274355 | 2021-06-15 19:49:58 | 15 |
| 6 | 16 | ledwards | +14567655645 | 2021-06-15 19:49:58 | 11 |
| 7 | 16 | medwards | +12223334545 | 2021-06-15 19:49:58 | 10 |
| 7 | 20 | medwards | +12223334545 | 2021-06-15 19:49:58 | 10 |
SAMPLE DATA FROM SOURCE TABELS AND TABLE DEFINITIONS:
MariaDB [devDisciplePlaceCom]> describe dbc_bp_groups_members;
+---------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+----------------+
| id | bigint(20) | NO | PRI | NULL | auto_increment |
| group_id | bigint(20) | NO | MUL | NULL | |
| user_id | bigint(20) | NO | MUL | NULL | |
| inviter_id | bigint(20) | NO | MUL | NULL | |
| is_admin | tinyint(1) | NO | MUL | 0 | |
| is_mod | tinyint(1) | NO | MUL | 0 | |
| user_title | varchar(100) | NO | | NULL | |
| date_modified | datetime | NO | | NULL | |
| comments | longtext | NO | | NULL | |
| is_confirmed | tinyint(1) | NO | MUL | 0 | |
| is_banned | tinyint(1) | NO | | 0 | |
| invite_sent | tinyint(1) | NO | | 0 | |
+---------------+--------------+------+-----+---------+----------------+
12 rows in set (0.002 sec)
describe dbc_bp_xprofile_data;
+--------------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+---------------------+------+-----+---------+----------------+
| id | bigint(20) unsigned | NO | PRI | NULL | auto_increment |
| field_id | bigint(20) unsigned | NO | MUL | NULL | |
| user_id | bigint(20) unsigned | NO | MUL | NULL | |
| value | longtext | NO | | NULL | |
| last_updated | datetime | NO | | NULL | |
+--------------+---------------------+------+-----+---------+----------------+
5 rows in set (0.001 sec)
THIS IS THE LIST OF GROUPS AND WHAT USERS THEY ARE IN.
select group_id,user_id from dbc_bp_groups_members ;
+----------+---------+
| group_id | user_id |
+----------+---------+
| 16 | 13 |
| 16 | 12 |
| 16 | 11 |
| 16 | 10 |
| 17 | 14 |
| 17 | 15 |
| 17 | 16 |
| 17 | 17 |
| 17 | 18 |
| 17 | 19 |
| 20 | 10 |
| 24 | 14 |
| 24 | 16 |
| 24 | 15 |
| 24 | 17 |
| 24 | 19 |
| 25 | 19 |
| 25 | 14 |
| 1 | 14 |
| 11 | 14 |
+----------+---------+
20 rows in set (0.000 sec)
THIS IS THE TABLE CONTAINING THE USERS METADATA. IN MY CASE I NEED THE PHOEN NUMBER AND NAME WHICH ARE IN THE value FIELD WITH A field_id of 3 and 4.
select * from dbc_bp_xprofile_data where user_id > 9 and field_id > 2 AND field_id < 5;
+-----+----------+---------+---------------+---------------------+
| id | field_id | user_id | value | last_updated |
+-----+----------+---------+---------------+---------------------+
| 31 | 3 | 10 | medwards | 2021-06-24 03:11:59 |
| 34 | 3 | 11 | ledwards | 2021-06-24 03:11:24 |
| 37 | 3 | 12 | nedwards | 2021-04-24 14:47:18 |
| 40 | 3 | 13 | iedwards | 2021-04-24 14:47:52 |
| 43 | 3 | 14 | hutchdad | 2021-06-21 14:53:08 |
| 46 | 3 | 15 | hutchmom | 2021-06-24 03:10:58 |
| 49 | 3 | 16 | hutchdaughter | 2021-04-24 16:54:48 |
| 52 | 3 | 17 | hutchson1 | 2021-04-24 16:55:43 |
| 55 | 3 | 18 | hutchson2 | 2021-04-24 16:57:42 |
| 58 | 3 | 19 | hutchson3 | 2021-04-24 16:58:44 |
| 78 | 3 | 25 | demoadmin | 2021-06-08 02:01:39 |
| 158 | 4 | 14 | 7047047045 | 2021-06-21 14:53:08 |
| 190 | 3 | 58 | dupdup | 2021-06-23 19:46:19 |
| 191 | 4 | 15 | 7773274355 | 2021-06-24 03:10:58 |
| 193 | 4 | 11 | 4567655645 | 2021-06-24 03:11:24 |
| 195 | 4 | 10 | 2223334545 | 2021-06-24 03:11:59 |
+-----+----------+---------+---------------+---------------------+
16 rows in set (0.000 sec)
If this can not be done is a single INSERT then I can use an INSERT with subsequent UPDATE statements. I also understand that this is not best practice and violates 3nf and probably several other best practice principles. Unfortunately, I am at the mercy of the application and can not change the code, so the only way to get this to work is to put duplicate data in the database as described below:
It can be done with a single INSERT. However, there are some information need to be addressed as what I've posted in a the comment. In the meantime, here is an example query that you can use to do the operation that you want:
SELECT ROW_NUMBER() OVER (ORDER BY A.group_id, A.user_id) AS 'jot_grpmemid',
A.group_id AS 'jot_grpid',
MAX(CASE WHEN B.field_id=3 THEN B.value ELSE '' END) AS 'jot_grpmemname',
MAX(CASE WHEN B.field_id=4 THEN CONCAT('+',B.value) ELSE '' END) AS 'jot_grpmemnum',
A.user_id AS 'jot_bbmemid'
FROM
dbc_bp_groups_members A JOIN dbc_bp_xprofile_data B
ON A.user_id=B.user_id
GROUP BY A.group_id, A.user_id;
Like I said in the comment, I'm not sure how you get/generate jot_grpmemid because you have two 7 in the expected result so I assume it's a typo. I guess, at this point it's up to you to modify the query accordingly.
Demo fiddle
meaning the result should be selecting 3rd, 7th, 11th, 15 rows etc.
Every row has an ID, in ascending order.
I am stuck on this for hours! Any help is highly appreciated!
A simpler way would be to use mod on the id itself:
select * from table where (id + 1) mod 4 = 0;
Suppose your schema is:
CREATE TABLE t1 (
id int(11) DEFAULT NULL,
data varchar(20) DEFAULT NULL
);
mysql> select * from t1;
+------+--------+
| id | data |
+------+--------+
| 1 | abc-1 |
| 2 | abc-2 |
| 3 | abc-3 |
| 4 | abc-4 |
| 5 | abc-5 |
| 6 | abc-6 |
| 7 | abc-7 |
| 8 | abc-8 |
| 9 | abc-9 |
| 10 | abc-10 |
| 11 | abc-11 |
| 12 | abc-12 |
| 13 | abc-13 |
| 14 | abc-14 |
| 15 | abc-15 |
| 16 | abc-16 |
+------+--------+
16 rows in set (0.00 sec)
mysql> select id,data from (select mod(#r:=#r+1,4) as isfetch,id,data from t1,(select #r:=0) s) k where k.isfetch=0 order by id;
+------+--------+
| id | data |
+------+--------+
| 4 | abc-4 |
| 8 | abc-8 |
| 12 | abc-12 |
| 16 | abc-16 |
+------+--------+
4 rows in set (0.01 sec)
I have a below mysql query. It's returns wrong value. Please HELP ME to resolve this issue.
SELECT
T1.PARENT_ID,
GROUP_CONCAT(
IF(T2.PROPERTIES IS NULL, "NA", T2.PROPERTIES)
ORDER BY T1.ORDER_INDEX ASC
) AS DATA
FROM TABLE_1 T1
JOIN TABLE_2 T2
ON T1.ID=T2.ID
WHERE T1.AUTHOR="123"
GROUP BY T1.PARENT_ID
ORDER BY T1.PARENT_ID;
Mysql Version ===> 5.0.27-standard-log
The above query returns:
+-----------+---------------+
| parent_id | data |
+-----------+---------------+
| 12345 | te,test1,test |
| 23456 | NA |
+-----------+---------------+
2 rows in set (0.00 sec)
_
But, it should be:
+-----------+---------------+
| parent_id | data |
+-----------+---------------+
| 12345 | NA,test1,test |
| 23456 | NA |
+-----------+---------------+
2 rows in set (0.00 sec)
-
-
########################### TABLE DATA ############################
mysql> select * from Table_1;
+----+-----------+--------+-------------+
| id | parent_id | author | order_index |
+----+-----------+--------+-------------+
| 1 | 12345 | 123 | 3 |
| 2 | 12345 | 123 | 1 |
| 3 | 23456 | 123 | 1 |
| 4 | 12345 | 123 | 2 |
+----+-----------+--------+-------------+
4 rows in set (0.00 sec)
mysql> select * from Table_2;
+----+------------+
| id | properties |
+----+------------+
| 1 | test |
| 2 | NULL |
| 3 | NULL |
| 4 | test1 |
+----+------------+
4 rows in set (0.00 sec)
-
-
########################### TABLE MODEL ############################
mysql> desc Table_1;
+-------------+-----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-----------+------+-----+---------+-------+
| id | int(11) | NO | PRI | | |
| parent_id | char(100) | YES | | NULL | |
| author | char(100) | YES | | NULL | |
| order_index | int(11) | YES | | 0 | |
+-------------+-----------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql> desc Table_2;
+------------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+---------+------+-----+---------+-------+
| id | int(11) | NO | PRI | | |
| properties | text | YES | | NULL | |
+------------+---------+------+-----+---------+-------+
2 rows in set (0.00 sec)
So, here is the structure:
mysql> describe tier;
+---------------------+----------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------------+----------------+------+-----+---------+----------------+
| ID | int(10) | NO | PRI | NULL | auto_increment |
| UP_TO | decimal(21,10) | YES | | NULL | |
+---------------------+----------------+------+-----+---------+----------------+
2 rows in set (0.01 sec)
Then the datas:
mysql> select id, up_to from tier;
+----+-----------------+
| id | up_to |
+----+-----------------+
| 1 | 1000.0000000000 |
| 2 | 2000.0000000000 |
| 3 | 3000.0000000000 |
| 4 | 500.0000000000 |
| 5 | 1000.0000000000 |
| 6 | 1500.0000000000 |
| 7 | 100.0000000000 |
| 8 | 200.0000000000 |
| 9 | 1000.0000000000 |
| 10 | 2000.0000000000 |
| 11 | 100.0000000000 |
| 12 | 200.0000000000 |
+----+-----------------+
12 rows in set (0.00 sec)
Then there's a little transformation:
mysql> SELECT id, TRIM(TRAILING '.' FROM TRIM(TRAILING '0' FROM CAST(up_to AS CH
AR) )) as converted from tier;
+----+-----------+
| id | converted |
+----+-----------+
| 1 | 1000 |
| 2 | 2000 |
| 3 | 3000 |
| 4 | 500 |
| 5 | 1000 |
| 6 | 1500 |
| 7 | 100 |
| 8 | 200 |
| 9 | 1000 |
| 10 | 2000 |
| 11 | 100 |
| 12 | 200 |
+----+-----------+
12 rows in set (0.00 sec)
Fine.
Let's put that in a stored function to be handy!
DELIMITER //
CREATE FUNCTION strip_trailing_zero(I_DEC DECIMAL(10,7)) RETURNS VARCHAR(20) DETERMINISTIC
BEGIN
DECLARE strBuff VARCHAR(20);
DECLARE cnt NUMERIC(2);
DECLARE tString VARCHAR(20);
SELECT CAST(I_DEC AS CHAR) INTO tString;
SELECT LOCATE('.',tString) INTO cnt;
IF cnt > 0 THEN
SELECT TRIM(TRAILING '.' FROM TRIM(TRAILING '0' FROM tString)) INTO strBuff;
ELSE
SET strBuff = tString;
END IF;
RETURN strBuff;
END//
DELIMITER ;
Cool.
GRANT EXECUTE ON FUNCTION mysql.strip_trailing_zero TO 'whatever'#'localhost';
At last, trying out my new toy now... :
mysql> select id, mysql.strip_trailing_zero(`up_to`) as converted_2 from tier;
+----+-------------+
| id | converted_2 |
+----+-------------+
| 1 | 999.9999999 |
| 2 | 999.9999999 |
| 3 | 999.9999999 |
| 4 | 500 |
| 5 | 999.9999999 |
| 6 | 999.9999999 |
| 7 | 100 |
| 8 | 200 |
| 9 | 999.9999999 |
| 10 | 999.9999999 |
| 11 | 100 |
| 12 | 200 |
+----+-------------+
12 rows in set, 7 warnings (0.02 sec)
Well, then, f*ck you too mysql!
No seriously, that's a silly joke. I'm convinced I did something wrong and there's a floating point conversion in the middle but I just can't figure it out!
Help welcome!
Thanks.
S.
edit: result after changing the input parameter type to DECIMAL(21,10):
mysql> select id, mysql.strip_trailing_zero(`up_to`) as converted_2 from tier;
+----+-------------+
| id | converted_2 |
+----+-------------+
| 1 | 1000 |
| 2 | 2000 |
| 3 | 3000 |
| 4 | 500 |
| 5 | 1000 |
| 6 | 1500 |
| 7 | 100 |
| 8 | 200 |
| 9 | 1000 |
| 10 | 2000 |
| 11 | 100 |
| 12 | 200 |
+----+-------------+
12 rows in set (0.02 sec)
Problem solved... Great! thank you!
Your UP_TO field in your table is defined as decimal(21,10), but your function takes in a decimal(10,7). I imagine that is stripping your value.
Try changing your function to accept decimal(21,10) instead.
I am attempting to write a migration script (between 2 versions of a program) to populate the phppos_permissions_actions table.
The rule for populating is: "If the user has permission for the module (based on phppos_permissions), then they are granted all action permissions for that module. (Which can be looked up in phppos_module_actions)".
I am trying to write a query or a set of queries that makes the following rule happen. Could someone guide me in the right direction? Below is my schema
mysql> describe phppos_modules;
+---------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+-------+
| name_lang_key | varchar(255) | NO | UNI | NULL | |
| desc_lang_key | varchar(255) | NO | UNI | NULL | |
| sort | int(10) | NO | | NULL | |
| module_id | varchar(255) | NO | PRI | NULL | |
+---------------+--------------+------+-----+---------+-------+
4 rows in set (0.01 sec)
mysql> select * from phppos_modules;
+-------------------+------------------------+------+------------+
| name_lang_key | desc_lang_key | sort | module_id |
+-------------------+------------------------+------+------------+
| module_config | module_config_desc | 100 | config |
| module_customers | module_customers_desc | 10 | customers |
| module_employees | module_employees_desc | 80 | employees |
| module_giftcards | module_giftcards_desc | 90 | giftcards |
| module_item_kits | module_item_kits_desc | 30 | item_kits |
| module_items | module_items_desc | 20 | items |
| module_receivings | module_receivings_desc | 60 | receivings |
| module_reports | module_reports_desc | 50 | reports |
| module_sales | module_sales_desc | 70 | sales |
| module_suppliers | module_suppliers_desc | 40 | suppliers |
+-------------------+------------------------+------+------------+
10 rows in set (0.00 sec)
mysql> describe phppos_modules_actions;
+-----------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+--------------+------+-----+---------+-------+
| action_id | varchar(255) | NO | PRI | NULL | |
| module_id | varchar(255) | NO | PRI | NULL | |
| action_name_key | varchar(255) | NO | | NULL | |
| sort | int(11) | NO | | NULL | |
+-----------------+--------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql>
mysql> select * from phppos_modules_actions;
+----------------+-----------+--------------------------------+------+
| action_id | module_id | action_name_key | sort |
+----------------+-----------+--------------------------------+------+
| add_update | customers | module_action_add_update | 1 |
| add_update | employees | module_action_add_update | 130 |
| add_update | item_kits | module_action_add_update | 70 |
| add_update | items | module_action_add_update | 40 |
| add_update | suppliers | module_action_add_update | 100 |
| delete | customers | module_action_delete | 20 |
| delete | employees | module_action_delete | 140 |
| delete | item_kits | module_action_delete | 80 |
| delete | items | module_action_delete | 50 |
| delete | suppliers | module_action_delete | 110 |
| search | customers | module_action_search_customers | 30 |
| search | employees | module_action_search_employees | 150 |
| search | item_kits | module_action_search_item_kits | 90 |
| search | items | module_action_search_items | 60 |
| search | suppliers | module_action_search_suppliers | 120 |
| see_cost_price | items | module_see_cost_price | 61 |
+----------------+-----------+--------------------------------+------+
16 rows in set (0.00 sec)
mysql> describe phppos_permissions
-> ;
+-----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+-------+
| module_id | varchar(255) | NO | PRI | NULL | |
| person_id | int(10) | NO | PRI | NULL | |
+-----------+--------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
mysql> select * from phppos_permissions;
+------------+-----------+
| module_id | person_id |
+------------+-----------+
| config | 1 |
| customers | 1 |
| employees | 1 |
| giftcards | 1 |
| item_kits | 1 |
| items | 1 |
| receivings | 1 |
| reports | 1 |
| sales | 1 |
| suppliers | 1 |
| sales | 301 |
| sales | 741 |
| config | 759 |
| customers | 759 |
| employees | 759 |
| giftcards | 759 |
| item_kits | 759 |
| items | 759 |
| receivings | 759 |
| reports | 759 |
| sales | 759 |
| suppliers | 759 |
| sales | 776 |
+------------+-----------+
23 rows in set (0.00 sec)
mysql> describe phppos_permissions_actions;
+-----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+-------+
| module_id | varchar(255) | NO | PRI | NULL | |
| person_id | int(11) | NO | PRI | NULL | |
| action_id | varchar(255) | NO | PRI | NULL | |
+-----------+--------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
mysql>
Does
insert phppos_permissions_actions (module_id, person_id, action_id)
select distinct
phppos_permissions.module_id, phppos_permissions.person_id, action_id
from phppos_permissions
inner join phppos_modules_actions on phppos_permissions.module_id = phppos_modules_actions.module_id
order by module_id, person_id
solve your problem?