GROUP BY doesn't work - mysql

please advice why group by doesn't work here.
I want it to be grouped by setup_time:
mysql> select i_connection, avg(setup_time) from CDR_Vendors where i_env = 9 group by i_connection order by setup_time asc limit 15;
+--------------+-----------------+
| i_connection | avg(setup_time) |
+--------------+-----------------+
| 0 | 0.0000 |
| 351 | 0.0000 |
| 235 | 11.9830 |
| 376 | 77.3151 |
| 396 | 168.3036 |
| 318 | 99.0741 |
| 319 | 126.4138 |
| 317 | 366.6881 |
| 545 | 2523.9878 |
| 551 | 4811.5482 |
| 485 | 3410.2093 |
| 289 | 692.0000 |
| 239 | 4314.1667 |
| 510 | 3322.8444 |
| 546 | 2041.7491 |
+--------------+-----------------+

select i_connection, avg(setup_time) from CDR_Vendors where i_env = 9 group by setup_time order by setup_time asc limit 15;
You can group by 'i_connection'.

Related

Mysql multiple order, one order by date with condition

I have a simple SQL request which orders the result by the number of sales and then by the number of views.
SELECT io__image.sales, io__image.viewcount, io__image.date
FROM io__image
ORDER BY io__image.sales DESC,
io__image.viewcount DESC;
But I would like the new images with a date greater than for example "2022-05-01" to appear just after the ones that have been sold.
Is it possible to have multiple order and in one of this order a condition?
ORDER BY io__image.sales DESC,
if(io_image.date >= "2022-05-01") ...,
io__image.viewcount DESC;
Example of current results:
+-------+-----------+---------------------+
| sales | viewcount | date |
+-------+-----------+---------------------+
| 5 | 7 | 2021-04-21 19:13:21 |
| 4 | 186 | 2018-05-09 13:45:40 |
| 4 | 135 | 2018-05-11 17:22:30 |
| 3 | 157 | 2018-05-02 09:47:48 |
| 1 | 8 | 2021-08-29 11:22:55 |
| 1 | 7 | 2021-06-21 12:26:32 |
| 1 | 5 | 2021-06-21 12:40:38 |
| 1 | 4 | 2021-06-14 15:15:01 |
| 0 | 824 | 2021-04-21 22:12:48 |
| 0 | 430 | 2020-11-27 13:46:59 |
| 0 | 228 | 2017-10-24 09:05:40 |
| 0 | 209 | 2019-11-24 11:32:43 |
| 0 | 184 | 2018-05-02 21:26:40 |
| 0 | 174 | 2018-05-02 21:21:20 |
| 0 | 174 | 2018-05-03 09:08:53 |
| 0 | 171 | 2018-05-02 09:20:34 |
Let's say we have 2 new images with a date >= 2022-05-01 with low viewcount and no sales, and I would like:
+-------+-----------+---------------------+
| sales | viewcount | date |
+-------+-----------+---------------------+
| 5 | 7 | 2021-04-21 19:13:21 |
| 4 | 186 | 2018-05-09 13:45:40 |
| 4 | 135 | 2018-05-11 17:22:30 |
| 3 | 157 | 2018-05-02 09:47:48 |
| 1 | 8 | 2021-08-29 11:22:55 |
| 1 | 7 | 2021-06-21 12:26:32 |
| 1 | 5 | 2021-06-21 12:40:38 |
| 1 | 4 | 2021-06-14 15:15:01 |
| 0 | 10 | 2022-07-14 12:11:25 |
| 0 | 5 | 2022-06-21 08:45:43 |
| 0 | 824 | 2021-04-21 22:12:48 |
| 0 | 430 | 2020-11-27 13:46:59 |
| 0 | 228 | 2017-10-24 09:05:40 |
| 0 | 209 | 2019-11-24 11:32:43 |
| 0 | 184 | 2018-05-02 21:26:40 |
| 0 | 174 | 2018-05-02 21:21:20 |
| 0 | 174 | 2018-05-03 09:08:53 |
| 0 | 171 | 2018-05-02 09:20:34 |
You can use the boolean expression sales = 0 AND date >= '2022-05-01' in the ORDER BY clause, between the 2 columns:
SELECT *
FROM io__image
ORDER BY sales DESC,
sales = 0 AND date >= '2022-05-01' DESC,
viewcount DESC;
See the demo.

Make a SQL query on three tables with a NOT-LIKE clause

I need to extract all servers that are not member of a specified group.
I have 3 tables: host (containing hosts), hostgroup_relation (containing the host id and the hostgroup id), hostgroup (containing hostgroups)
I can get the relationships, but I need every hosts that are NOT member of the group with id 180
Host:
SELECT host_id,host_name FROM host LIMIT 10;
+---------+-------------------+
| host_id | host_name |
+---------+-------------------+
| 1482 | AADSYNC1 |
| 442 | Acces-Point-Wifi |
| 1916 | ADAUDIT1 |
| 1562 | ADMORA1 |
| 2247 | ADMRDS2 |
| 2226 | ADSECU1 |
| 1203 | ADSELFSERVICE1 |
| 1172 | ALFRESCO1 |
| 1841 | ALFRESCO2 |
| 172 | Antispam-Ironport |
+---------+-------------------+
Hostgroups:
SELECT hg_id, hg_name FROM hostgroup LIMIT 10
+-------+----------------------+
| hg_id | hg_name |
+-------+----------------------+
| 82 | Antivirus-Trend |
| 65 | Autocoms |
| 72 | Baies-de-stockage |
| 78 | Consoles |
| 192 | Databases-All |
| 193 | Databases-Main |
| 68 | Databases-MySql |
| 67 | Databases-Oracle |
| 181 | Databases-PostgreSql |
| 69 | Databases-SQLServer |
+-------+----------------------+
Host/hostgroup relation:
SELECT * FROM hostgroup_relation LIMIT 10;
+--------+-----------------+--------------+
| hgr_id | hostgroup_hg_id | host_host_id |
+--------+-----------------+--------------+
| 5698 | 70 | 1167 |
| 6772 | 53 | 1167 |
| 6820 | 144 | 1369 |
| 6821 | 62 | 1369 |
| 6822 | 53 | 1369 |
| 6823 | 70 | 1369 |
| 6825 | 62 | 1370 |
| 6826 | 53 | 1370 |
| 6827 | 70 | 1370 |
| 6829 | 62 | 1371 |
+--------+-----------------+--------------+
Here is where I've gone so far:
SELECT host.host_name, hostgroup.hg_name
FROM host, hostgroup_relation, hostgroup
WHERE hostgroup_relation.hostgroup_hg_id = hostgroup.hg_id
AND hostgroup_relation.host_host_id = host.host_id
LIMIT 10;
+-----------+-----------------------------+
| host_name | hg_name |
+-----------+-----------------------------+
| AADSYNC1 | Default-bi |
| AADSYNC1 | Serveurs-Virtuels |
| AADSYNC1 | Serveurs-Windows |
| AADSYNC1 | Reboot_serveurs-12h00:14h00 |
| ADAUDIT1 | Default-bi |
| ADAUDIT1 | Serveurs-Virtuels |
| ADAUDIT1 | Serveurs-Windows |
| ADAUDIT1 | Reboot_serveurs-12h00:14h00 |
| ADMORA1 | Default-bi |
| ADMORA1 | Reboot_serveurs-00h00:4h00 |
+-----------+-----------------------------+
And I need a list of all servers that are not in a specified group.
You need to do a simple join.
This could work:
SELECT h.host_id, h.host_name
FROM host h
LEFT OUTER JOIN hostgroup_relation hgr ON (hgr.host_host_id = h.host_id AND hgr.hostgroup_hg_id = 180)
WHERE hgr.hgr_id IS NULL
Try this:
select h.host_id, hg.host_name, hg.hg_id
from host h
join hostgroup_relation hg
where h.host_id = hg.host_host_id
and hg_id not in 180;

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;

SQL sorting such that rows are sorted only where another column value is equal

I have a SQL query problem. So here is a table which is a result of a large query.
+---------+-------------+------------+
| page_id | TotalDegree | matchCount |
+---------+-------------+------------+
| 116 | 7.0000 | 4 |
| 142 | 3.0000 | 4 |
| 109 | 4.0000 | 2 |
| 119 | 4.0000 | 2 |
| 108 | 2.0000 | 2 |
| 132 | 4.0000 | 1 |
| 133 | 9.0000 | 1 |
| 146 | 9.0000 | 1 |
| 114 | 4.0000 | 1 |
| 125 | 9.0000 | 1 |
| 113 | 1.0000 | 1 |
| 127 | 9.0000 | 1 |
| 120 | 4.0000 | 1 |
| 129 | 9.0000 | 1 |
| 121 | 9.0000 | 1 |
+---------+-------------+------------+
15 rows in set (0.00 sec)
Now I want to sort a table on the basis of 'TotalDegree' in ascending order such that sorting should be done internally where the 'matchCount' column value is same.
Example: The first two rows has 'matchCount' value 4 so sorting should be done in between these two rows on the basis of 'TotalDegree' in ascending order.
Similarly, all the rows with 'matchCount' equals to 1 should be sorted amongst themselves.
So the sorted table should look like as below.
+---------+-------------+------------+
| page_id | TotalDegree | matchCount |
+---------+-------------+------------+
| 142 | 3.0000 | 4 |
| 116 | 7.0000 | 4 |
| 108 | 2.0000 | 2 |
| 109 | 4.0000 | 2 |
| 119 | 4.0000 | 2 |
| 113 | 1.0000 | 1 |
| 132 | 4.0000 | 1 |
| 114 | 4.0000 | 1 |
| 120 | 4.0000 | 1 |
| 133 | 9.0000 | 1 |
| 146 | 9.0000 | 1 |
| 125 | 9.0000 | 1 |
| 127 | 9.0000 | 1 |
| 129 | 9.0000 | 1 |
| 121 | 9.0000 | 1 |
+---------+-------------+------------+
How can such a query can be designed ? Thanks in advance.
You want to sort on MatchCount first (in descending order),
then on TotalDegree (in ascending order).
select * from TableName
order by MatchCount desc, TotalDegree asc;
Have a look at the order by clause
select * from fooTable order by foo1 desc, foo2 asc
in your case:
select page_id ,TotalDegree , matchCount from fooFable order by matchCount desc, TotalDegree asc

MySQL information from another table

I have the following SQL:
Select roleid , deity_level FROM default_jd_deity_role LIMIT 10;
That gives the output:
+--------+-------------+
| roleid | deity_level |
+--------+-------------+
| 1024 | 1 |
| 1043 | 54 |
| 1056 | 1 |
| 1057 | 54 |
| 1072 | 54 |
| 1074 | 45 |
| 1075 | 36 |
| 1088 | 45 |
| 1089 | 45 |
| 1104 | 27 |
+--------+-------------+
Then I have this SQL:
Select roleid , name FROM default_jd_ingame_roles LIMIT 22, 10
That gives the following output:
+--------+---------+
| roleid | name |
+--------+---------+
| 1024 | Hulu |
| 1043 | Cookiez |
| 1056 | Sam |
| 1057 | Sugar |
| 1072 | Leah |
| 1073 | Smexy |
| 1074 | Bam! |
| 1075 | Lexi |
| 1088 | OneShot |
| 1089 | Demono |
+--------+---------+
What I am trying to do is make deity_level add on to the second SQL Query like this:
+--------+---------+-------------+
| roleid | name | deity_level |
+--------+---------+-------------+
| 1024 | Hulu | 1 |
| 1043 | Cookiez | 54 |
| 1056 | Sam | 1 |
| 1057 | Sugar | 54 |
| 1072 | Leah | 54 |
| 1073 | Smexy | 45 |
| 1074 | Bam! | 36 |
| 1075 | Lexi | 45 |
| 1088 | OneShot | 45 |
| 1089 | Demono | 27 |
+--------+---------+-------------+
Try this:
Select a.roleid , a.deity_level, b.name
FROM default_jd_deity_role AS a
JOIN default_jd_ingame_roles AS b ON a.roleid=b.roleid
LIMIT 10
You can achieve this with a JOIN, like this:
SELECT main.roleid, roles.name, main.deity_level
FROM default_jd_deity_role main
LEFT JOIN default_jd_ingame_roles roles ON main.roleid = roles.roleid
LIMIT 22, 10
I'll leave two nice tutorials to get you started on JOINS:
http://www.sitepoint.com/understanding-sql-joins-mysql-database/
http://www.codinghorror.com/blog/2007/10/a-visual-explanation-of-sql-joins.html
You are looking for a JOIN query:
SELECT igr.roleid , igr.name, dr.diety_level
FROM default_jd_ingame_roles igr
JOIN default_jd_deity_role dr ON dr.roleid = igr.roleid