簡體   English   中英

MYSQL / SQL計數百分比查詢

[英]MYSQL / SQL Count Percentage Query

是)我有的

$query =
  "SELECT
      status_type,
      COUNT(*) as count,
      kpi_type,
      COUNT('kpi') * 100.00 as percentage
  FROM
      main
  GROUP BY
      status_type DESC;";

我得到了什么。

[{"Status":"SOLD","KPI_Percentage":"1400","KPI":"SALE","Status_Count":"14"}]

這是我的JSON數組

 $JSON_output[] = array('Status'         => $row['pin_status'],
                           'KPI'            => $row['kpi_type'],
                           'KPI_Percentage'         => $row['percent'],
                           'Status_Count'           => $row['count'],);}}

我想做什么......

[{"status":"value","kpi":"value","count":"12""percentage":"%32.42"}]

我正在嘗試計算status_type ,並顯示status_type ,我正在嘗試顯示kpi_type ,並計算kpi_type與其他kpi_type s相比所以如果我有3個已售出,2個潛在客戶和5個潛在客戶,則會顯示:“LEAD: 20%出售30%前景50%“

+------+-----------------+------------------+--------------+
| ID   | kpi_type        | status_type      | Is Deleted   |
+------+-----------------+------------------+--------------+
|    1 |            SALE |SOLD              |     no       |
|    2 |            LEAD |Maybe             |     no       |
|    3 |            LEAD |Hot Lead          |     no       |
|    4 |        PROSPECT |Not Home          |     no       |
|    5 |            SALE |SOLD              |     no       |
|    6 |            LEAD |Maybe             |     no       |
|    7 |            LEAD |Hot Lead          |     no       |
|    8 |  Not Interested |Not Interested    |     no       |
+------+-----------------+------------------+--------------+

我正在尋求什么答案......

$query = //// THE CORRECT GOODS TO INSERT INTO THIS QUERY ////
  "SELECT
      status_type,
      COUNT(*) as count,
      kpi_type,
      COUNT('kpi') * 100.00 as percentage
  FROM
      main
  GROUP BY
      status_type DESC;";

如果您有任何疑問,請告訴我。

您可以嘗試以下查詢:

    SELECT status_type, kpi_type, Count(*), 
    ((Count(*)  * 100.0)/ (select Count(*) FROM main)) AS Percentage
    FROM main
    GROUP BY status_type, kpi_type
    ORDER BY status_type, kpi_type

補充評論。

$query = "SELECT status_type, kpi_type, Count(*) as count,((Count(*)  * 100.0)/ (select Count(*) FROM main)) AS Percentage
                    FROM main
                    GROUP BY status_type, kpi_type
                    ORDER BY status_type, kpi_type";

我現在沒有MySql在你面前,但你需要得到所有記錄的總數,然后按狀態和KPI做一組像這樣(這是粗略的語法 - 也許它會讓你更接近):

select @Total:=Count(*) from InputTable;
select `Status`, `Kpi`, Count(*) as Status_Count, ((Count(*)  * 100.0)/ @Total) AS KPI_Percentage
from InputTable 
group by `Status`, `Kpi`
order by `Status`, `KPI`;

查詢結果

        Invalid query: You have an error in your SQL syntax; check the manual
     that corresponds to your MySQL server version for the right syntax to use
     near 'select [status_type], [kpi_type], Count(), ((Count() * 100.0)/ @Total) 
    AS Perc' at line 3 Whole query: select @Total =(select Count() from main_pins) 
    select [status_type], [kpi_type], Count(), ((Count(*) * 100.0)/ @Total) 
    AS Percentage from main_pins group by [status_type], [kpi_type] order by [status_type],
 [kpi_type]

在修補Raveenanigam的答案之后能夠得到答案。 感謝大家的幫助和贊成,以獲得一些關注。

$query = "SELECT status_type, kpi_type, Count(*) as count,((Count(*)  * 100.0)/ (select Count(*) FROM main)) AS Percentage
                    FROM main
                    GROUP BY status_type, kpi_type
                    ORDER BY status_type, kpi_type";

結果給出。

[{"status_type":"Hot Lead","KPI":"Lead","Percentage":"0.31596","Count":"2"}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM