触摸屏租赁
Touch screen rental
广告机租赁
Advertising rental
机器人租赁
Robot leasing
 当前位置:首页 > > sql合计和统计

统计不同productname的个数
select productname, COUNT(*) FROM   product GROUP BY productname
统计productname>1的个数(可查询有无重复)
select productname, COUNT(*) FROM   product GROUP BY productname HAVING COUNT(*)>1

合计数量
select sum(Inventory) as 总计 from product
统计不同Inventory的个数
select Inventory, COUNT(*) as 个数 FROM   product GROUP BY Inventory

select Inventory,ZF,sum(bid*Inventory) as 合计 from product group by Inventory,ZF
select sum(bid*Inventory) as 总计 from product

例:
前台:countsellproduct.aspx
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td height="20px" width="30%" class="but" align="center">部件名称</td>
<td height="20px" width="40%" class="but" align="center">ZF号</td>
<td height="20px" width="10%" class="but" align="center">数量合计</td>
<td height="20px" width="20%" class="but" align="center">金额</td>
</tr>
</table>
<asp:DataList ID="DataList1" runat="server" Width="100%" CellPadding="0"  DataKeyField="zf">
<ItemTemplate>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td height="20" width="30%" class="but" align="center"><%# DataBinder.Eval(Container, "DataItem.productname ")%></a></td>
<td height="20" width="40%" class="but" align="center"><%# DataBinder.Eval(Container, "DataItem.zf")%></td>
<td height="20" width="10%" class="but" align="center"><%# DataBinder.Eval(Container, "DataItem.Inventory")%></td>
<td height="20" width="20%" class="but" align="center"><%# DataBinder.Eval(Container, "DataItem.bid2")%></td>
</tr>
</table>
</ItemTemplate>
   
</asp:DataList>
<table width="100%" cellpadding="0" cellspacing="0">
<tr>
<td style="text-align: right; height:20px;">
<asp:Label ID="Label7" runat="server" Text="当前页码为:"></asp:Label>
[
<asp:Label ID="labPage" runat="server" Text="1"></asp:Label>
]
<asp:Label ID="Label6" runat="server" Text="总页码为:"></asp:Label>
[
<asp:Label ID="labBackPage" runat="server"></asp:Label>
]<asp:LinkButton ID="lnkbtnOne" runat="server" Font-Underline="False" ForeColor="black"
OnClick="lnkbtnOne_Click">第一页</asp:LinkButton>
<asp:LinkButton ID="lnkbtnUp" runat="server" Font-Underline="False" ForeColor="black"
OnClick="lnkbtnUp_Click">上一页</asp:LinkButton>
<asp:LinkButton ID="lnkbtnNext" runat="server" Font-Underline="False" ForeColor="black"
OnClick="lnkbtnNext_Click">下一页</asp:LinkButton>
<asp:LinkButton ID="lnkbtnBack" runat="server" Font-Underline="False" ForeColor="black"
OnClick="lnkbtnBack_Click">最后一页</asp:LinkButton> </td>
</tr>
</table> 

后台:countsellproduct.aspx.cs
 public void BindData()
    {

        //从配置文件获取数据库连接串
        string strCon = ConfigurationManager.AppSettings["conStr"];
        SqlConnection sqlcon = new SqlConnection(strCon);
        sqlcon.Open();
        int curpage = Convert.ToInt32(this.labPage.Text);
        PagedDataSource ps = new PagedDataSource();
        string sqlstr = "select productname,zf,sum(Inventory) as Inventory,sum(bid2) as bid2  from jxc_sellproduct  where zf is not null " + Session["sqlfind"] + "   group by productname,zf order by sum(Inventory) desc";
        
        SqlDataAdapter MyAdapter = new SqlDataAdapter(sqlstr, sqlcon);
        DataSet ds = new DataSet();
        MyAdapter.Fill(ds, "zf");
        ps.DataSource = ds.Tables["zf"].DefaultView;
        ps.AllowPaging = true; //是否可以分页
        ps.PageSize = 10; //显示的数量
        ps.CurrentPageIndex = curpage - 1; //取得当前页的页码
        this.lnkbtnUp.Enabled = true;
        this.lnkbtnNext.Enabled = true;
        this.lnkbtnBack.Enabled = true;
        this.lnkbtnOne.Enabled = true;
        if (curpage == 1)
        {
            this.lnkbtnOne.Enabled = false;//不显示第一页按钮
            this.lnkbtnUp.Enabled = false;//不显示上一页按钮
        }
        if (curpage == ps.PageCount)
        {
            this.lnkbtnNext.Enabled = false;//不显示下一页
            this.lnkbtnBack.Enabled = false;//不显示最后一页
        }
        this.labBackPage.Text = Convert.ToString(ps.PageCount);
        this.DataList1.DataSource = ps;
        this.DataList1.DataKeyField = "zf";
        this.DataList1.DataBind();
        sqlcon.Close();
    }

合计数量
select sum(Inventory) as 合计 from product
select sum(Inventory) as 合计 from jxc_buyproduct
总计金额
select sum(bid*Inventory) as 总计 from product
select sum(bid*Inventory) as 总计 from jxc_buyproduct
查询
select zf,Inventory  from product order by Inventory desc , zf asc
select zf,Inventory from jxc_buyproduct order by Inventory desc , zf asc


工作室地址:重庆石桥铺电脑城B座 | 微信:z35544216 | 网址:www.35544216.com