Pivot table query using SQL Server 2008 R2

MAK

I have the following table with two fields as shown below:

I want to show the pivot table of the following data into verticle form.

Table: test_10

create table test_10
(
    col1 varchar(10),
    col2 varchar(10)
);

Inserting records:

insert into test_10 values('A','2015-01-01'),('A','2015-01-05'),('A','2015-01-10'),('A','2015-02-15'),
                            ('B','2015-01-01'),('B','2015-01-05'),('B','2015-01-10'),('B','2015-02-15'),
                            ('C','2015-02-02'),('C','2015-02-05'),('C','2015-02-08'),('C','2015-02-16');

Expected Result:

ColX    Jan    Feb
-------------------                     
A       3      1
B       3      1
C       0      4

My try:

SELECT ColX,SUM(Jan) as Jan,SUM(Feb) as after
from
    ( 
        SELECT col1 as ColX,col1,
        (CASE WHEN col2 BETWEEN convert(date,'01-01-2015',105) AND convert(date,'31-01-2015',105) THEN count(col1) ELSE 0 END) as Jan, 
        (CASE WHEN col2 BETWEEN convert(date,'01-02-2015',105) AND convert(date,'28-02-2015',105) THEN count(col1) ELSE 0 end) as Feb
        from test_10
        Group By col1,col2
    ) a
Pivot 
(
    COUNT(col1)
    FOR col1 in([A],[B],[C])
)as pvt
GROUP BY ColX;  

But Getting result:

ColX    Jan    Feb
-------------------
A       1       1
B       1       1
C       0       1   
Deepak Pawar

No need of pivot, try conditional aggregation :

SELECT col1,
       SUM(CASE
             WHEN col2 BETWEEN CONVERT(DATE, '01-01-2015', 105) AND CONVERT(DATE, '31-01-2015', 105) THEN 1
             ELSE 0
           END) AS Jan,
       SUM(CASE
             WHEN col2 BETWEEN CONVERT(DATE, '01-02-2015', 105) AND CONVERT(DATE, '28-02-2015', 105) THEN 1
             ELSE 0
           END) AS Feb
FROM   test_10
GROUP  BY col1 

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

SQL Server 2008 R2: Dynamic Pivot table query performance

SQL Server 2008 R2: Pivot table with count

SQL Server 2008 R2: Pivot table

SQL Server 2008 R2 Dynamic Pivot Query with bit types

Getting Null data while using PIVOT in sql server 2008 R2

SQL Server 2008 R2 - Pivot Usage?

Pivot table in SQL Server 2008

sql server 2008 pivot table

Using multiple like clauses with dapper for paging in a query with SQL Server 2008 R2

SELECT query that Selects from Next Row given certain criteria using SQL Server 2008 R2?

How to create a table with a column that will automatically compute the sum of the other column using SQL Server 2008 R2?

SQL Server 2008 R2: Recursive query

SQL Server 2008 R2: Tuning query

Improve WITH CTE Query Optimization SQL Server 2008 R2

SQL Server 2008 r2: TSQL IIF Query not recognized

Unable to Execute a (DELETE) Query on SQL Server 2008 R2

sql server 2008 R2 management studio table recreation

Use of ISNULL in a PIVOT Query in SQL Server 2008

SQL Server 2008 pivot table with dates

Pivot table Q - SQL Server 2008

SQL Server 2008 R2 - Dynamic Pivot/Unpivot with (moving) Dates

SQL query working fine in SQL Server 2012, but failed to execute in SQL Server 2008 R2

Using query export SQL Server 2008 table to Excel Sheet

SQL Server 2008 R2: Query MS Access from SQL Server

SQL Server 2008 - Pivot

SQL Server 2008 R2 Referencing Clustered Index of Tables with SQL Query

BCP from One Table to Another table in different servers in SQL Server 2008 R2

SQL Server 2008 R2: Update table values which matched with another table

How to pull data from one table based on a temp table SQL Server 2008 R2?