简体   繁体   中英

Get the percentage by SQL query

Below is the query which I have created to fetch the data from SCCM database. I am facing a little bit of trouble to get more details.

    select UI.ArticleID ,ui.DateLastModified, 
    SUM (CaSe WHEN (AAA.StateID = '2') Then 1 else 0
        END) As Install_Count,
    sum ( CASE 
          WHEN ((AAA.StateID = '2') and  ( OPSYS.Caption0 like 'Microsoft Windows%10%')) THEN  1 else 0
          END  ) As 'W10',
    sum ( CASE 
          WHEN  ((AAA.StateID = '2') and (OPSYS.Caption0 = 'Microsoft Windows 7 Enterprise' or OPSYS.Caption0 = 'Microsoft Windows 7 Entreprise')) THEN  1 else 0
          END  ) As 'W7',
sum ( CASE 
      WHEN  ((AAA.StateID != '2') and ((OPSYS.Caption0 not like 'Microsoft Windows%10%') and (OPSYS.Caption0 != 'Microsoft Windows 7 Enterprise') and (OPSYS.Caption0 != 'Microsoft Windows 7 Entreprise'))) THEN  1 else 0
      END  ) As 'Other',
        count(AAA.ResourceID) As 'Total Machines'
    FROM v_UpdateInfo UI 
            INNER JOIN v_CIAssignmentToCI CIA ON UI.CI_ID = CIA.CI_ID 
            INNER JOIN v_CIAssignment ON CIA.AssignmentID = v_CIAssignment.AssignmentID 
            Inner Join v_AssignmentStatePerTopic AAA on AAA.AssignmentID = CIA.AssignmentID
            inner join v_GS_OPERATING_SYSTEM  OPSYS  on OPSYS.ResourceID=AAA.ResourceID 
    where     CIA.AssignmentID = '1234567' and aaa.TopicType = '302'
    Group By ui.Title, UI.ArticleID ,ui.DateLastModified

Output of above query.

ArticleID   DateLastModified          Install_Count W10 W7  Other   Total Machines
4519998      2019-10-09 02:19:43.000    26          23  3    0         28
4520010      2019-10-09 02:18:04.000    26          23  3    0         28
4520004      2019-10-09 02:20:05.000    26          23  3    0         28

As you can see in output Other is showing 0. As Total machines are 28 then I am expecting 2 machines in Other sections. I will investigate it later.

I want to get the percent (in decimal) of Install_Count from Total Machines .

Try the below snippet:

 ((Install_Count * 100) /  Total Machines) AS Total_Percentage

code implementation:

    select UI.ArticleID ,ui.DateLastModified, 
        SUM (CaSe WHEN (AAA.StateID = '2') Then 1 else 0
            END) As Install_Count,
        sum ( CASE 
              WHEN ((AAA.StateID = '2') and  ( OPSYS.Caption0 like 'Microsoft Windows%10%')) THEN  1 else 0
              END  ) As 'W10',
        sum ( CASE 
              WHEN  ((AAA.StateID = '2') and (OPSYS.Caption0 = 'Microsoft Windows 7 Enterprise' or OPSYS.Caption0 = 'Microsoft Windows 7 Entreprise')) THEN  1 else 0
              END  ) As 'W7',
    sum ( CASE 
          WHEN  ((AAA.StateID != '2') and ((OPSYS.Caption0 not like 'Microsoft Windows%10%') and (OPSYS.Caption0 != 'Microsoft Windows 7 Enterprise') and (OPSYS.Caption0 != 'Microsoft Windows 7 Entreprise'))) THEN  1 else 0
          END  ) As 'Other',
            count(AAA.ResourceID) As 'Total Machines',
((SUM (CaSe WHEN (AAA.StateID = '2') Then 1 else 0
            END) *100)/count(AAA.ResourceID)) AS Total_percentage
        FROM v_UpdateInfo UI 
                INNER JOIN v_CIAssignmentToCI CIA ON UI.CI_ID = CIA.CI_ID 
                INNER JOIN v_CIAssignment ON CIA.AssignmentID = v_CIAssignment.AssignmentID 
                Inner Join v_AssignmentStatePerTopic AAA on AAA.AssignmentID = CIA.AssignmentID
                inner join v_GS_OPERATING_SYSTEM  OPSYS  on OPSYS.ResourceID=AAA.ResourceID 
        where     CIA.AssignmentID = '1234567' and aaa.TopicType = '302'
        Group By ui.Title, UI.ArticleID ,ui.DateLastModified

Edited:

select UI.ArticleID ,ui.DateLastModified, 
        SUM (CaSe WHEN (AAA.StateID = '2') Then 1 else 0
            END) As Install_Count,
        sum ( CASE 
              WHEN ((AAA.StateID = '2') and  ( OPSYS.Caption0 like 'Microsoft Windows%10%')) THEN  1 else 0
              END  ) As 'W10',
        sum ( CASE 
              WHEN  ((AAA.StateID = '2') and (OPSYS.Caption0 = 'Microsoft Windows 7 Enterprise' or OPSYS.Caption0 = 'Microsoft Windows 7 Entreprise')) THEN  1 else 0
              END  ) As 'W7',
    sum ( CASE 
          WHEN  ((AAA.StateID != '2') and ((OPSYS.Caption0 not like 'Microsoft Windows%10%') and (OPSYS.Caption0 != 'Microsoft Windows 7 Enterprise') and (OPSYS.Caption0 != 'Microsoft Windows 7 Entreprise'))) THEN  1 else 0
          END  ) As 'Other',
            count(AAA.ResourceID) As 'Total Machines',
cast(((SUM (CaSe WHEN (AAA.StateID = '2') Then 1 else 0
            END) *100)/count(AAA.ResourceID))as decimal(10,2)) AS Total_percentage
        FROM v_UpdateInfo UI 
                INNER JOIN v_CIAssignmentToCI CIA ON UI.CI_ID = CIA.CI_ID 
                INNER JOIN v_CIAssignment ON CIA.AssignmentID = v_CIAssignment.AssignmentID 
                Inner Join v_AssignmentStatePerTopic AAA on AAA.AssignmentID = CIA.AssignmentID
                inner join v_GS_OPERATING_SYSTEM  OPSYS  on OPSYS.ResourceID=AAA.ResourceID 
        where     CIA.AssignmentID = '1234567' and aaa.TopicType = '302'
        Group By ui.Title, UI.ArticleID ,ui.DateLastModified

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM