varsha dodiya

varsha dodiya

  • NA
  • 407
  • 75.2k

GROUP BY With Order by for multiple columns

Jul 22 2014 8:21 AM
hie Geeks ,
 i'm facing trouble with Group by with order by clause in sql
 I have sql table(STOCK) in visual studio 2010 , 
P_sup
P_name S_PQTY
S_Ptotal
varsha disprin 19 20
priya crocin 2 10
Opening disprin 12 20
Ankit combiflame 4 30
Ankitvicks
6
20
 
 
I want P_name values to be appear at once .
 
 
  P_sup               P_name         S_Pqty         S_Ptotal
varsha               disprin                     31                  40
priya                  crocin                     2                  10
Ankit                  combiflame         4                     30   
Ankit                  vicks                        6                  20   
 
 
and also order by S_Pqty  and result table should be like this
 
  P_sup       P_name          S_Pqty       S_Ptotal
priya           crocin                 2                    10
Ankit          combiflame         4                   30
Ankit             vicks                   6                    20
varsha           disprin             31                      40 
 
this is what i tried
 private void SHRTSTK()
{ // sort acc to short stock
    cmd = new SqlCommand("select S_P_name ,SUM(S_P_savail) as ast,SUM(S_P_tavail) as atb , SUM(S_P_grsv) as grs from STOCK group by S_P_name      order by S_P_savail", conn);
     conn.Open();
    dr = cmd.ExecuteReader();
           while (dr.Read())
          {
             int i = 0;
               dataGridView6.Rows.Insert(i);
               dataGridView6.Rows[i].Cells[5].Style.ForeColor = Color.Red;
             dataGridView6.Rows[i].Cells["S_Psname1"].Value = dr["S_Psname"].ToString();
             dataGridView6.Rows[i].Cells["S_P_name1"].Value = dr["S_P_name"].ToString();
             dataGridView6.Rows[i].Cells["S_P_savail"].Value = dr["ast"].ToString();
            dataGridView6.Rows[i].Cells["S_P_tavail"].Value = dr["atb"].ToString();
            dataGridView6.Rows[i].Cells["S_P_grsv"].Value = dr["grs"].ToString();

                i++;
           }
     conn.Close();
}
 
 
 help me in query plz.
 

 

Answers (17)