If I insert a value with an order by name like this.
+------------------+
| Table: example |
+------------------+
| Name | Value |
+---------+--------+
| A | 153 |
| B | 10 |
| C | 20 |
| D | 50 |
| E | 100 |
+---------+--------+
When I select the value with a query
select * from example
or
select * from example limit 2
without a where, order by or group by, is the order of the result defaulted to the insert order??
Thanks,
Related
this is all data from current table
SELECT
id,harga,kode_tahun_ajaran
FROM
tblharga
+----+---------+-------------------+
| id | harga | kode_tahun_ajaran |
+----+---------+-------------------+
| 1 | 400000 | THN2018/2019 |
| 2 | 50000 | THN2018/2019 |
| 3 | 1000000 | THN2018/2019 |
| 4 | 900000 | THN2018/2019 |
| 5 | 500000 | THN2017/2018 |
| 6 | 600000 | THN2018/2019 |
+----+---------+-------------------+
and i run this code to get the last harga with grouping the kode_tahun_ajaran
SELECT
id,harga,kode_tahun_ajaran
FROM
tblharga
GROUP BY
kode_tahun_ajaran
ORDER BY id DESC
+----+--------+-------------------+
| id | harga | kode_tahun_ajaran |
+----+--------+-------------------+
| 5 | 500000 | THN2017/2018 |
| 1 | 400000 | THN2018/2019 |
+----+--------+-------------------+
the harga column should take the last data which is 600000 because of the order by code.
how can I retrieve the latest data by grouping it another way?
thanks in advance.
You can try below
SELECT
id,harga,kode_tahun_ajaran
FROM
tblharga where id in (select max(id) from tblharga group by kode_tahun_ajaran)
select id,S.kode_tahun_ajaran,harga
from tblharga S inner join
( select kode_tahun_ajaran,max(id) maxid from tblharga group by kode_tahun_ajaran) T
where T.kode_tahun_ajaran=S.kode_tahun_ajaran and maxid=id;
So I have a table in this form
+----+-----+
| id | Val |
+----+-----+
| 1 | 20 |
| 1 | 30 |
| 2 | 0 |
| 2 | 80 |
| 3 | 45 |
| 3 | 60 |
| 4 | 0 |
| 4 | 30 |
+----+-----+
I want to do a groupby on the id and the sum the values in the Val column. Only, if one of the values is zero, the resultant sum should also be zero
For the above table, the output would be
+----+-------+
| id |SumVal |
+----+-------+
| 1 | 50 |
| 2 | 0 |
| 3 | 105 |
| 4 | 0 |
+----+-------+
I've tried using conditional statements(CASE and IF) inside the sum function but those conditions seem to work only on the individual elements.
Any pointers ?
Using SUM and IF:
SELECT id, (SUM(val) * IF(val = 0, 0, 1)) AS SumVal FROM idval GROUP BY id;
Check This Simple Query.
select id ,SUM(val)
from #Table
where val=0
group by ID
union
select id ,SUM(val)
from #Table
where id not in (select id from #Table where val=0)
group by ID
OutPut :
try this
select id,if(cast(val as signed),sum(val),0) as total from tbl_name group by id;
I have a table with multiple Paths of shares i want the get all the paths where the first N different Servernames in these Paths as example here the whole table.
+----+--------------------------------+
| ID | BACKUPPATH |
+----+--------------------------------+
| 1 | //server.domain/share/folder |
| 2 | //server.domain/share/folder3 |
| 3 | //server.domain/share/folder2 |
| 4 | //server2.domain/share/folder1 |
| 5 | //server2.domain/share/folder2 |
| 6 | //server3.domain/share/folder1 |
| 7 | //server3.domain/share/folder2 |
| 8 | //server3.domain/share/folder3 |
+----+--------------------------------+
the Servernames could vary each time and the number the different Servenames could vary too. As example i want to get all Paths of the first 2 different Servernames i expect as Result:
+----+--------------------------------+
| ID | BACKUPPATH |
+----+--------------------------------+
| 1 | //server.domain/share/folder |
| 2 | //server.domain/share/folder3 |
| 3 | //server.domain/share/folder2 |
| 4 | //server2.domain/share/folder1 |
| 5 | //server2.domain/share/folder2 |
+----+--------------------------------+
as subquery i use the following query to get rowset of the Servernames:
select SUBSTRING_INDEX(BACKUPPATH,'/',3) as SERVERNAMES from(select BACKUPPATH from Backuppaths GROUP BY SUBSTRING_INDEX(BACKUPPATH,'/',3))as NUMEROFSERVERS LIMIT 2;
+------------------+
| SERVERNAMES |
+------------------+
| //server.domain |
| //server2.domain |
+------------------+
i am stuck now in how to use this subquery to get the results i expect.
Thanks for any help in this
You can join to inline view:
select s.*
from servernames s
join (select substring_index(backuppath, '/', 3) as servername
from servernames
group by servername
order by min(id) limit 2) v
on substring_index(backuppath, '/', 3) = v.servername
order by id
Fiddle: http://sqlfiddle.com/#!2/b6a16/1/0
Use Distinct
SELECT DISTINCT SUBSTRING_INDEX(BACKUPPATH,'/',3) AS SERVERNAMES FROM Backuppaths LIMIT 2;
I have a MySQL table with rows containing duplicate values of text ('a' and 'c'):
+------+-----+
| text | num |
+------+-----+
| a | 10 |
| b | 10 |
| c | 10 |
| d | 10 |
| c | 5 |
| z | 10 |
| a | 6 |
+------+-----+
So, I want to update these rows summing the values of num. After that the table should look like this:
+------+-----+
| text | num |
+------+-----+
| a | 16 |
| b | 10 |
| c | 15 |
| d | 10 |
| z | 10 |
+------+-----+
UPD: Edited question.
Use the aggregate function SUM with a GROUP BY clause. Something like this:
SELECT `text`, SUM(num) AS num
FROM YourTableName
GROUP BY `text`;
SQL fiddle Demo
This will give you:
| TEXT | NUM |
--------------
| a | 16 |
| b | 10 |
| c | 15 |
| d | 10 |
| z | 10 |
You can create a temporary table to store the aggregated data temporarily into that and then update the original table from it.
Create a temporary table
select the aggregated data from the original
then delete all data in the original table
and then select the aggregated data from the temporary table into the original table.
Example SQL:
BEGIN;
CREATE TEMPORARY TABLE `table_name_tmp` LIKE `table_name`;
INSERT INTO `table_name_tmp` SELECT `text`, SUM(num) AS num FROM `table_name` GROUP BY 1;
DELETE FROM `table_name`;
INSERT INTO `table_name` SELECT * FROM `table_name_tmp`;
-- COMMIT;
I commented out the COMMIT command to avoid unwanted errors, please check the results before using it.
Lets say I have a table with the following rows/values:
+--------+----------+
| ID | adspot |
+--------+----------+
| 1 | A |
| 2 | B |
| 3 | A |
| 4 | B |
| 5 | C |
| 6 | A |
+--------+----------+
I need a way to select the values in adspot but only once if they're duplicated. So from this example I'd want to select A once and B once. The SQL result should look like this then:
+----------+
| adspot |
+----------+
| A |
| B |
| C |
+----------+
I'm using mySQL and PHP, in case anyone asks.
Thanks.
SELECT DISTINCT adspot FROM your_table; ( this may not perform well at all in large tables )
SELECT adspot FROM table GROUP BY adspot
see: http://www.tizag.com/mysqlTutorial/mysqlgroupby.php