---
title: "Pivot table in SQL Server 2008"  
description: "Pivot table in SQL Server 2008"  
author: "Anonymous User"  
published: 2013-09-28  
updated: 2013-09-28  
canonical: https://www.mindstick.com/forum/1565/pivot-table-in-sql-server-2008  
category: "mssql server"  
tags: ["mssql server"]  
reading_time: 2 minutes  

---

# Pivot table in SQL Server 2008

Please help me out in [SQL server](https://www.mindstick.com/articles/12999/what-is-table-valued-function-in-sql-server) [PIVOT Table](https://www.mindstick.com/forum/160541/how-to-create-pivot-table-in-sql-server). I got the [output](https://www.mindstick.com/interview/34427/explain-the-output-in-angular) like below. Now I want the total [count](https://www.mindstick.com/forum/157774/selecting-count-with-distinct) of allocation [pending](https://answers.mindstick.com/qa/33513/what-is-a-pending-intent) and [coding](https://www.mindstick.com/articles/12290/teaching-coding-from-the-metal-up-or-from-the-glass-back) pending in separate columns under each date row.

```
select ScanDate, filestatus, COUNT(filestatus) as filecount from ScanLog
where FileSource = 'ebridge'
group by filestatus, ScanDate
scandate filestatus filecount
2013-08-01 Allocation Pending 8
2013-08-01 Coding Pending 1
2013-08-02 Allocation Pending 4
2013-08-02 Coding Pending 1
2013-08-03 Allocation Pending 4
2013-08-04 Allocation Pending 18
2013-08-04 Coding Pending 3
2013-08-05 Allocation Pending 6
```

I used the following code but got [error](https://yourviews.mindstick.com/view/88527/fixing-quickbooks-error-4120-reinstalling-vs-repairing) as 'scandate' is not a valid field. Please [guide](https://www.mindstick.com/articles/44463/business-to-business-vat-reclaiming-a-guide-for-your-employees) me.\

```
select [scandate] from ScanLog
pivot (count(scandate)
for filestatus in ([allocation pending],[coding pending])) as A
where FileSource = 'ebridge'
```

## Replies

### Reply by Anonymous User

Hey Mannerheim!\

Try this one -

```
DECLARE @temp TABLE (
      ScanDate DATETIME
    , FileSource VARCHAR(10)
    , FileStatus VARCHAR(30)
    , FileCount INT

)

INSERT INTO @temp
VALUES
    ('2013-08-01', 'ebridge', 'Allocation Pending', 8),
    ('2013-08-01', 'ebridge', 'Coding Pending', 1),
    ('2013-08-02', 'ebridge', 'Allocation Pending', 4),
    ('2013-08-02', 'ebridge', 'Coding Pending', 1),
    ('2013-08-03', 'ebridge', 'Allocation Pending', 4),
    ('2013-08-04', 'ebridge', 'Allocation Pending', 18),
    ('2013-08-04', 'ebridge', 'Coding Pending', 3),
    ('2013-08-05', 'ebridge', 'Allocation Pending', 6)

SELECT *
FROM (
    SELECT scandate, filestatus
    FROM @temp
    WHERE FileSource = 'ebridge'
) t
PIVOT (
    COUNT(scandate)
    FOR filestatus IN ([Allocation Pending], [Coding Pending])
) a
```

\

\

\


---

Original Source: https://www.mindstick.com/forum/1565/pivot-table-in-sql-server-2008

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
