Sqlserver
 sql >> Teknologi Basis Data >  >> RDS >> Sqlserver

Dapatkan hitungan untuk beberapa bidang menggunakan grup dengan di SQL

select data.category, cl.combovalue as esilocation, cd.combovalue as esidispensary,
    year(date) as year, month(date) as month,
    sum(data.joins) as [Joining Count], sum(data.terms) as [Termination Count]
from (
    select category, esilocation, esidispensary, dateofjoining as date,
           1 as joins, 0 as terms
    from dbo.employeedetail
    where dateofjoining is not null
    union all
    select category, esilocation, esidispensary, terminationdate as date,
           0 as joins, 1 as terms
    from dbo.employeedetail
    where terminationdate is not null
) data
left join dbo.combovalues cl on cl.id = data.esilocation
left join dbo.combovalues cd on cd.id = data.esidispensary
where category in ( 1, 2 ) 
and date >= '2014-01-01' 
and date <= '2014-12-31'
group by data.category, cl.combovalue, cd.combovalue, year(date), month(date)


  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Akses TimeZoneInfo dari SQL 2005 Server

  2. Apakah mungkin untuk menghapus log di SQL Server 2008 menggunakan kueri?

  3. SQL Server :UPDATE MyTable SET col1 =nilai, col2 =col1

  4. Bagaimana cara mengekstrak data dari PDF?

  5. Menggunakan tabel Temp di SSIS