How To select Query - mysql

I have a table view named as tes.
+--------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+-------+
| id_oba | int(11) | NO | | NULL | |
| nama_obat | varchar(120) | NO | | NULL | |
| satuan | varchar(20) | YES | | NULL | |
| stok_awal | int(5) | YES | | NULL | |
| persediaan | int(5) | YES | | NULL | |
| nama_suplier | varchar(30) | NO | | NULL | |
| pemakaian | int(4) | NO | | NULL | |
| tanggal | date | YES | | NULL | |
+--------------+--------------+------+-----+---------+-------+
When I execute:
select id_oba, nama_obat, satuan, stok_awal, sum(pemakaian) AS pemakaian, persediaan,
from tes group by id_oba;
I get this result:
+--------+-----------+--------+-----------+-----------+------------+
| id_oba | nama_obat | satuan | stok_awal | pemakaian | persediaan |
+--------+-----------+--------+-----------+-----------+------------+
| 1 | Paramex | Botol | 30 | 40 | 5 |
| 2 | Oskadon | Botol | 30 | 41 | 27 |
| 3 | Lindon | Botol | 31 | 30 | 4 |
+--------+-----------+--------+-----------+-----------+------------+
How can I modify my query to get the result like this?
+--------+-----------+--------+-----------+-----------+------------+-------+
| id_oba | nama_obat | satuan | stok_awal | pemakaian | persediaan | total |
+--------+-----------+--------+-----------+-----------+------------+-------+
| 1 | Paramex | Botol | 30 | 40 | 5 | 45 |
| 2 | Oskadon | Botol | 30 | 41 | 27 | 67 |
| 3 | Lindon | Botol | 31 | 30 | 4 | 34 |
+--------+-----------+--------+-----------+-----------+------------+-------+
Please help me with my problem.
Thanks.

Change like this
select id_oba, nama_obat, satuan, stok_awal, sum(pemakaian) AS pemakaian,
persediaan,(sum(pemakaian)+persediaan) as total from tes group by id_oba;

Related

How do I prevent combinations or permutaions in a joining table?

I have the following tables in MySQL Workbench:
MdV, MdV_has_Chain and Chain.
describe MdV
+------------+--------------+------+-----+-------------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+-------------+-------+
| MdVID | varchar(255) | NO | PRI | NULL | |
| Source | longtext | YES | | NULL | |
| Chains | int unsigned | NO | | 0 | |
| CampaignID | varchar(255) | NO | MUL | No_Campaign | |
+------------+--------------+------+-----+-------------+-------+
describe MdV_has_Chain
+-----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+-------+
| MdVID | varchar(255) | NO | PRI | NULL | |
| ChainID | varchar(255) | NO | PRI | NULL | |
| Chain_Num | int unsigned | NO | | NULL | |
+-----------+--------------+------+-----+---------+-------+
describe Chain
+-----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+-------+
| ChainID | varchar(255) | NO | PRI | NULL | |
| Positions | int unsigned | NO | | 0 | |
| VectorID | varchar(255) | NO | | NULL | |
+-----------+--------------+------+-----+---------+-------+
Here they are in the EER diagram.
Currently the tables of interest hold the following mock data:
select * from mdv;
+-------------+-----------------+--------+-------------+
| MdVID | Source | Chains | CampaignID |
+-------------+-----------------+--------+-------------+
| ITS058-M152 | | 1 | C7 |
| ITS058-M182 | | 2 | No_Campaign |
| ITS058-M244 | Rational Design | 1 | C16 |
| ITS058-M253 | Rational Design | 2 | C17 |
| ITS058-M258 | | 1 | No_Campaign |
| TEST | | 0 | No_Campaign |
+-------------+-----------------+--------+-------------+
select * from mdv_has_chain;
+-------------+------------+----------------+
| MdVID | ChainID | chain_position |
+-------------+------------+----------------+
| ITS058-M152 | ITS058-Ch1 | 1 |
| ITS058-M182 | ITS058-Ch2 | 1 |
| ITS058-M182 | ITS058-Ch3 | 2 |
| ITS058-M244 | ITS058-Ch4 | 1 |
| ITS058-M253 | Ch1 | 2 |
| ITS058-M253 | ITS058-Ch5 | 1 |
| ITS058-M258 | ITS058-Ch6 | 1 |
+-------------+------------+----------------+
select * from chain;
+------------+-----------+-------------+
| ChainID | Positions | VectorID |
+------------+-----------+-------------+
| Ch1 | 2 | T343 |
| ITS058-Ch1 | 7 | ITS058-V240 |
| ITS058-Ch2 | 7 | ITS058-V278 |
| ITS058-Ch3 | 1 | R208 |
| ITS058-Ch4 | 6 | ITS058-V352 |
| ITS058-Ch5 | 7 | ITS058-V361 |
| ITS058-Ch6 | 6 | ITS058-V366 |
+------------+-----------+-------------+
To see what chains each MdV has I used the following query:
select mdv.mdvid, group_concat(mdv_has_chain.chainid order by mdv_has_chain.chain_num) as chainid, group_concat(mdv_has_chain.chain_num order by mdv_has_chain.chain_num) as chain_position from mdv inner join mdv_has_chain on mdv.mdvid = mdv_has_chain.mdvid group by mdv_has_chain.mdvid
+-------------+-----------------------+----------------+
| mdvid | chainid | chain_position |
+-------------+-----------------------+----------------+
| ITS058-M152 | ITS058-Ch1 | 1 |
| ITS058-M182 | ITS058-Ch2,ITS058-Ch3 | 1,2 |
| ITS058-M244 | ITS058-Ch4 | 1 |
| ITS058-M253 | ITS058-Ch5,Ch1 | 1,2 |
| ITS058-M258 | ITS058-Ch6 | 1 |
+-------------+-----------------------+----------------+
I was wondering how I would prevent same ChainIds or a combination of them from being entered to a different MdVID? For example how would I prevent the following insert from working:
insert into mdv_has_chain (mdvid, chainid, chain_num) values ("TEST", "ITS058-Ch2", 2), ("TEST", "ITS058-Ch3", 1)
ITS058-M182 already has those ChainIDs, but in different positions. Note that an MdV can have from 1 to N chains. Similarly, how would I allow permutations of ChainIds at different positions, but prevent entries with the same positions from being entered? I don't intend on implementing both things at once. I just wanted to know how I would achieve either individually.
Thank you for you time. Any help is appreciated.

fill at once data null on table sql

I have table transaksi:
+-------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(150) | YES | | NULL | |
| price | int(11) | YES | | NULL | |
| type | int(11) | YES | | NULL | |
+-------------+--------------+------+-----+---------+-------+
and have data (I fill in the data with insert into transaksi (name,price,type) select name,price,type from store)
+-----------+--------------------------+---------+-------+
| id | name | price | Type |
+-----------+--------------------------+---------+-------+
| NULL | Pants | 79 | 9 |
| NULL | Cup | 38 | 7 |
| NULL | Shoes | 21 | 1 |
| NULL | Hat | 11 | 5 |
| NULL | Pulpen | 39 | 2 |
+___________|__________________________|_________|_______|
How to fill "NULL" data at once in ID ?

Mysql sum of every 5 data

Hello I have table named eod where i tons of data by date. here is the schema of the table:
+------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+----------------+
| ID | int(11) | NO | PRI | NULL | auto_increment |
| ticker | varchar(20) | NO | MUL | NULL | |
| entry_date | date | NO | MUL | NULL | |
| high | float(10,2) | NO | MUL | NULL | |
| low | float(10,2) | NO | MUL | NULL | |
| close | float(10,2) | NO | MUL | NULL | |
| vol | int(11) | NO | MUL | NULL | |
+------------+-------------+------+-----+---------+----------------+
here are few sample of data:
+----+-----------+------------+------+------+-------+--------+
| ID | ticker | entry_date | high | low | close | vol |
+----+-----------+------------+------+------+-------+--------+
| 1 | x | 2013-02-20 | 6.70 | 6.50 | 6.50 | 136500 |
| 2 | y | 2013-02-24 | 6.60 | 6.40 | 6.40 | 174500 |
| 3 | z | 2013-02-25 | 6.60 | 6.40 | 6.40 | 214000 |
| 4 | x | 2013-02-26 | 6.50 | 6.30 | 6.40 | 238000 |
| 5 | x | 2013-02-27 | 6.50 | 6.20 | 6.20 | 279000 |
| 6 | a | 2013-02-28 | 6.20 | 6.00 | 6.00 | 260500 |
| 7 | x | 2013-03-03 | 6.10 | 5.70 | 5.70 | 131000 |
| 8 | a | 2013-03-04 | 6.10 | 5.80 | 6.00 | 150000 |
| 9 | x | 2013-03-05 | 6.20 | 6.00 | 6.00 | 95000 |
| 10 | z | 2013-03-06 | 6.10 | 6.00 | 6.00 | 84000 |
+----+-----------+------------+------+------+-------+--------+
I want to know how to get sum of every 5 vol, means from ID 1 to 5, then 6 to 10 .....continues by ticker.
sorry for creating confusion at the first time.
Try this:
SELECT sum(column), floor((id-1)/5) + 1 as min_id
FROM table_name
GROUP BY min_id
Try this
SELECT SUM(vol)
FROM (
SELECT vol, TRUNCATE(id/5,0) AS id
FROM eod) AS t
GROUP BY id
Not tested but logic is correct

rows missing with group by added

this is probably something very noob of me
i have the following table
+------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| from_id | int(11) | YES | MUL | NULL | |
| to_id | int(11) | YES | MUL | NULL | |
| reply_cost | int(5) | YES | | 0 | |
| date | timestamp | YES | | NULL | |
| body | text | YES | | NULL | |
| read_ | char(1) | YES | | 0 | |
| trash | tinyint(1) | YES | MUL | 0 | |
| trashDate | datetime | YES | | NULL | |
| ip_address | varchar(20) | YES | | NULL | |
+------------+-------------+------+-----+---------+----------------+
a select like
select id,from_id,to_id,date from mail where to_id='100' order by id desc limit 50;
returns
+----------+---------+-------+---------------------+
| id | from_id | to_id | date |
+----------+---------+-------+---------------------+
| 30071061 | 142 | 100 | 2013-08-15 04:39:56 |
| 30070785 | 282 | 100 | 2013-08-15 02:29:00 |
| 30064666 | 282 | 100 | 2013-08-14 16:10:39 |
| 30042809 | 458582 | 100 | 2013-08-12 15:50:45 |
| 30042560 | 458582 | 100 | 2013-08-12 15:28:39 |
| 30042557 | 458582 | 100 | 2013-08-12 15:28:22 |
| 30022845 | 458582 | 100 | 2013-08-10 17:32:40 |
| 30022834 | 458582 | 100 | 2013-08-10 17:31:22 |
| 30018276 | 458582 | 100 | 2013-08-10 06:09:27 |
| 30018275 | 458582 | 100 | 2013-08-10 06:09:00 |
but a select like this
select id,from_id,to_id,date from mail where to_id='100' group by from_id order by id desc limit 50;
outputs like this (from_id 282 is missing)
+----------+---------+-------+---------------------+
| id | from_id | to_id | date |
+----------+---------+-------+---------------------+
| 30017678 | 142 | 100 | 2013-08-10 01:56:38 |
| 29928935 | 189638 | 100 | 2013-07-31 18:33:01 |
| 29894382 | 458582 | 100 | 2013-07-27 22:15:53 |
| 29883054 | 409699 | 100 | 2013-07-26 15:22:35 |
any idea how this can be avoided ?
EDIT: forgot to mention, the goal is to return only one row per from_id.
What about the query
select id,from_id,to_id,date from mail where to_id='100' order by id, from_id desc limit 50;
That should work, hopefully.

Fetch cumulative sum from MySQL table

I have a table containing donations, and I am now creating a page to view statistics. I would like to fetch monthly data from the database with gross and cumulative gross.
mysql> describe donations;
+------------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| transaction_id | varchar(64) | NO | UNI | | |
| donor_email | varchar(255) | NO | | | |
| net | double | NO | | 0 | |
| gross | double | NO | | NULL | |
| original_request | text | NO | | NULL | |
| time | datetime | NO | | NULL | |
| claimed | tinyint(4) | NO | | NULL | |
+------------------+------------------+------+-----+---------+----------------+
Here's what I've tried:
SET #cgross = 0;
SELECT YEAR(`time`), MONTH(`time`), SUM(`gross`), (#cgross := #cgross + SUM(`gross`)) AS `cumulative_gross` FROM `donations` GROUP BY YEAR(`time`), MONTH(`time`);
The result is:
+--------------+---------------+--------------+------------------+
| YEAR(`time`) | MONTH(`time`) | SUM(`gross`) | cumulative_gross |
+--------------+---------------+--------------+------------------+
| 2013 | 1 | 257 | 257 |
| 2013 | 2 | 140 | 140 |
| 2013 | 3 | 311 | 311 |
| 2013 | 4 | 279 | 279 |
+--------------+---------------+--------------+------------------+
Which is wrong. The desired result would be:
+--------------+---------------+--------------+------------------+
| YEAR(`time`) | MONTH(`time`) | SUM(`gross`) | cumulative_gross |
+--------------+---------------+--------------+------------------+
| 2013 | 1 | 257 | 257 |
| 2013 | 2 | 140 | 397 |
| 2013 | 3 | 311 | 708 |
| 2013 | 4 | 279 | 987 |
+--------------+---------------+--------------+------------------+
I tried this without SUM, and it did work as expected.
SET #cgross = 0;
SELECT YEAR(`time`), MONTH(`time`), SUM(`gross`), (#cgross := #cgross + 10) AS `cumulative_gross` FROM `donations` GROUP BY YEAR(`time`), MONTH(`time`);
+--------------+---------------+--------------+------------------+
| YEAR(`time`) | MONTH(`time`) | SUM(`gross`) | cumulative_gross |
+--------------+---------------+--------------+------------------+
| 2013 | 1 | 257 | 10 |
| 2013 | 2 | 140 | 20 |
| 2013 | 3 | 311 | 30 |
| 2013 | 4 | 279 | 40 |
+--------------+---------------+--------------+------------------+
Why doesn't it work with SUM? Any ideas how I could fix it?
Thanks,
Lassi
A subquery without variables will do it just as easily, and quite a bit more portably;
SELECT YEAR(`time`),
MONTH(`time`),
SUM(gross),
(SELECT SUM(gross)
FROM donations
WHERE `time`<=MAX(a.`time`)) cumulative_gross
FROM donations a GROUP BY YEAR(`time`), MONTH(`time`);
An SQLfiddle to test with.