在写一个sql,select * from (select count(*) m from Product where ProductPrice>100) more,(select count(*) l from Product where ProductPrice<100) less group by more.m, less.l
这条语句可以查出来俩列,m 商品价格>100的数量,l 商品价格<100的数量
这是表结构
语句结果:
我需要的效果是两个列,一个列的值为m,l 另一个列的值对应上面的8,47 ,但我查询到的m,l都是列名了,不重建表的情况下怎么把他俩列名放一个列里,求解求解
是两行吧,第一行为:m,l ,第二行为:8,47。
select
concat(sum(case when ProductPrice>100 then 1 else 0 end),',',sum(case when ProductPrice<=100 then 1 else 0 end)) AS 'm,l'
FROM
Product
select * from (select count(*) c from Product where ProductPrice > 100) more group by more.c
UNION ALL
select * from (select count(*) c from Product where ProductPrice < 100) less group by less.c
你们的代码,我在数据库去试了下,但我需要的是像这种的效果,因为需要把语句在后台查到,然后弄个饼状图显示
先上代码:
<!DOCTYPE html>
@using WebMatrix.Data;
<html>
<head>
<title></title>
</head>
<body>
@{
var db = Database.Open("sqlcon");
var dbdata = db.Query("select * from (select count(*) m from Product where ProductPrice>100) more,(select count(*) l from Product where ProductPrice<100) less group by more.m, less.l");
var chart = new Chart(600,500,ChartTheme.Vanilla);
chart.AddTitle("商品各个价位数量比例");
chart.AddSeries(
name: "价位商品数统计",
chartType: "pie",
xValue: dbdata,
xField: "m",
yValues: dbdata,
yFields: "l"
);
chart.Write();
}
</body>
</html>
我在学C# web page 这是前后端代码混写那种方式,我用加粗和下划线标的,是我目前理解的关键的两个属性,对应着我需要的两个列,xField 后面应该是代表我的一个列,这个列需要两条数据,也就是m,l,yFields 后面需要的是我查出来的对应的数,这俩列名放上去应该是可以生成一个饼状图的。
所以我需要的效果应该是类似于这样的:
示例的库:
库里的表:
需要类似这样结构的结果:
select Sex,count(*) as [count] from stars group by Sex
在百度上找了很久,没有找到过类似的,我是想把价格>100的数和<100的数像男女这样子展示,然后用到饼状图上。
非常感谢大神的解答,我变通了一下,只求了一个列,在Xfiled 和yfileds里都用了,效果实现了,文本相信应该不会很难