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;
Fairly new to MySQL and I'm getting a little lost in how to build the following query.
The table below shows what I want my intended output to be like.
+---------+------------+-----------------+-------+----------+------------+
| Account | Inv Date | Description | Value | Inv Item | Eff From |
+---------+------------+-----------------+-------+----------+------------+
| 12 | 2018-08-14 | Unlimited (SV4) | 5.99 | 3056746 | 2018-08-02 |
| 12 | 2018-08-14 | Unlimited (SV4) | 5.99 | 3056748 | 2018-08-02 |
| 15 | 2018-09-14 | Unlimited (GB2) | 7.99 | 3187748 | 2018-09-07 |
| 15 | 2018-09-14 | Unlimited (GB1) | 7.26 | 3187749 | 2018-09-07 |
+---------+------------+-----------------+-------+----------+------------+
I want to see accounts that have more than one description like "Unlimited", the "Inv Date" needs to show only the oldest date for each account. And the "Eff from" needs to be from when the "status" was first "active" on the account.
Here is my code as it currently stands, i've also included the tables i'm trying to join.
SELECT
AA.Account,
MIN(II.InvDate),
ST.Description,
ST.Value,
ST.InvItem,
MIN(AA.EffFrom)
FROM db.tblAccountStatus AA
INNER JOIN db.tblInvoiceID II ON II.Account = AA.Account
INNER JOIN db.tblInvoiceStatus ST ON ST.InvID = II.InvID
WHERE AA.Status = 'Active'
AND ST.Description LIKE '%Unlimited%'
GROUP BY ST.InvItem HAVING COUNT(II.InvDate) >1
.
db.tblInvoiceStatus
+--------+------------------+-------+----------+
| Inv ID | Description | Value | Inv Item |
+--------+------------------+-------+----------+
| 1030 | Unlimited (SV4) | 5.99 | 3056746 |
| 1030 | Unlimited (SV4) | 5.99 | 3056748 |
| 1030 | Extras (PB1) | 0.99 | 3056751 |
| 1045 | Unlimited (SLV2) | 5.99 | 3166769 |
| 1045 | Extras (PA1) | 1.99 | 3166770 |
| 1078 | Unlimited (GB2) | 7.99 | 3187748 |
| 1078 | Unlimited (GB1) | 7.26 | 3187749 |
| 1091 | Unlimited (SV1) | 5.99 | 3186788 |
| 1098 | Unlimited (BA2) | 5.49 | 3209899 |
+--------+------------------+-------+----------+
.
db.tblInvoiceID
+--------+---------+------------+
| Inv ID | Account | Inv Date |
+--------+---------+------------+
| 1030 | 12 | 2018-08-14 |
| 1045 | 12 | 2018-09-14 |
| 1078 | 15 | 2018-09-14 |
| 1091 | 17 | 2018-09-15 |
| 1098 | 17 | 2018-10-15 |
| 1099 | 19 | 2018-10-20 |
+--------+---------+------------+
.
db.tblAccountStatus
+---------+--------+------------+------------+
| Account | Status | Eff From | Eff To |
+---------+--------+------------+------------+
| 12 | Active | 2018-08-02 | 2018-09-16 |
| 12 | Active | 2018-09-17 | 2018-09-28 |
| 12 | Active | 2018-09-29 | NULL |
| 15 | Active | 2018-09-07 | 2018-09-16 |
| 15 | Closed | 2018-09-17 | NULL |
| 17 | Active | 2016-04-28 | NULL |
| 19 | Active | 2015-05-05 | NULL |
+---------+--------+------------+------------+
It seems good to inner join db.tblInvoiceID that has oldest date records as below sql.
SELECT
AA.Account,
II.InvDate,
ST.Description,
ST.Value,
ST.InvItem,
AA.EffFrom
FROM db.tblAccountStatus AA
INNER JOIN (
SELECT InvID, Account, MIN(InvDate) AS InvDate FROM db.tblInvoiceID
GROUP BY Account
) II ON II.Account = AA.Account
INNER JOIN db.tblInvoiceStatus ST ON ST.InvID = II.InvID
WHERE AA.Status = 'Active'
AND ST.Description LIKE '%Unlimited%'
GROUP BY ST.InvItem
The following is my output.
Although your intended output doesn't include Account = 17, I couldn't understand a condition from your question. Please teach me if you have additional conditions.
+---------+------------+------------------+-------+---------+------------+
| Account | InvDate | Description | Value | InvItem | EffFrom |
+---------+------------+------------------+-------+---------+------------+
| 12 | 2018-08-14 | Unlimited (SV4) | 5.99 | 3056746 | 2018-08-02 |
| 12 | 2018-08-14 | Unlimited (SV4) | 5.99 | 3056748 | 2018-08-02 |
| 17 | 2018-09-15 | Unlimited (SV1) | 5.99 | 3186788 | 2016-04-28 |
| 15 | 2018-09-14 | Unlimited (GB2) | 7.99 | 3187748 | 2018-09-07 |
| 15 | 2018-09-14 | Unlimited (GB1) | 7.26 | 3187749 | 2018-09-07 |
+---------+------------+------------------+-------+---------+------------+
5 rows in set (0.00 sec)
I have three tables :
em_employee :
emp_number | emp_firstname |
+------------+---------------+
| 1 | Vikram |
| 2 | S. |
| 3 | Gopal |
| 4 | Vaishnavi |
| 5 | Srinivasan |
| 6 | Saravanan
em_project
+------------+------------------------------+
| project_id | name |
+------------+------------------------------+
| 339 | MoneyGram |
| 340 | SERVICE LINE HEAD COMPLIANCE |
| 341 | SERVICE LINE HEAD ANALYTICS |
| 342 | GSI |
| 343 | Tandem |
| 344 | Master Card |
+------------+------------------------------+
em_project_employee:
+------------+------------+
| emp_number | project_id |
+------------+------------+
| 1 | 339 |
| 2 | 340 |
| 3 | 341 |
| 4 | 342 |
| 1 | 343 |
| 6 | 344 |
| 2 | 342 |
+------------+------------+
And I want Output like :
+------------+----------------------------------+
| emp_number | name |
+------------+----------------------------------+
| 1 | MoneyGram , Tandem |
| 2 | SERVICE LINE HEAD COMPLIANCE,GSI |
| 3 | SERVICE LINE HEAD ANALYTICS |
| 4 | GSI |
| 6 | Master Card |
+------------+----------------------------------+
I have tried it with GROUP_CONCAT, but something going wrong. Please help me on this.
Try this query, it produes that output:
SELECT emp_number, GROUP_CONCAT(name) FROM em_project p
INNER JOIN em_project_employee em ON p.project_id = em.project_id
GROUP BY emp_number;
The order of the data will e slightly different from what's in your desired output. If the ordering is important.
GROUP_CONCAT(name ORDER BY p.project_id)
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'.
I wrote this query to find the list of shops selling what categories.
SELECT GROUP_CONCAT(distinct(sub_category_id)) AS s,
vendor_id AS v
FROM link_products_lists_vendors
GROUP BY vendor_id;
This results in,
+---------------------------------------+------------+
| category_ids | vendor_ids |
+---------------------------------------+------------+
| 24,28,25,16,26,23,27,2 | 3 |
| 2 | 67 |
| 19,28,17,16,20,2 | 68 |
| 19,28,24,26,23,21,16,27,22,17,25,2 | 122 |
| 16,2 | 123 |
| 28,17,22,21,18,16,26,27,20,23,25,2 | 124 |
| 22,19,21,20,16,24,28,25,23,26,2 | 125 |
| 23,24,26,25,28,16,20,27,19,2 | 126 |
| 19,26,28,18,20,27,22,16 | 127 |
| 22,26,28,21,23,20,24,19,16,17,27,25,2 | 128 |
| 2 | 129 |
| 2 | 133 |
| 19,20,28,16,27,25,21,23,26,24,22 | 135 |
| 23,28,17,22,26,21,16,20,27,24,25,2 | 136 |
| 19,17,16,21,23,26,22,25,27,20,28 | 137 |
| 19,20,26,22,21,24,23,17,28,16,27,25,2 | 138 |
| 19,20,23,28,26,21,24,16,27,22,25,17,2 | 139 |
| 22,27,20,21,24,17,23,28,26,19,25,2 | 142 |
| 19,28,17,20,2 | 143 |
+---------------------------------------+------------+
19 rows in set (0.01 sec)
What I want now is something like,
+-------------------------- -----------+--- ----------+
| category_names | vendor_names |
+---------------------------------------+--------------+
| mobiles,laptops,desktops | abcShop |
| mobiles | xyzShop |
| desktops,mouses,keyboards | pqrShop |
+---------------------------------------+--------------+
I have the categories table as,
+----+---------------+
| id | name |
+----+---------------+
| 17 | desktops |
| 18 | external_hdds |
| 26 | headphones |
| 27 | headsets |
| 22 | keyboards |
| 16 | laptops |
| 24 | memory_cards |
| 2 | mobile-phones |
| 21 | mouses |
| 25 | pendrives |
| 19 | printers |
| 20 | routers |
| 23 | speakers |
| 28 | tablets |
+----+---------------+
Vendors table as,
+-----+----------------------+
| id | name |
+-----+----------------------+
| 108 | abcShop |
| 109 | xyzShop |
| 45 | pqrShop |
| 89 | . |
| 63 | . |
| 64 | . |
+-----+----------------------+
How should I write a query that will not display ids but use the table that displays ids and displays names? I have no clue from where to start this. Please help!
Just join in the other tables and select those values instead
SELECT GROUP_CONCAT(distinct(c.name)) AS s,
v.name AS v
FROM link_products_lists_vendors l
JOIN categories c on l.category_id = c.id
JOIN vendors v on l.vendor_id = v.id
GROUP BY v.name;
This works as long as vendor names and description names are unique.