Sqlserver
 sql >> Baza danych >  >> RDS >> Sqlserver

SQL Server 2008:Policz liczbę kluczy w wielu zakresach dat

SELECT COUNT(*),  Year(pm.order_dts) as Year
FROM dm.medications pm
WHERE Year(pm.order_dts) between 1996 AND 2008
group by Year(pm.order_dts)

Jeśli chcesz to wszystko w jednym rzędzie, możesz zrobić:

SELECT 
    sum(case when Year(pm.order_dts) = 1996 then 1 end) as Count1996,
    sum(case when Year(pm.order_dts) = 1997 then 1 end) as Count1997,
    sum(case when Year(pm.order_dts) = 1998 then 1 end) as Count1998,
    sum(case when Year(pm.order_dts) = 1999 then 1 end) as Count1999,
    sum(case when Year(pm.order_dts) = 2000 then 1 end) as Count2000,
    sum(case when Year(pm.order_dts) = 2001 then 1 end) as Count2001,
    sum(case when Year(pm.order_dts) = 2002 then 1 end) as Count2002,
    sum(case when Year(pm.order_dts) = 2003 then 1 end) as Count2003,
    sum(case when Year(pm.order_dts) = 2004 then 1 end) as Count2004,
    sum(case when Year(pm.order_dts) = 2005 then 1 end) as Count2005,
    sum(case when Year(pm.order_dts) = 2006 then 1 end) as Count2006,
    sum(case when Year(pm.order_dts) = 2007 then 1 end) as Count2007,
    sum(case when Year(pm.order_dts) = 2008 then 1 end) as Count2008
FROM dm.medications pm


  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Jaki jest najlepszy sposób na zbiorcze wstawianie bazy danych z c#?

  2. TSQL - Dodaj kolumnę do wszystkich tabel w bazie danych [Przykład kursora]

  3. Zapytanie SQL dla 7-dniowej średniej kroczącej w SQL Server

  4. Jak filtrować wiersze z wartościami null w instrukcji Select w SQL Server — samouczek SQL Server / TSQL, część 110

  5. SQL Jak zaktualizować SUMA kolumny nad grupą w tej samej tabeli