How to print table like(see image) that in mysql? [duplicate] - mysql

This question already exists:
how to display hour based column display in mysql? here with I attached images [duplicate]
Closed 2 years ago.
My table format
mysql> desc attendance;
+--------+------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+------------+------+-----+---------+----------------+
| aid | bigint(20) | NO | PRI | NULL | auto_increment |
| sid | int(10) | YES | MUL | NULL | |
| cid | bigint(20) | YES | MUL | NULL | |
| ttid | bigint(20) | YES | MUL | NULL | |
| did | int(3) | YES | MUL | NULL | |
| date | date | YES | | NULL | |
| hour | varchar(3) | YES | | NULL | |
| stuid | bigint(20) | YES | MUL | NULL | |
| status | varchar(8) | YES | | NULL | |
+--------+------------+------+-----+---------+----------------+
9 rows in set (0.47 sec)
mysql>
My table result is
mysql> select * from attendance;
+-----+------+------+------+------+------------+------+-------+---------+
| aid | sid | cid | ttid | did | date | hour | stuid | status |
+-----+------+------+------+------+------------+------+-------+---------+
| 1 | 2 | 13 | 4 | 3 | 2020-03-25 | p1 | 16 | present |
| 2 | 2 | 13 | 4 | 3 | 2020-03-25 | p1 | 17 | absent |
| 3 | 2 | 13 | 4 | 3 | 2020-03-25 | p1 | 18 | present |
| 4 | 2 | 13 | 4 | 3 | 2020-03-25 | p1 | 19 | absent |
| 5 | 2 | 13 | 4 | 3 | 2020-03-25 | p1 | 20 | present |
| 6 | 2 | 13 | 4 | 3 | 2020-03-25 | p1 | 15 | absent |
| 7 | 2 | 13 | 2 | 2 | 2020-03-17 | p6 | 16 | present |
| 8 | 2 | 13 | 2 | 2 | 2020-03-17 | p6 | 17 | absent |
| 9 | 2 | 13 | 2 | 2 | 2020-03-17 | p6 | 18 | present |
| 10 | 2 | 13 | 2 | 2 | 2020-03-17 | p6 | 19 | absent |
| 11 | 2 | 13 | 2 | 2 | 2020-03-17 | p6 | 20 | present |
| 12 | 2 | 13 | 2 | 2 | 2020-03-17 | p6 | 15 | absent |
| 13 | 2 | 13 | 4 | 3 | 2020-03-25 | p2 | 16 | present |
| 14 | 2 | 13 | 4 | 3 | 2020-03-25 | p2 | 17 | present |
| 15 | 2 | 13 | 4 | 3 | 2020-03-25 | p2 | 18 | present |
| 16 | 2 | 13 | 4 | 3 | 2020-03-25 | p2 | 19 | present |
| 17 | 2 | 13 | 4 | 3 | 2020-03-25 | p2 | 20 | present |
| 18 | 2 | 13 | 4 | 3 | 2020-03-25 | p2 | 15 | present |
| 19 | 2 | 13 | 4 | 3 | 2020-03-25 | p8 | 16 | absent |
| 20 | 2 | 13 | 4 | 3 | 2020-03-25 | p8 | 17 | absent |
| 21 | 2 | 13 | 4 | 3 | 2020-03-25 | p8 | 18 | present |
| 22 | 2 | 13 | 4 | 3 | 2020-03-25 | p8 | 19 | present |
| 23 | 2 | 13 | 4 | 3 | 2020-03-25 | p8 | 20 | present |
| 24 | 2 | 13 | 4 | 3 | 2020-03-25 | p8 | 15 | absent |
+-----+------+------+------+------+------------+------+-------+---------+
24 rows in set (0.00 sec)
I need predefined table result will be like this.
Please refer attached image. Only print present or absent under p1 , p2, upto p8.
Those result must be student id, cid, ttid,did, date based result.
Can anyone do this?

You want to create a pivot table result from your query. I would recommend use a case statement in your query to get the desire result.
Check pivot table in mysql
Check similar issue on stackoverflow

Related

Complex INSERT or UPDATE MariaDB tables with data from other MariaDB tables using JOIN or UNION

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

Is there a way to grab associated data from three different tables and display them?

I have four tables, three of which I need data from, as well as a fourth that stores how this data is associated. When running my query, I'm getting the wrong output and it seems like I'm not linking things up correctly, but I'm unsure as to how to do so. How do I link my joins to get the output I'm looking for?
So far, I've used
SELECT c.class, p.name, s.specialization
FROM players_classes pc
JOIN players p ON p.player_id=pc.player_id
JOIN classes c ON c.class_id=pc.class_id
JOIN specialization s ON s.spec_id=pc.spec_id
which references
CREATE TABLE players(
player_id INT UNSIGNED auto_increment PRIMARY KEY,
name VARCHAR(20)
)...;
CREATE TABLE classes(
class_id INT UNSIGNED auto_increment PRIMARY KEY,
class VARCHAR(20)
)...;
CREATE TABLE specializations(
spec_id INT UNSIGNED auto_increment PRIMARY KEY,
class_id INT UNSIGNED,
specialization VARCHAR(20)
)...;
but I'd like to be using this table in some way to display the information correctly linked, I'm just unsure how to do so:
CREATE TABLE players_classes(
pc_id INT UNSIGNED auto_increment PRIMARY KEY,
FOREIGN KEY(class_id) REFERENCES classes(class_id),
FOREIGN KEY(player_id) REFERENCES players(player_id),
FOREIGN KEY(spec_id) REFERENCES specializations(spec_id)
)...;
I'm expecting to be able to grab a player's name as well as their associated class and specialization, actual results are showing the values associated with 1/1/1 for each id, and so on and so forth.
edit: the data from players_classes is as follows
pc_id | class_id | player_id | spec_id |
1 3 1 8
2 12 2 35
3 2 3 6
etc.
therefore the expected result from this is
class | name | spec
paladin seranul holypa
hunter contherious marksmanship
priest unicorns holypr
I am instead getting
class | name | spec
warlock Affliction Affliction
warlock Grireaver Demonology
warlock Affliction Demonology
and so on throughout the table, listing combinations that do not exist within my players_classes table
specializations table
+---------+----------+----------------+
| spec_id | class_id | specialization |
+---------+----------+----------------+
| 1 | 1 | Affliction |
| 2 | 1 | Destruction |
| 3 | 1 | Demonology |
| 4 | 2 | Shadow |
| 5 | 2 | Discipline |
| 6 | 2 | HolyPr |
| 7 | 3 | Retribution |
| 8 | 3 | HolyPa |
| 9 | 3 | ProtectionPa |
| 10 | 4 | ProtectionWa |
| 11 | 4 | Arms |
| 12 | 4 | Fury |
| 13 | 5 | FrostMa |
| 14 | 5 | Fire |
| 15 | 5 | Arcane |
| 16 | 6 | vengeance |
| 17 | 6 | havoc |
| 18 | 7 | guardian |
| 19 | 7 | balance |
| 20 | 7 | feral |
| 21 | 7 | restorationDr |
| 22 | 8 | elemental |
| 23 | 8 | enhance |
| 24 | 8 | restorationSh |
| 25 | 9 | frostDk |
| 26 | 9 | blood |
| 27 | 9 | unholy |
| 28 | 10 | outlaw |
| 29 | 10 | assassin |
| 30 | 10 | subtlety |
| 31 | 11 | brewmaster |
| 32 | 11 | windwalker |
| 33 | 11 | mistweaver |
| 34 | 12 | BeastMaster |
| 35 | 12 | marksmanship |
| 36 | 12 | Survival |
+---------+----------+----------------+
classes table
+----------+-------------+
| class_id | class |
+----------+-------------+
| 1 | warlock |
| 2 | priest |
| 3 | paladin |
| 4 | warrior |
| 5 | mage |
| 6 | demonhunter |
| 7 | druid |
| 8 | shaman |
| 9 | deathknight |
| 10 | rogue |
| 11 | monk |
| 12 | hunter |
+----------+-------------+
players table
+-----------+--------------+
| player_id | name |
+-----------+--------------+
| 1 | Seranul |
| 2 | Contherious |
| 3 | Unicorns |
| 4 | Remereili |
| 5 | Affliction |
| 6 | Meowing |
| 7 | Brobot |
| 8 | Bagelsbbq |
| 9 | Rafusen |
| 10 | Taiboku |
| 11 | Yikes |
| 12 | Thunderblaze |
| 13 | Muo |
| 14 | Intz |
| 15 | Trunks |
| 16 | Kalphyte |
| 17 | Eyeoftheshoe |
| 18 | Amuhnet |
| 19 | Synkka |
| 20 | Affliction |
| 21 | Kts |
| 22 | Shadowdreams |
| 23 | Zahel |
| 24 | Azrama |
| 25 | Seranul |
| 26 | Momspaghetti |
| 27 | Ohki |
| 28 | Rafusen |
| 29 | Cindyy |
| 30 | Grireaver |
| 31 | Intz |
| 32 | lazy |
| 33 | missworld |
| 34 | Affliction |
| 35 | Amuhnet |
| 36 | eyeoftheshoe |
| 37 | sanctus |
| 38 | nozshelen |
| 39 | Contherious |
| 40 | messer |
| 41 | catathor |
| 42 | demonblaze |
| 43 | wrillett |
| 44 | raagnnar |
| 45 | xizi |
| 46 | nemesix |
| 47 | zeroskill |
| 48 | chikfillidan |
| 49 | tentenlol |
| 50 | unicorns |
| 51 | bubuhtide |
| 52 | ohki |
| 53 | azrama |
+-----------+--------------+
players_classes table
+-------+----------+-----------+---------+
| pc_id | class_id | player_id | spec_id |
+-------+----------+-----------+---------+
| 1 | 3 | 1 | 8 |
| 2 | 12 | 2 | 35 |
| 3 | 2 | 3 | 6 |
| 4 | 11 | 4 | 31 |
| 5 | 1 | 5 | 1 |
| 6 | 12 | 6 | 34 |
| 7 | 2 | 7 | 6 |
| 8 | 11 | 8 | 31 |
| 9 | 2 | 9 | 6 |
| 10 | 7 | 10 | 21 |
| 11 | 4 | 11 | 11 |
| 12 | 8 | 12 | 22 |
| 13 | 4 | 13 | 12 |
| 14 | 5 | 14 | 13 |
| 15 | 3 | 15 | 7 |
| 16 | 11 | 16 | 33 |
| 17 | 8 | 17 | 22 |
| 18 | 2 | 18 | 6 |
| 19 | 8 | 19 | 23 |
| 20 | 11 | 20 | 33 |
| 21 | 5 | 21 | 13 |
| 22 | 6 | 22 | 17 |
| 23 | 10 | 23 | 29 |
| 24 | 8 | 24 | 22 |
| 25 | 11 | 25 | 31 |
| 26 | 11 | 26 | 32 |
| 27 | 4 | 27 | 11 |
| 28 | 5 | 28 | 13 |
| 29 | 7 | 29 | 19 |
| 30 | 1 | 30 | 3 |
| 31 | 9 | 31 | 25 |
| 32 | 3 | 32 | 8 |
| 33 | 9 | 33 | 25 |
| 34 | 1 | 34 | 3 |
| 35 | 2 | 35 | 6 |
| 36 | 4 | 36 | 11 |
| 37 | 5 | 37 | 13 |
| 38 | 5 | 38 | 13 |
| 39 | 9 | 39 | 25 |
| 40 | 6 | 40 | 17 |
| 41 | 10 | 41 | 28 |
| 42 | 2 | 42 | 6 |
| 43 | 8 | 43 | 24 |
+-------+----------+-----------+---------+
You need to use the relationship table in your query:
SELECT c.class, p.name, s.specialization
FROM players_classes pc
JOIN players p ON p.player_id = pc.player_id
JOIN classes c ON c.class_id = pc.class_id
JOIN specializations s ON s.spec_id = pc.spec_id
DEMO
You shouldn't even have class_id and spec_id in the other tables, since these are many-to-many relationships, and those columns can only be used for one-to-one relationships.

mysql stored procedure to allocate confirmed quanitity according to ordered quantity?

How can I allocate confirmed quanitity according to ordered quantity?
Also I would like to save more or less quantity in one table.
Sqlfiddle: http://sqlfiddle.com/#!9/3fd97
Thanks guys!
I have 2 tables:
orders
+----------+--------------+-----+
| order_id | product_code | qty |
+----------+--------------+-----+
| 23 | A1 | 3 |
| 34 | A1 | 1 |
| 54 | A1 | 7 |
| 55 | A1 | 2 |
| 60 | B1 | 4 |
| 72 | B1 | 10 |
| 80 | C1 | 2 |
| 85 | E1 | 1 |
+----------+--------------+-----+
order_confirms
+-------+--------------+-----+
| oc_id | product_code | qty |
+-------+--------------+-----+
| 268 | A1 | 6 |
| 522 | A1 | 1 |
| 1093 | A1 | 2 |
| 1354 | A1 | 7 |
| 1580 | B1 | 1 |
| 1690 | C1 | 3 |
| 1820 | D1 | 1 |
+-------+--------------+-----+
expected results:
+----+----------+--------------+-----+-------+--------------+
| id | order_id | product_code | qty | oc_id | confirm_type |
+----+----------+--------------+-----+-------+--------------+
| 1 | 23 | A1 | 3 | 268 | ok |
| 2 | 34 | A1 | 1 | 268 | ok |
| 3 | 54 | A1 | 2 | 268 | ok |
| 4 | 54 | A1 | 1 | 522 | ok |
| 5 | 54 | A1 | 2 | 1093 | ok |
| 6 | 54 | A1 | 2 | 1354 | ok |
| 7 | 55 | A1 | 2 | 1354 | ok |
| 8 | NULL | A1 | 3 | 1354 | over |
| 9 | 60 | B1 | 1 | 1580 | ok |
| 10 | 60 | B1 | 3 | NULL | less |
| 11 | 72 | B1 | 10 | NULL | less |
| 12 | 80 | C1 | 2 | 1690 | ok |
| 13 | NULL | C1 | 1 | 1690 | over |
| 14 | NULL | D1 | 1 | 1820 | over |
| 15 | 85 | E1 | 1 | NULL | less |
+----+----------+--------------+-----+-------+--------------+

not able to fetch date value using subqueries mysql

i have two tables called
1 table smartpos.pos_order_Id
+---------+--------------+---------+--------+--------------+----------------+------------+
| orderId | restaurantId | tableId | closed | customerName | customerNumber | dateorderd |
+---------+--------------+---------+--------+--------------+----------------+------------+
| 7 | 14 | 0 | yes | | | 21/03/2018 |
| 8 | 14 | 0 | yes | | | 21/03/2018 |
| 9 | 14 | 0 | no | | | 20/03/2018 |
| 10 | 14 | 0 | yes | soumya | 1234567890 | 21/03/2018 |
| 11 | 14 | 0 | yes | | | 21/03/2018 |
| 12 | 14 | 0 | yes | | | 21/03/2018 |
| 13 | 14 | 0 | yes | | | 21/03/2018 |
| 14 | 14 | 0 | yes | | | 20/03/2018 |
| 15 | 14 | 0 | no | | | 22/03/2018 |
+---------+--------------+---------+--------+--------------+----------------+------------+
2smartpos.pos_invoice
+---------------+---------+----------+-------------+-------------+------------+-------------+---------------+
| invoiceNumber | orderId | totalAmt | discountAmt | totalTaxAmt | grandTotal | paymentmode | paymentrefNum |
+---------------+---------+----------+-------------+-------------+------------+-------------+---------------+
| 1 | 7 | 200 | 34 | 46 | 212 | Cash | |
| 2 | 10 | 1200 | 200 | 280 | 1280 | Cash | |
| 3 | 1 | 720 | 34 | 120 | 806 | Cash | |
| 4 | 12 | 240 | 34 | 58 | 264 | Cash | |
| 5 | 13 | 330 | 32 | 83 | 381 | Cash | |
| 6 | 14 | 80 | 2 | 22 | 100 | Cash | |
+---------------+---------+----------+-------------+-------------+------------+-------------+---------------+
i want to fetch invoice details using restaurantId and two dates by providing restaurantId and dates as follows
select inv.invoiceNumber ,inv.totalAmt,inv.discountAmt,inv.totalTaxAmt,inv.grandTotal,i.dateorderd from smartpos.pos_invoice inv,smartpos.pos_order_Id i where inv.invoiceNumber in (select invv.invoiceNumber from smartpos.pos_invoice invv where invv.orderId in(select ii.orderId from smartpos.pos_order_Id ii where ii.closed='yes' and ii.restaurantId=14 and STR_TO_DATE(dateorderd,'%d/%m/%Y') between STR_TO_DATE('20/03/2018','%d/%m/%Y') and STR_TO_DATE('21/03/2018','%d/%m/%Y'))) group by inv.invoiceNumber ;
out put:
+---------------+----------+-------------+-------------+------------+------------+
| invoiceNumber | totalAmt | discountAmt | totalTaxAmt | grandTotal | dateorderd |
+---------------+----------+-------------+-------------+------------+------------+
| 1 | 200 | 34 | 46 | 212 | NULL |
| 2 | 1200 | 200 | 280 | 1280 | NULL |
| 4 | 240 | 34 | 58 | 264 | NULL |
| 5 | 330 | 32 | 83 | 381 | NULL |
| 6 | 80 | 2 | 22 | 100 | NULL |
+---------------+----------+-------------+-------------+------------+------------+
but when i run above query it gives null values , how to fetch the date as well?
Its difficult to understand what you wrote but i think you query is much simple than it seems, try to use this approach :
select
inv.invoiceNumber ,
inv.totalAmt,
inv.discountAmt,
inv.totalTaxAmt,
inv.grandTotal,
i.dateorderd
from
smartpos.pos_invoice inv,
smartpos.pos_order_Id i
where
inv.orderId = i.orderId
and
i.closed='yes'
and
i.restaurantId=14
and
STR_TO_DATE(dateorderd,'%d/%m/%Y') between STR_TO_DATE('20/03/2018','%d/%m/%Y') and STR_TO_DATE('21/03/2018','%d/%m/%Y')
group by
inv.invoiceNumber;

transfer data from other table and set field name as value [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
i have a query problem
i have a table
-------------------------------------------------------------------------------
| coding | prov | kota | kecamatan | kode | reg | etd_reg | oke | etd_oke | yes |
-------------------------------------------------------------------------------
| 1 | a | a_1 | a_1_1 | A | 20 | 1 -2 | 22 | 2 -3 | 25 |
| 2 | a | a_1 | a_1_2 | A | 19 | 2 -3 | 21 | 3 -5 | 24 |
| 3 | a | a_1 | a_1_3 | A | 21 | 3 -5 | 23 | 5 -7 | 26 |
| 4 | a | a_2 | a_2_1 | A | 22 | 1 -2 | 24 | 2 -3 | 27 |
| 5 | a | a_2 | a_2_2 | A | 20 | 2 -4 | 22 | 4 -6 | 25 |
| 6 | b | b_1 | b_1_1 | B | 22 | 7 -9 | - | - | - |
| 7 | b | b_1 | b_1_2 | B | 15 | 4 -5 | 17 | 6 -7 | 20 |
| 8 | b | b_2 | b_2_1 | B | 27 | 2 -4 | - | - | - |
| 9 | b | b_2 | b_2_2 | B | 11 | 2 -3 | 13 | 3 -5 | 16 |
| 10 | b | b_2 | b_2_3 | B | 23 | 1 -2 | 25 | 2 -3 | - |
-------------------------------------------------------------------------------
i want to transfer the data of that table to a new table like below
---------------------------------------------------------------------
| id | coding | prov | kota | kecamatan | kode | paket | cost | etd |
---------------------------------------------------------------------
| 1 | 1 | a | a_1 | a_1_1 | A | reg | 20 | 1 -2 |
| 2 | 1 | a | a_1 | a_1_1 | A | oke | 22 | 2 -3 |
| 3 | 1 | a | a_1 | a_1_1 | A | yes | 25 | 1 |
| 4 | 2 | a | a_1 | a_1_2 | A | reg | 19 | 2 -3 |
| 5 | 2 | a | a_1 | a_1_2 | A | oke | 21 | 3 -5 |
| 6 | 2 | a | a_1 | a_1_2 | A | yes | 24 | 1 |
| 7 | 3 | a | a_1 | a_1_3 | A | reg | 21 | 3 -5 |
| 8 | 3 | a | a_1 | a_1_3 | A | oke | 23 | 5 -7 |
| 9 | 3 | a | a_1 | a_1_3 | A | yes | 26 | 1 |
| 10 | 4 | a | a_2 | a_2_1 | A | reg | 22 | 1 -2 |
| 11 | 4 | a | a_2 | a_2_1 | A | oke | 24 | 2 -3 |
| 12 | 4 | a | a_2 | a_2_1 | A | yes | 27 | 1 |
| 10 | 5 | a | a_2 | a_2_2 | A | reg | 20 | 2 -4 |
| 11 | 5 | a | a_2 | a_2_2 | A | oke | 22 | 4 -6 |
| 12 | 5 | a | a_2 | a_2_2 | A | yes | 25 | 1 |
| 13 | 6 | b | b_1 | b_1_1 | B | reg | 22 | 7 -9 |
| 14 | 7 | b | b_1 | b_1_2 | B | reg | 15 | 4 -5 |
| 15 | 7 | b | b_1 | b_1_2 | B | oke | 17 | 6 -7 |
| 16 | 7 | b | b_1 | b_1_2 | B | yes | 20 | 1 |
| 17 | 8 | b | b_2 | b_2_1 | B | reg | 27 | 2 -4 |
| 14 | 9 | b | b_2 | b_2_2 | B | reg | 11 | 2 -3 |
| 15 | 9 | b | b_2 | b_2_2 | B | oke | 13 | 3 -5 |
| 16 | 9 | b | b_2 | b_2_2 | B | yes | 16 | 1 |
| 17 | 10 | b | b_2 | b_2_3 | B | reg | 23 | 1 -2 |
| 19 | 10 | b | b_2 | b_2_3 | B | oke | 25 | 2 -3 |
---------------------------------------------------------------------
actual data is more 5000 row.. is posible to transfer it??
if the value of field is - that not need to make new row
can someone help??
First you'll have to create the new table. EG:
CREATE TABLE RESULT(
id int NOT NULL AUTO_INCREMENT, coding int, prov varchar(1), kota varchar(3),
kecamatan varchar(5), kode varchar(1), paket varchar(3), cost int,
etd varchar(4), PRIMARY KEY (id));
Note that this table will contain the cost as an int (which is much better than a string, right?) so you'll have to convert it later (as you'll see in the query below).
Then you have to insert the data in this new table and union based on the different types of kode. EG:
INSERT INTO RESULT (coding, prov, kota, kecamatan, kode, paket, cost, etd)
SELECT coding, prov, kota, kecamatan, kode, 'reg' paket, reg+0 cost, etd_reg etd
FROM t
UNION ALL
SELECT coding, prov, kota, kecamatan, kode, 'oke' paket, oke+0 cost, etd_oke etd
FROM t
WHERE oke != '-'
UNION ALL
SELECT coding, prov, kota, kecamatan, kode, 'yes' paket, yes+0 cost, 1 etd
FROM t
WHERE yes != '-'
You can see this in action in this fiddle.