简体   繁体   English

PHP中SQL Server Union查询的回显值和百分比

[英]Echo value and percentage from SQL server Union query in PHP

I have an issue where I'm trying to find the percentage of two queries but want to retain the session value to echo elsewhere. 我有一个问题要尝试查找两个查询的百分比,但想保留会话值以在其他地方回显。 let me explain. 让我解释。

My first union query returns a value EG 100 我的第一个联合查询返回值EG 100

$sql1 = "select COUNT(*) From 
(select inc.INCIDENT_NUMBER as TICKET 
From dbo.HELP_DESK as inc
Where inc.STATUS < 3
and inc.ASSIGNED_GROUP = 'Scheduling'
UNION ALL
SELECT chg.INFRASTRUCTURE_CHANGE_ID AS TICKET
FROM dbo.CHANGE as chg
WHERE chg.CHANGE_REQUEST_STATUS NOT IN (1,4,5,8,9,10,11,12)
and chg.ASGRP in = 'Scheduling') num1";

I then want to echo the result as a session value so: 然后,我想将结果作为会话值进行回显,以便:

$SCH = sqlsrv_query($conn,$sql1);
if( $SCH === false) {
    die( print_r( sqlsrv_errors(), true) );
}
while( $Row = sqlsrv_fetch_array( $SCH, SQLSRV_FETCH_NUMERIC) ) {
$_SESSION['$SCH'] = $Row[0];
}

so far so good. 到现在为止还挺好。 However, I want to calculate the percentage based on my second union query. 但是,我想根据我的第二个联合查询来计算百分比。

$sql2 = "select COUNT(*) From 
(select inc.INCIDENT_NUMBER as TICKET 
From dbo.HELP_DESK as inc
Where inc.STATUS < 4
and inc.ASSIGNED_GROUP = 'Scheduling'
UNION ALL
SELECT chg.INFRASTRUCTURE_CHANGE_ID AS TICKET
FROM dbo.CHANGE as chg
WHERE chg.CHANGE_REQUEST_STATUS < 9
and chg.ASGRP = 'Scheduling') num2"

The second value is 250 as an example. 以第二值为250为例。 So I'm trying to calculate the percentage eg (100/250) * 100 = 40% 所以我试图计算百分比,例如(100/250)* 100 = 40%

function percentage ($sql1,$sql2)
{ 
  return ($sql1/$sql2) * 100;
}
Echo percentage

The last bit pulls back nothing, blank screen. 最后一点没有拉回任何东西,黑屏。 There's probably a much easier way of doing this and given my relative php newcomer status, I'm probably mixing up several functions at once. 可能有一种更简单的方法,并且鉴于我的相对PHP新手的身份,我可能同时混合了几个功能。

I'd appreciate a little help getting it working. 我会很乐意为您提供帮助。 Thanks for taking the time to read. 感谢您抽出宝贵的时间阅读。

Managed to resolve the percentage issues, can output seperate session values. 设法解决百分比问题,可以输出单独的会话值。

select
    s.activeCount * 100 / s.totalCount as Percentage
    FROM
(select activecount = (select COUNT(*) From 
(select inc.INCIDENT_NUMBER AS TICKET
From dbo.HELP_DESK as inc
Where inc.STATUS < 3
and inc.ASSIGNED_GROUP = 'Scheduling'
UNION ALL
SELECT chg.INFRASTRUCTURE_CHANGE_ID AS TICKET
FROM dbo.CHANGE as chg
WHERE chg.CHANGE_REQUEST_STATUS NOT IN (1,4,5,8,9,10,11,12)
and chg.ASGRP = 'Scheduling') num), 
totalCount = (select COUNT(*) From 
(select inc.INCIDENT_NUMBER AS TICKET
From dbo.HELP_DESK as inc
Where inc.STATUS < 4
and inc.ASSIGNED_GROUP = 'Scheduling'
UNION ALL
SELECT chg.INFRASTRUCTURE_CHANGE_ID AS TICKET
FROM dbo.CHANGE as chg
WHERE chg.CHANGE_REQUEST_STATUS < 9
and chg.ASGRP = 'Scheduling') num)) s

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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