I want to increase count in count column one by one if the same Id is found in records.
Don't consider about auto increment of Id.
MySQL version 8.0 and you can order by name column.
I tried but not getting any solution.
My table Employee looks like this
+------+-------+-------+
| Id | Name |address|
+------+-------+-------+
| 111 | Sam | xyz |
| 112 | Mike | xyz |
| 113 | Carol | xyz |
| 113 | Bob | xyz |
| 113 | John | xyz |
| 116 | Adam | xyz |
| 116 | David | xyz |
+------+-------+-------+
I want to run sql query which will give output like this, if Id is same increase count
+------+-------+-------+
| Id | Name | count |
+------+-------+-------+
| 111 | Sam | 1 |
| 112 | Mike | 1 |
| 113 | Carol | 1 |
| 113 | Bob | 2 |
| 113 | John | 3 |
| 114 | Adam | 1 |
| 114 | David | 2 |
+------+-------+-------+
The ROW_NUMBER() function can generate your current count column, more or less:
SELECT
Id,
Name,
ROW_NUMBER() OVER (PARTITION BY Id ORDER BY Name) count
FROM yourTable
ORDER BY
Id,
Name;
Note that ROW_NUMBER requires MySQL 8+.
I have a table called transactions that looks like this:
transactions:
| id | PartNumber | Quantity |
I know that I can use the COUNT property in MySQL, which would give me the the duplicate part numbers in a new column called total_quantity:
SELECT COUNT(transactions.id) AS total_quantity
FROM transactions
GROUP BY transactions.PartNumber
However, now I already have an existing quantity column and want to compute a new count quantity taking into account the previous one as well and updating it in the existing quantity column
What's the most efficient way of doing this?
For example: I want to go from this:
transactions
| id | PartNumber | Quantity |
| 1 | 123 | 1 |
| 2 | 124 | 2 |
| 3 | 125 | 2 |
| 4 | 124 | 2 |
| 5 | 124 | 3 |
| 6 | 126 | 4 |
| 7 | 125 | 1 |
| 8 | 127 | 2 |
To this:
transactions
| id | PartNumber | Quantity |
| 1 | 123 | 1 |
| 2 | 124 | 7 |
| 3 | 125 | 3 |
| 4 | 126 | 4 |
| 5 | 127 | 2 |
You can use this sql request :
SELECT PartNumber, sum(Quantity) as 'SumQuantity' FROM transactions GROUP BY PartNumber
It will gives you sometings like this :
transactions
| PartNumber | SumQuantity |
| 123 | 1 |
| 124 | 7 |
| 125 | 3 |
| 126 | 4 |
| 127 | 2 |
You can find a code sample on SQL Fiddle
I am new to SQL, and I have a problem in my practice program,
assume I have a table below:
----------------------------------------
job_Name | member1 | member2 | member3 | ...The number of members is unknown
-----------------------------------------
ABC | 10 | 20 | null | ...The value is the price which paid
-----------------------------------------
bcd | null | 20 | null | ...to the member. that mean member1
-----------------------------------------
asd | null | 20 | 10 | ...and member2 finish this job
-----------------------------------------
qwe | 10 | 20 | 10 | ...togeter
What I want to get below:
--------------------------------------
job_Name | members |
--------------------------------------
ABC | member1,member2 |
--------------------------------------
bcd | member2 |
--------------------------------------
asd | member2,member3 |
--------------------------------------
qwe | member1,member2,member3 |
This is easy to get on excel with Visual Basic
In Mysql, I have no idea about that.
I have following tables to manage purchase and issues of items.
item Table
+---------+-----------+
| item_id | item_name |
+---------+-----------+
| 500 | A4 |
| 501 | A5 |
| 502 | B5 |
| 503 | B4 |
| 504 | A3 |
+---------+-----------+
supplier Table
+-------------+---------------+
| sup_id | supplier_name |
+-------------+---------------+
| 1 | ABC Suppliers |
| 2 | DEF Suppliers |
| 3 | GHI Suppliers |
+-------------+---------------+
officer Table
+------------+--------------+
| officer_id | officer_name |
+------------+--------------+
| 1 | Jhon |
| 2 | William |
| 3 | Ken |
| 4 | Robert |
+------------+--------------+
purchase / issue table
+-----------+---------+---------+---------+------------+-----+-------------+
| update_id | bill_no | sup_id | item_id | date | qty | type |
+-----------+---------+---------+---------+------------+-----+-------------+
| 1000 | 10 | 1 | 500 | 2018-11-01 | 50 | purchase |
| 1001 | 40 | 1 | 500 | 2018-11-02 | 25 | purchase |
| 1002 | 32 | 2 | 500 | 2018-11-04 | 10 | issue |
| 1003 | 25 | 3 | 500 | 2018-11-05 | 12 | issue |
| 1004 | 14 | 1 | 500 | 2018-11-08 | 22 | purchase |
| 1005 | 55 | 2 | 501 | 2018-11-09 | 10 | purchase |
| 1006 | 30 | 2 | 502 | 2018-11-10 | 40 | purchase |
+-----------+---------+---------+---------+------------+-----+-------------+
02) purchase / issue table holds the purchase details and issue details by mentioning the "type" at the end of table.
03) I need to get the following output.
+-----------+------------------------------+------------+-----+------------+
| item_name | supplier_name / officer_name | date | qty |type |
+-----------+------------------------------+------------+-----+------------+
| A4 | ABC Suppliers | 2018-11-01 | 50 | purchase |
| A4 | ABC Suppliers | 2018-11-02 | 25 | purchase |
| A4 | William | 2018-11-04 | 10 | issue |
| A4 | Ken | 2018-11-05 | 12 | issue |
| A4 | ABC Suppliers | 2018-11-08 | 22 | purchase |
| A5 | DEF Suppliers | 2018-11-09 | 10 | purchase |
| B5 | DEF Suppliers | 2018-11-10 | 40 | purchase |
+-----------+------------------------------+------------+-----+------------+
03) I used the following query
select item.item_name,
supplier.supplier_name,
purchase.date,
purchase.qty,
purchase.type
from purchase
join item on item.item_id = purchase.item_id
where supplier.sup_id in (select sup_id from supplier)
04) But I did't get the desired output. I can not understand what I am going wrong. Can anyone help me ?
In your SELECT clause, you are referring to supplier table, without joining to that table. Now, it seems that you want to show officer_name when the purchase type is issue, else supplier_name when it is purchase.
We can do a LEFT JOIN to both the tables, and use CASE .. WHEN to determine the respective name.
I have removed the WHERE .. IN subquery, which does not seem to serve any purpose here.
select item.item_name,
CASE purchase.type
WHEN 'purchase' THEN supplier.supplier_name
WHEN 'issue' THEN officer.officer_name
END AS `supplier_name_officer_name`
purchase.date,
purchase.qty,
purchase.type
from purchase
join item on item.item_id = purchase.item_id
left join supplier on purchase.sup_id = supplier.sup_id
left join officer on purchase.sup_id = officer.officer_id
You need to join the supplier just as you did for the item.
And I see no point in the where clause at all.
I have a table with the following fields.
Account,DeviceID,timestamp,odometer
The first three fields are Primary Key
Some deviceID are cloned in different account.
Now those clones have corrupted odometer.
Account,DeviceID,timestamp,odometer
1 A 001 145
1 A 002 147
1 A 003 148
2 A 001 145
2 A 002 NULL
2 A 003 0
Device A in account 2 is the clon of device A in account 1.
So, how I can clone the odometer values from account 1 and device A to the same timestamp of device A in account 2?
I need to create a store procedure that I can feed whit three paramaters, OriginalAccount, CloneAccount, DeviceID, to fix one by one all the clones.
Edit:
If I have the following data
+---------+-----------+-----------+----------+
| account | device_id | timestamp | odometer |
+---------+-----------+-----------+----------+
| 1 | A | 001 | 145 |
| 1 | A | 002 | 147 |
| 1 | A | 003 | 148 |
| 2 | A | 001 | 145 |
| 2 | A | 002 | NULL |
| 2 | A | 003 | 0 |
After call the store procedure the table must look like this
+---------+-----------+-----------+----------+
| account | device_id | timestamp | odometer |
+---------+-----------+-----------+----------+
| 1 | A | 001 | 145 |
| 1 | A | 002 | 147 |
| 1 | A | 003 | 148 |
| 2 | A | 001 | 145 |
| 2 | A | 002 | 147 |
| 2 | A | 003 | 148 |
Schema
create table thing7
( account int not null,
device_id varchar(10) not null,
`timestamp` int(3) zerofill not null, -- not a great column name btw (it is a keyword, not a reserved word)
odometer int null,
primary key(account,device_id,timestamp)
);
insert thing7(account,device_id,timestamp,odometer) values
(1,'A',1,145),
(1,'A',2,147),
(1,'A',3,148),
(2,'A',1,145),
(2,'A',2,NULL),
(2,'A',3,0);
Look at Data at start:
select * from thing7;
+---------+-----------+-----------+----------+
| account | device_id | timestamp | odometer |
+---------+-----------+-----------+----------+
| 1 | A | 001 | 145 |
| 1 | A | 002 | 147 |
| 1 | A | 003 | 148 |
| 2 | A | 001 | 145 |
| 2 | A | 002 | NULL |
| 2 | A | 003 | 0 |
+---------+-----------+-----------+----------+
Stored Procedure
drop procedure if exists cloneIt;
DELIMITER $$
create procedure cloneIt
( srcAcct int, -- the data of the source to be copied
destAcct int, -- the target to receive it
device varchar(10)
)
BEGIN
-- clean out any old junk in case src does not match dest
-- this is due to edge conditions where dest has more rows than src, and you said you want a clone
delete from thing7 where account=destAcct and device_id=device;
-- now clone it
insert thing7(account,device_id,`timestamp`,odometer)
select destAcct,device_id,`timestamp`,odometer
from thing7
where account=srcAcct and device_id=device;
END
$$
DELIMITER ;
Delimiters set up wrong can waste hours. Have them or look into their use.
clone 1 to 7:
call cloneIt(1,7,'A');
Test one edge condition: dest has more info than src
insert thing7(account,device_id,timestamp,odometer) values
(14,'A',1,145),
(14,'A',2,147),
(14,'A',3,148),
(14,'A',4,199);
14 now had 4 rows
call cloneIt(1,14,'A');
Look at results:
select * from thing7;
+---------+-----------+-----------+----------+
| account | device_id | timestamp | odometer |
+---------+-----------+-----------+----------+
| 1 | A | 001 | 145 |
| 1 | A | 002 | 147 |
| 1 | A | 003 | 148 |
| 2 | A | 001 | 145 |
| 2 | A | 002 | NULL |
| 2 | A | 003 | 0 |
| 7 | A | 001 | 145 |
| 7 | A | 002 | 147 |
| 7 | A | 003 | 148 |
| 14 | A | 001 | 145 |
| 14 | A | 002 | 147 |
| 14 | A | 003 | 148 |
+---------+-----------+-----------+----------+
cloneIt works. It creates exact clones