SELECT 'COUNT=' + CONVERT(VARCHAR(64), COUNT(s.company)) AS sites
FROM site s
WHERE s.sitetype = 'om'
AND s.status = 1
EXCEPT
SELECT DISTINCT sg.company
FROM snmpmibdevice AS sdevice
JOIN site sg
ON sg.guid = sdevice.siteguid
JOIN snmpmibdata sdata
ON sdata.snmpmibdeviceguid = sdevice.snmpmibdeviceguid
WHERE sdata.sampletimestamp > Dateadd (mi, -15, Getutcdate())
AND sg.sitetype = 'OM'
Basically I am attempting to return a count of company names from this. If I remove the count and just select "S.Company" I will get 2 values (I would like "COUNT=2"), but with the count it comes up as 34 records (COUNT=34).
I would appreciate any help. Thanks!
SQL SELECT COUNT() can be clubbed with SQL WHERE clause. Using the WHERE clause, we have access to restrict the data to be fed to the COUNT() function and SELECT statement through a condition.
The count() function with the GROUP BY clause is used to count the data which were grouped on a particular attribute of the table.
COUNT(*) returns the number of rows in a specified table, and it preserves duplicate rows. It counts each row separately. This includes rows that contain null values.
wrap the whole thing in a "select count(*) from ()"
SELECT 'COUNT=' + CONVERT(VARCHAR(64), COUNT(company)) AS sites
FROM (
SELECT s.company
FROM site s
WHERE s.sitetype = 'om'
AND s.status = 1
EXCEPT
SELECT DISTINCT sg.company
FROM snmpmibdevice AS sdevice
JOIN site sg
ON sg.guid = sdevice.siteguid
JOIN snmpmibdata sdata
ON sdata.snmpmibdeviceguid = sdevice.snmpmibdeviceguid
WHERE sdata.sampletimestamp > Dateadd (mi, -15, Getutcdate())
AND sg.sitetype = 'OM'
) a
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With