Архивы автора: 1 1

Группировка данных по дням, по месяцам или по годам в MSSQL

По дням:

select convert(char(8),Date_and_Time,112) mydate, count(calls_id)
from calls
group by convert(char(8),Date_and_Time,112)
order by mydate

По месяцам:

select convert(char(6),Date_and_Time,112) mydate, count(calls_id)
from calls
group by convert(char(6),Date_and_Time,112)
order by mydate 

По годам:

select convert(char(4),Date_and_Time,112) mydate, count(calls_id)
from calls
group by convert(char(4),Date_and_Time,112)
order by mydate

Python подключение к базе данных MSSQL

import pyodbc
cnxn = pyodbc.connect("Driver={SQL Server};"
                      "Server=srvmssql;"
                      "Database=mydatabase;"
                      "Trusted_Connection=yes;")
cursor = cnxn.cursor()
cursor.execute('SELECT TOP 10 * FROM MYTABLE')
for row in cursor:
    print('row = %r' % (row,))
input()

MSSQL результат — значения в строку через запятую

Пример запроса:

DECLARE @idName INT
SET @someColumn = 7
DECLARE @s VARCHAR(MAX)
SET @s = ''
SELECT  @s  = @s + CASE WHEN @s <> '' THEN ', ' ELSE '' END + CAST(myColumn AS VARCHAR)
FROM t1
WHERE someColumn = @someColumn
SELECT @s AS ResultList