获取在C#中运行的多个Sql命令

时间:2016-05-03 06:26:16

标签: c# sql

我正在尝试获取特定名称的分数计数值,如下所示

var query = "select round((cast(x.num as float) / cast(y.num as float)*100),0) as Q from (select count(*) as num from VisitDistribution  where Name = '" + data.K + "' and Visits ='" +
                            c +
                            "') x join (select count(*) as num from VisitDistribution where Name = '" + data.K + "' ) y on 1=1 ";

我想要做的是以相同的方法执行以下命令

var query = "select round((cast(x.num as float) / cast(y.num as float)*100),0) as Q from (select count(*) as num from VisitDistribution  where Name = '" + data.C + "' and Visits ='" +
                            c +
                            "') x join (select count(*) as num from VisitDistribution where Name = '" + data.C + "' ) y on 1=1 ";

我需要使用angular js从这些查询中获取值以分隔列。我可以这样做,但问题是我无法同时执行这两个查询,并将结果值添加到列表的相应列。

3 个答案:

答案 0 :(得分:0)

    var query = "select round((cast(x.num as float) / cast(CASE WHEN y.num = 0 THEN 1 ELSE y.num END as float)*100),0) as Q, round((cast(a.num as float) / cast(CASE WHEN b.num = 0 THEN 1 ELSE b.num END as float)*100),0) as R from 
                (select count(*) as num from VisitDistribution  where Name = '" + data.K + "' and Visits ='" + c + "' ) x 
                join (select count(*) as num from VisitDistribution where Name = '" + data.K + "' ) y on 1=1
                join (select count(*) as num from VisitDistribution  where Name = '" + data.C + "' and Visits ='" + c + "' ) a on 1=1
                join (select count(*) as num from VisitDistribution where Name = '" + data.C + "' ) b on 1=1"

答案 1 :(得分:0)

使用CROSS JOIN

尝试此操作
SELECT T1.Columns1 , T2.Columns2   FROM TABLE1 AS T1  CROSS JOIN  TABLE2 AS T2

答案 2 :(得分:0)

你可能想试试这个。第一列为data.K提供值,第二列提供data.C。

"Select SUM( case when Z.Name = data.K then Z.Q else 0 end) K,
SUM( case when Z.Name = data.C then Z.Q else 0 end) C

(select X.Name, 

round((cast(X.num as float)/cast(y.num as float)*100,0) as Q

from (select X.Name,count(*) as num from VisitDistribution where Name
IN (' + data.K + "','" + data.C + "') and Visits ='" + c +
"' group by Name) x join (Y.Name,select count(*) as num from 
VisitDistribution where Name IN (' + data.K + "','" + data.C + "' Group by 
Name) y on x.Name = Y.Name) Z"; 

希望这有帮助!

感谢, 卡塔斯