Головна‎ > ‎SQL‎ > ‎

GROUP BY

Об'єднання функцій за групами результуючих даних
Підрахунок кількості входжень


SELECT Country FROM Customers GROUP BY Country;

-- відсортовані країни без дублікатів



SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country;




SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country
ORDER BY COUNT(CustomerID) DESC;




SELECT Shippers.ShipperName,COUNT(Orders.OrderID) AS NumberOfOrders FROM Orders
LEFT JOIN Shippers ON Orders.ShipperID = Shippers.ShipperID
GROUP BY ShipperName;








string sSQL = "SELECT Count (Id), Departmen FROM [dbo].[Table] GROUP BY Departmen";

string cn_txt = Properties.Settings.Default.cn;

SqlConnection cn_connection = new SqlConnection(cn_txt);
if (cn_connection.State != ConnectionState.Open)
{
     cn_connection.Open();
}

DataTable table = new DataTable();
SqlDataAdapter adapter = new SqlDataAdapter(sSQL, cn_connection);
adapter.Fill(table);

foreach (DataRow row in table.Rows)
{
     tb1.Text +=row.Field<int>(0)+" "+ row.Field<string>(1) + Environment.NewLine;
}