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.


Ramesh MaruthiRank #937 · 2.2K reputationPosted Jul 22, 2014, 5:46 AM EDT
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
varsha dodiyaRank #4,270 · 354 reputationPosted Jul 22, 2014, 9:02 PM EDT
Ramesh MaruthiRank #937 · 2.2K reputationPosted Jul 22, 2014, 9:14 PM EDT
if u didn't tried can you please add one line under groupby
p_name as , p_sup
varsha dodiyaRank #4,270 · 354 reputationPosted Jul 22, 2014, 9:18 PM EDT
Ramesh MaruthiRank #937 · 2.2K reputationPosted Jul 22, 2014, 9:24 PM EDT
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 dodiyaRank #4,270 · 354 reputationPosted Jul 22, 2014, 9:26 PM EDT
Ramesh MaruthiRank #937 · 2.2K reputationPosted Jul 22, 2014, 9:32 PM EDT
FROM Stock
GROUP BY
p_name
,P_sup
ORDER BY
ast ASC
, atb
Rajesh Pathakoti161 reputationPosted Jul 22, 2014, 9:34 PM EDT
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)
varsha dodiyaRank #4,270 · 354 reputationPosted Jul 22, 2014, 9:37 PM EDT
varsha dodiyaRank #4,270 · 354 reputationPosted Jul 22, 2014, 9:38 PM EDT
Rajesh Pathakoti161 reputationPosted Jul 22, 2014, 9:39 PM EDT
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)
Ramesh MaruthiRank #937 · 2.2K reputationPosted Jul 22, 2014, 9:40 PM EDT
Rajesh Pathakoti161 reputationPosted Jul 22, 2014, 9:42 PM EDT
varsha dodiyaRank #4,270 · 354 reputationPosted Jul 22, 2014, 9:54 PM EDT
Rajesh Pathakoti161 reputationPosted Jul 22, 2014, 9:57 PM EDT
You want O/P like this na, i tried its coming
varsha dodiyaRank #4,270 · 354 reputationPosted Jul 22, 2014, 9:58 PM EDT
yhanks for ur time. :)
Rajesh Pathakoti161 reputationPosted Jul 22, 2014, 10:02 PM EDT
okay we are enjoyed to share the knowledge and gain the knowledge. :)