N
N
Nikolai Trifonov2015-10-22 10:15:30
SQL
Nikolai Trifonov, 2015-10-22 10:15:30

How to display an array from a database grouped by column values?

I need to display the result of this query

SELECT
r.name region
, o.name organization
, co.name object
, b.name boiler
, b.inventory_number
, hm.counts_number
, c.start_date date
, co.address
, LTRIM(RTRIM(c.number)) number
, bu.name budget
FROM com_jur2015..contracts c
LEFT JOIN com_jur2015..budgets bu ON bu.id = c.id_budget 
LEFT JOIN com_jur2015..contract_objects co ON co.number = c.number 
LEFT JOIN com_jur2015..organizations o ON o.id = c.id_organization 
LEFT JOIN com_jur2015..cities ci ON ci.id = co.id_city 
LEFT JOIN com_jur2015..municipals m ON ci.id_municipal = m.id 
LEFT JOIN com_jur2015..regions r ON r.id = co.id_region 
LEFT JOIN com_jur2015..boilers b ON b.id = co.id_boiler 
LEFT JOIN com_jur2015..counts hm ON o.id = hm.id_organization 
WHERE c.dead = 0 AND r.name IS NOT NULL 
ORDER BY r.name, o.name, co.name

in this form
| Region |
| Budget type |
Organization | Object | Boiler room | etc.
Now I'm outputting it like this:
Sort the rows in advance through the query and output by comparing the current value of the column with the previous one
SqlDataReader reader = query.ExecuteReader();
            int i = 7;
            string region = "Ноль";
            string budget_type = "Ноль";
            while (reader.Read())
            {
                //string date_after_convert;
                //string date_before_convert = reader["date"].ToString();

                //форматирование времени, т.к. дата вытаскивается в таком виде гггг-мм-дд чч:мм:сс

                //if (date_before_convert == "") date_after_convert = "";
                //else date_after_convert = DateTime.Parse(date_before_convert).ToShortDateString();

                //вывод [0] столбца в консоль, для проверки корректности получения данных
                //чтоб проверить вывод, в свойствах выбери console app
                //Console.WriteLine(reader[0]);
                
                //херится красивый внешний вид, если нарушается порядок сортировки
                //надо найти аналог group_concat из mysql
                if (region != reader["region"].ToString()) {
                    region = reader["region"].ToString();
                    ws.Range["A" + i + ":O" + i].Merge();
                    ws.Range["A" + i + ":O" + i].BorderAround2(XlLineStyle.xlContinuous, XlBorderWeight.xlMedium);
                    ws.Range["A" + i + ":O" + i].Value = region;
                    i++;
                }
                if (budget_type != reader["budget"].ToString()) {
                    budget_type = reader["budget"].ToString();
                    ws.Range["A" + i + ":O" + i].Merge();
                    ws.Range["A" + i + ":O" + i].BorderAround2(XlLineStyle.xlContinuous, XlBorderWeight.xlMedium);
                    ws.Range["A" + i + ":O" + i].Value = budget_type;
                    i++;
                }
                ws.Range["A"+i].Value = reader["organization"].ToString();
                ws.Range["B"+i].Value = reader["object"].ToString();
                //Берем название котельной, вырезаем слово "котельная", подаем на вывод
                ws.Range["C" + i].Value = reader["boiler"].ToString().Replace("котельная","")+" "+reader["inventory_number"].ToString();
                ws.Range["F" + i].Value = reader["counts_number"].ToString();
                i++;
                //= reader["region"].ToString(),
            }

Everything works, but it seems to me that it could be done better and more beautiful

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Kuzmin, 2015-10-22
@fridriekh

Add a Group By to the query

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question