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 |
| Ankit | vicks | 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();
}
{ // 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.
17 Replies
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.

Ramesh MaruthiPosted Jul 22, 2014, 9:46 AM
can you try in this way
SELECT P_sup,p_name,SUM(S_PQTY)as ast,SUM(S_Ptotal) as atb
FROM Stock
GROUP BY
p_name
ORDER BY
ast ASC
, atb
Rajesh PathakotiPosted Jul 23, 2014, 2:02 AM
okay we are enjoyed to share the knowledge and gain the knowledge. :)
varsha dodiyaPosted Jul 23, 2014, 1:58 AM
yhanks for ur time. :)
Rajesh PathakotiPosted Jul 23, 2014, 1:57 AM
You want O/P like this na, i tried its coming
varsha dodiyaPosted Jul 23, 2014, 1:54 AM
Rajesh PathakotiPosted Jul 23, 2014, 1:42 AM
Ramesh MaruthiPosted Jul 23, 2014, 1:40 AM
Rajesh PathakotiPosted Jul 23, 2014, 1:39 AM
In my first answer i missed one column, now final query is,
select max(p_sup) as P_Sup,max(P_name) as P_Name,sum(S_PQTY) as Qty,sum(S_Ptotal) as S_Portal from Stock group by P_name order by sum(S_PQTY)
varsha dodiyaPosted Jul 23, 2014, 1:38 AM
varsha dodiyaPosted Jul 23, 2014, 1:37 AM
Rajesh PathakotiPosted Jul 23, 2014, 1:34 AM
see once
select P_name,sum(S_PQTY) as Qty,sum(S_Ptotal) as S_Portal from Stock group by P_name order by sum(S_PQTY)
Ramesh MaruthiPosted Jul 23, 2014, 1:32 AM
FROM Stock
GROUP BY
p_name
,P_sup
ORDER BY
ast ASC
, atb
varsha dodiyaPosted Jul 23, 2014, 1:26 AM
Ramesh MaruthiPosted Jul 23, 2014, 1:24 AM
SELECT P_sup,p_name,SUM(S_PQTY)as ast,SUM(S_Ptotal) as atb
FROM Stock
GROUP BY
p_name
,P_sup
ORDER BY
ast ASC
, atb
varsha dodiyaPosted Jul 23, 2014, 1:18 AM
Ramesh MaruthiPosted Jul 23, 2014, 1:14 AM
if u didn't tried can you please add one line under groupby
p_name as , p_sup
varsha dodiyaPosted Jul 23, 2014, 1:02 AM