Combine SQL Count Results - sql

I'm currently trying to teach myself SQL in order to write better reports with our Orion system and I'm running into a small issue. I want to generate a report with a count of the number of Windows machines and Linux machines. This is my current code.
SELECT OperatingSystem, Count(OperatingSystem) AS TotalMachines
FROM Machines
Where
(
(OperatingSystem LIKE '%Windows%') OR
(OperatingSystem LIKE '%Linux%')
)
GROUP BY OperatingSystem
And the return I get is this
Red Hat Enterprise Linux 20
Novell SUSE Linux Enterprise 17
Debian Linux 5
Windows Server 2008 (32-bit) 11
Windows Server 2008 R2 (32-bit) 49
Windows Server 2008 (64-bit) 33
Windows Server 2008 R2 (64-bits) 16
Windows Server 2003 (32-bit) 35
Is it possible to combine all of the different Linux Operating Systems into a single row called Linux and combine all of the Windows Operating Systems into a single row called Windows in an SQL Query?

Yes. You want to use case in the group by clause itself:
SELECT (case when OperatingSystem LIKE '%Windows%' then 'Windows'
when OperatingSystem LIKE '%Linux%' then 'Linux'
end) as WhichOs, Count(*) AS TotalMachines
FROM Machines
Where (OperatingSystem LIKE '%Windows%') OR
(OperatingSystem LIKE '%Linux%')
GROUP BY (case when OperatingSystem LIKE '%Windows%' then 'Windows'
when OperatingSystem LIKE '%Linux%' then 'Linux
end);
EDIT:
The above should work (note the same expression is in the select and group by. Perhaps this will work:
SELECT WhichOs, Count(*) AS TotalMachines
FROM (SELECT m.*,
(case when OperatingSystem LIKE '%Windows%' then 'Windows'
when OperatingSystem LIKE '%Linux%' then 'Linux'
end) as WhichOs
FROM Machines m
) m
Where (OperatingSystem LIKE '%Windows%') OR
(OperatingSystem LIKE '%Linux%')
GROUP BY WhichOs;

to solve such problems I prefer to use union neither case, as it helps you easily extend query in future:
select OSType, count(*) as TotalMachines
from (
SELECT 'Linux' as OSType FROM Machines WHERE OperatingSystem LIKE '%Linux%'
UNION ALL
SELECT 'Windows' as OSType FROM Machines WHERE OperatingSystem LIKE '%Windows%'
) as subquery
GROUP BY OSType
in any case, check both variants and select fastest

Try something like this:
SELECT
case
when OperatingSystem like '%Windows%' then 'Windows'
when OperatingSystem like '%Linux%' then 'Linux'
else 'Other'
end as Operating_System
, Count(OperatingSystem) AS TotalMachines
FROM Machines
Where
(
(OperatingSystem LIKE '%Windows%') OR
(OperatingSystem LIKE '%Linux%')
)
GROUP BY
OperatingSystem
, case
when OperatingSystem like '%Windows%' then 'Windows'
when OperatingSystem like '%Linux%' then 'Linux'
else 'Other'
end

Related

Counting instances of value in postgres query

Bit of an SQL newbie question..
If I have a table along the lines of the following :
host fault fatal groupname
Host A Data smells iffy n research
Host B Flanklecrumpet needs a cuddle y production
Host A RAM loves EWE n research
Host Z One of the crossbeams gone askew on the treadle y research
.. and I want to get some stats, I can..
select count(distinct host) as hosts, count(host) as faults, group from tablename group by groupname
.. which gives me the number of faults and affected hosts per groupname.
hosts faults groupname
2 3 research
1 1 production
Can I, in the same query, show the number of fatal entries?
I would use aggregation, but in Postgres would phrase this as:
select groupname, count(distinct host) as hosts,
count(*) as num_faults,
count(*) filter (where fatal = 'Y') as num_fatal
from t
group by groupname;
use conditional aggregation
select count(distinct host) as hosts,
count(host) as faults,sum(case when fatal='y' then 1 else 0 end) as numberofenty,
groupname from tablename group by groupname

How can we convert sql 5.6 query to 5.7

I m facing an issues in sql query
It worked with previous version - 5.6
But now its not working with version 5.7
So anyone can help me to convert this query to sql 5.7
SELECT to_startdate, to_enddate
FROM tour
WHERE to_name !=''
AND to_startdate !='0000-00-00'
AND to_deactivated !=1
GROUP BY MONTH(to_startdate), YEAR(to_startdate)
ORDER BY to_startdate
Your query doesn't make sense in general -- the select references columns and these are not aggregated. You can use aggregation functions. I don't know what values you want, but something like this:
SELECT MIN(to_startdate), MIN(to_enddate)
FROM tour
WHERE to_name <> '' AND
to_startdate <> '0000-00-00' AND
to_deactivated <> 1
GROUP BY MONTH(to_startdate), YEAR(to_startdate)
ORDER BY MIN(to_startdate)

SQL get number of unique client by browser

I'm using AWS Athena to parse my Application Load Balancer logs.
I'm trying to get a list of browsers and for each browsers, the number of unique user.
I've managed to get this list but the user count is not correct. I don't know how to group users by their IP.
1 Google Chrome 9000000
2 Apple Safari 8000000
3 Unknown 5000000
4 Mozilla Firefox 2000000
5 Internet Explorer 10000
6 Outlook 10000
7 Opera 88
8 Edge 7
Here is the query
SELECT DISTINCT
CASE
WHEN user_agent LIKE '%edge%'THEN 'Edge'
WHEN user_agent LIKE '%MSIE%' THEN
'Internet Explorer'
WHEN user_agent LIKE '%Firefox%' THEN
'Mozilla Firefox'
WHEN user_agent LIKE '%Chrome%' THEN
'Google Chrome'
WHEN user_agent LIKE '%Safari%' THEN
'Apple Safari'
WHEN user_agent LIKE '%Opera%' THEN
'Opera'
WHEN user_agent LIKE '%Outlook%' THEN
'Outlook'
ELSE 'Unknown'
END AS browser , COUNT(client_ip) AS Number
FROM alb_logs
WHERE parse_datetime(time,'yyyy-MM-DD''T''HH:mm:ss.SSSSSS''Z')
BETWEEN parse_datetime('2018-01-01-00:00:00','yyyy-MM-DD-HH:mm:ss')
AND parse_datetime('2018-07-18-00:00:00','yyyy-MM-DD-HH:mm:ss')
GROUP BY CASE
WHEN user_agent LIKE '%edge%'THEN 'Edge'
WHEN user_agent LIKE '%MSIE%' THEN
'Internet Explorer'
WHEN user_agent LIKE '%Firefox%' THEN
'Mozilla Firefox'
WHEN user_agent LIKE '%Chrome%' THEN
'Google Chrome'
WHEN user_agent LIKE '%Safari%' THEN
'Apple Safari'
WHEN user_agent LIKE '%Opera%' THEN
'Opera'
WHEN user_agent LIKE '%Outlook%' THEN
'Outlook'
ELSE 'Unknown'
END
ORDER BY Number DESC
I'm missing some kind of group by client_ip, but the result would be wrong...
You need COUNT(DISTINCT client_ip) aggregation and also you don't need SELECT DISTINCT, like this
SELECT CASE WHEN user_agent ... END AS browser, COUNT(DISTINCT client_ip) AS Number
FROM alb_logs
WHERE ...
GROUP BY 1
ORDER BY 2 DESC

SQL Count not working correctly

I am trying to use the query below to get a list of software programs and the count of computers it is installed on. So the output would be like
Count (computers) Software
55 Microsoft Outlook
When I use the query below it gives me a count but I don't think its the count I am looking for. So if the software is installed on 55 computers it would return that count. TIA
SELECT COUNT(c.Name0),
a.DisplayName0
FROM v_GS_ADD_REMOVE_PROGRAMS a,
v_R_System c
WHERE A.DisplayName0 NOT LIKE 'hotfix for%'
AND A.DisplayName0 NOT LIKE 'Security Update for%'
GROUP BY a.DisplayName0
ORDER BY COUNT(c.Name0)
It looks like you're missing a join predicate between the two tables. Is the reference to v_R_System necessary?
If not you can just use
SELECT COUNT(*),
a.DisplayName0
FROM v_GS_ADD_REMOVE_PROGRAMS a
WHERE A.DisplayName0 NOT LIKE 'hotfix for%'
AND A.DisplayName0 NOT LIKE 'Security Update for%'
GROUP BY a.DisplayName0
ORDER BY COUNT(*);
If it is necessary you will need to add a join predicate between the tables.

SQL query works in PL/SQL but not in Visual Studio

I searched online and found out many had the same problem, but non of the solutions worked for me.
I'm really hoping you could help me:
I have this ORACLE SQL query that is working fine in PL/SQL:
select a.bzq_terminate_provider, a.callsnum, a.at_call_dur_sec, sum_charge
From (select * from usage_cycle_sum
where ban='80072922' and ben='1'
and subscriber_no='036585305'
and start_cycle_code ='20150207'
and feature_code_rank='1') a, (select bzq_terminate_provider,sum(charge_amount) as sum_charge from usage_cycle_sum
where ban='80072922' and ben='1'
and subscriber_no='036585305'
and start_cycle_code ='20150207' group by bzq_terminate_provider) b
where a.bzq_terminate_provider=b.bzq_terminate_provider
I also tried this other version that works fine as well:
select PROVIDER,sum(CALLS),sum(CHARGE),sum(DUR)
from (
select bzq_terminate_provider PROVIDER,callsnum CALLS,charge_amount CHARGE,at_call_dur_sec DUR
from usage_cycle_sum
where ban='80072922' and ben='1'
and subscriber_no='036585305'
and start_cycle_code ='20150207'
and feature_code_rank='1'
union
select bzq_terminate_provider PROVIDER,0 CALLS,charge_amount CHARGE,0 DUR
from usage_cycle_sum
where ban='80072922' and ben='1'
and subscriber_no='036585305'
and start_cycle_code ='20150207'
and feature_code_rank='2'
)
group by PROVIDER
My problem is that when i create a datagrid in Visual Studio web application, i get an error: syntax error: expecting identifier or quoted identifier
The connection is ok, i checked the simple select queries as well as the whole union part in the second query i attached, they work!
But when i use those two versions, i get this error.
What can be the problem? Is there another way to solve this?
Thanks.
EDIT 21/06/2015
It seems that visual studio doesn't work well with complex queries and i'm still looking for a solution for this, since my next queries are more complex...
Your second query is so much nicer to write as:
select bzq_terminate_provider as PROVIDER, sum(callsnum) as CALLS,
sum(charge_amount) as CHARGE, sum(at_call_dur_sec) as DUR
from usage_cycle_sum
where ban = '80072922' and ben = '1' and
subscriber_no = '036585305' and
start_cycle_code ='20150207' and
feature_code_rank in ('1', '2')
group by bzq_terminate_provider ;
Or, perhaps the select needs to be:
select bzq_terminate_provider as PROVIDER,
sum(case when feature = '1' then callsnum else 0 end) as CALLS,
sum(charge_amount) as CHARGE,
sum(case when feature = '1' then at_call_dur_sec else 0 end) as DUR
(The first version assumed that the fields were zeroed out in the second subquery because they are NULL in the data, but that might not be true.)
However, application software is not yet smart enough to identify such awkwardly written queries, so that is not the actual problem you are facing. If the query works in the database, but not in the application, then typical problems are:
The application is not connected to the right database.
The application does not have permissions on the database or table.
The application query is different from the query run in the database, typically due to some substitution problem.
The results from running the query in the application are not being interpreted correctly.