Copying data table to table without column specification in query - mysql

I have a problem to copy data from one table to another table. There is lots of solution found but my problem is something different. I have two tables sku and shipping_skudetails. I have to copy data form shipping_skudetails to sku, for this m doing this,
INSERT INTO test.sku
SELECT SkuDetailsId as id,
sku,
seller_id as sellerID,
itemName as name,
itemLength as length,
itemWidth as width,
itemHeight as height,
itemWeight as weight,
modeType_id as mode
FROM testdb.shipping_skudetails;
+----+---------+----------+-------------+--------+-------+--------+--------+------+
| id | sku | sellerID | name | length | width | height | weight | mode |
+----+---------+----------+-------------+--------+-------+--------+--------+------+
| 1 | Sample | 1 | SampleItem | 1 | 1 | 1 | 1 | 1 |
| 2 | Sample | 2 | Sample1 | 1 | 1 | 1 | 1 | 1 |
| 3 | SDGS046 | 1 | Shivaprasad | 1 | 1 | 3 | 1 | 2 |
+----+---------+----------+-------------+--------+-------+--------+--------+------+
test.sku
+--------------+---------+-----------+-------------+------------+------------+------------+-----------+
| SkuDetailsId | sku | seller_id | itemName | itemLength | itemWeight | itemHeight | itemWidth |
+--------------+---------+-----------+-------------+------------+------------+------------+-----------+
| 1 | Sample | 1 | SampleItem | 1 | 1.000 | 1 | 1 |
| 2 | Sample | 2 | Sample1 | 1 | 1.000 | 1 | 1 |
| 3 | SDGS046 | 1 | Shivaprasad | 1 | 3.000 | 1 | 1 |
+--------------+---------+-----------+-------------+------------+------------+------------+-----------+
testdb.shipping_skudetails
now the problem is values are copied into different columns,
(like : shipping_skudetails.itemWeight copied into test.width)
I want copy data column to column. not by arrange column in query.
I want a query that copy data by identifying column name.

try this
INSERT INTO shyplite.sku (id, sku, sellerID, name, length, width, height, weight, mode)
SELECT SkuDetailsId as id,
sku,
seller_id as sellerID,
itemName as name,
itemLength as length,
itemWidth as width,
itemHeight as height,
itemWeight as weight,
modeType_id as mode
FROM shyplitelivedb.shipping_skudetails;

Related

How to get count on a separte column?

I have the following table which I will like to get the count of items with the same product name on a separate column
Here is the table
+-----------+-------------+-------+------+
| ProductID | ProductName | Price | URL |
+-----------+-------------+-------+------+
| 1 | Book | 2 | url1 |
| 2 | Pen | 1 | url2 |
| 3 | pencil | 0.5 | url3 |
| 4 | Book | 2 | url1 |
+-----------+-------------+-------+------+
And I will like get the following from the table
+-----------+-------------+-------+------+-------+
| ProductID | ProductName | Price | URL | Count |
+-----------+-------------+-------+------+-------+
| 1 | Book | 2 | url1 | 2 |
| 2 | Pen | 1 | url2 | 1 |
| 3 | pencil | 0.5 | url3 | 1 |
+-----------+-------------+-------+------+-------+
The reason why I need this is because the items need to be rendered on an external application with the count. I do not know how to get the count on another column.
This looks like aggregation:
select min(ProductID) as ProductID, ProductName, Price, URL, COUNT(*)
from t
group by ProductName, Price, URL;
SELECT ProductID
,ProductName
,Price
,URL
,COUNT(ProductName) AS CountProduct
FROM products
GROUP BY ProductName

SQL:pivot: convert each row into many rows depends on a condition

I have been trying to solve this but could not figure it out.
so this is the table and more columns could be added:
+------------+-----------+------------+-----------+------------+------------+
| patient_ID | code_ID | happy? | mad? | smiling? | scared? |
+------------+-----------+------------+-----------+------------+------------+
| kkk | kgg | 1 | 0 | 1 | 1 |
+------------+-----------+------------+-----------+------------+------------+
| 2k2 | 2g2 | 0 | 1 | 0 | 1 |
+------------+-----------+------------+-----------+------------+------------+
So if the value is one then I will display it in a different row but with the value being the column header itself
My boss suggested using pivot to solve this but I am open to any way.
output should be like this:
+------------+-----------+------------+
| patient_ID | code_ID | segment |
+------------+-----------+------------+
| kkk | kgg | happy? |
+------------+-----------+------------+
| kkk | kgg | smiling? |
+------------+-----------+------------+
| kkk | kgg | scared? |
+------------+-----------+------------+
| 2k2 | 2g2 | mad? |
+------------+-----------+------------+
| 2k2 | 2g2 | scared? |
+------------+-----------+------------+
One method is union all:
select patient_id, code_id, 'happy?' as segment
from t
where `happy?` = 1
union all
select patient_id, code_id, 'mad?'
from t
where `mad?` = 1
union all
select patient_id, code_id, 'smiling?'
from t
where `smiling?` = 1
union all
select patient_id, code_id, 'scared?'
from t
where `scared?` = 1;

mysql pivot using column and row numbers

I am stuck in this situation where I need to use Row Number and Column Number values from table's columns to derive the output mentioned below. I have tried everything - if/else, case when/then but not helping.
Any help/suggestions are really appreciated!
Here is a mocked up sample data present in db table -
+--------+--------+--------+----------+-------------+
| Record | ColNbr | RowNbr | ColTitle | CellContent |
+--------+--------+--------+----------+-------------+
| 1 | 1 | 1 | Unit | sqf |
| 1 | 1 | 2 | Unit | cm |
| 1 | 2 | 1 | Desc | roof |
| 1 | 2 | 2 | Desc | rod |
| 1 | 3 | 1 | Material | concrete |
| 1 | 3 | 2 | Material | steel |
| 1 | 4 | 1 | Quantity | 100 |
| 1 | 4 | 2 | Quantity | 12 |
| 1 | 1 | 1 | Unit | liter |
| 1 | 1 | 2 | Unit | ml |
| 1 | 2 | 1 | Desc | bowl |
| 1 | 2 | 2 | Desc | plate |
| 1 | 3 | 1 | Material | plastic |
| 1 | 3 | 2 | Material | glass |
| 1 | 4 | 1 | Quantity | 2 |
| 1 | 4 | 2 | Quantity | 250 |
+--------+--------+--------+----------+-------------+
Expected Output -
+--------+--------+--------+----------+-------------+
| Record | Unit | Desc | Material | Quantity |
+--------+--------+--------+----------+-------------+
| 1 | sqf | roof | concrete | 100 |
| 1 | cm | rod | steel | 12 |
| 2 | liter | bowl | plastic | 2 |
| 2 | ml | plate | glass | 250 |
+--------+--------+--------+----------+-------------+
If your actual data is like that, I suggest that you consider to separate the data to; for example, 4 different tables (unit,description,material & a table to store all that ids+quantity). The former 3 tables will store the prerequisite info that get minor updates throughout time and the last table will store all the quantity records. Let's say your tables will look something like this:
CREATE TABLE `Unit` (
unit_id INT,
unit_name VARCHAR(50));
+---------+-----------+
| unit_id | unit_name |
+---------+-----------+
| 1 | sqf |
| 2 | cm |
| 3 | liter |
| 4 | ml |
+---------+-----------+
CREATE TABLE `Description` (
desc_id INT,
desc_name VARCHAR(50));
+---------+-----------+
| desc_id | desc_name |
+---------+-----------+
| 1 | roof |
| 2 | rod |
| 3 | bowl |
| 4 | plate |
+---------+-----------+
CREATE TABLE `Material` (
mat_id INT,
mat_name VARCHAR(50));
+--------+----------+
| mat_id | mat_name |
+--------+----------+
| 1 | concrete |
| 2 | steel |
| 3 | plastic |
| 4 | glass |
+--------+----------+
CREATE TABLE `Records` (
unit_id INT,
desc_id INT,
mat_id INT,
quantity DECIMAL(14,4));
+---------+---------+--------+----------+
| unit_id | desc_id | mat_id | Quantity |
+---------+---------+--------+----------+
| 1 | 1 | 1 | 100 |
| 2 | 2 | 2 | 12 |
| 3 | 3 | 3 | 2 |
| 4 | 4 | 4 | 250 |
+---------+---------+--------+----------+
Then you insert the data accordingly.
Anyhow, for your existing data example, it could be done but there are some concern over whether the unit+desc+material+quantity matching are correct. The only way I can maybe at least think that it's correctly matched is by giving all of the query a similar ORDER BY clause. Hence, the following:
SELECT A.record,A.unit,B.Desc,C.Material,D.Quantity FROM
(SELECT #rn:=#rn+1 AS record,CASE WHEN coltitle='unit' THEN cellcontent END AS Unit
FROM yourtable, (SELECT #rn :=0 ) v
HAVING unit IS NOT NULL
ORDER BY colnbr) A LEFT JOIN
(SELECT #rn1:=#rn1+1 AS record,CASE WHEN coltitle='Desc' THEN cellcontent END AS `Desc`
FROM yourtable, (SELECT #rn1 :=0 ) v
HAVING `Desc` IS NOT NULL
ORDER BY colnbr) B ON a.record=b.record LEFT JOIN
(SELECT #rn2:=#rn2+1 AS record,CASE WHEN coltitle='material' THEN cellcontent END AS Material
FROM yourtable, (SELECT #rn2:=0 ) v
HAVING Material IS NOT NULL
ORDER BY colnbr) C ON a.record=c.record LEFT JOIN
(SELECT #rn3:=#rn3+1 AS record,CASE WHEN coltitle='Quantity' THEN cellcontent END AS Quantity
FROM yourtable, (SELECT #rn3:=0 ) v
HAVING Quantity IS NOT NULL
ORDER BY colnbr) D ON a.record=d.record;
The idea here is to make a sub-query based on COLTITLE then assign a numbering/ranking (#rn,#rn1,#rn2,#rn3) variable to each of the sub-query and join them up using LEFT JOIN. Now, this experiment works to exactly return the output that you need but its not a definite answer because there are some part that is questionable especially on matching the combination correctly. Hopefully, this will give you some idea.

How to create a new table with "default value column" depending on the tables I select from?

Let's say, I have two tables, one for "sales" and another for "stock".
Sales table would be like this:
-------------------------
| location | item | qty |
------------------------
| 1 | 11 | 1 |
| 2 | 12 | 1 |
-------------------------
And stock table would look like this:
-------------------------
| location | item | qty |
------------------------
| 1 | 11 | 90 |
| 2 | 12 | 70 |
-------------------------
I want to insert the data from both tables for items "11" and "12" in a new table, and separating between them with "sales" and "stock" in a new column called "type" like this:
---------------------------------
| type | location | item | qty |
---------------------------------
| sales | 1 | 11 | 1 |
| sales | 2 | 12 | 1 |
| stock | 1 | 11 | 90 |
| stock | 2 | 12 | 70 |
---------------------------------
Any ideas?
insert into table3 (thetype,location,item,qty) select 'sales',location,item,qty
from sales where item in (11,12)
insert into table3 (thetype,location,item,qty) select 'stock',location,item,qty
from stock where item in (11,12)

Mysql rows to columns

I have the following mysql table:
+----+-----+-----+--------+
| id | sid | tid | val |
+----+-----+-----+--------+
| 1 | 1 | 1 | square |
| 2 | 1 | 2 | big |
| 3 | 1 | 3 | red |
| 4 | 2 | 1 | circle |
| 5 | 2 | 2 | small |
| 6 | 2 | 3 | yellow |
+----+-----+-----+--------+
And I would need a query to get the following results:
+-----+--------+-------+--------+
| sid | figure | size | colour |
+-----+--------+-------+--------+
| 1 | square | big | red |
| 2 | circle | small | yellow |
+-----+--------+-------+--------+
Any ideas?
Thanks.
You didn't provide any details about how you determine the new column names but based on your data I am guessing that it is based on the values in the tid column. You can use an aggregate function with a case expression to get the result:
select
sid,
max(case when tid = 1 then val end) figure,
max(case when tid = 2 then val end) size,
max(case when tid = 3 then val end) color
from yourtable
group by sid;
See SQL Fiddle with Demo