.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal
Post New Web Links

Using PIVOT and UNPIVOT for ranged data

Posted By:      Posted Date: September 23, 2010    Points: 0   Category :Sql Server
Hi everyone!

I was just wondering if a SQL SELECT statement can be created for the following:

The table looks like this (TableName = Employees):

EmployeeId INTEGER
Salary MONEY

And the output I need is this:


Salary Range           21 - 25      26 - 30       31 - 35

1000 - 1999               12               15              18

2000 - 2999                6                 4                7

3000 - 3999             

View Complete Post

More Related Resource Links

Using Pivot to arranging data

Hello, I am attempting to optimize a selection query and I thought I would try a PIVOT. My question is simply is this the best way to do it? If not, why and what may be better? Please note the Tmstmp column is not necessarily spaced evenly like this example. Sample table:   Declare @temp table (Tmstmp datetime, Value integer ) INSERT INTO @temp (Tmstmp, Value) VALUES ('01/01/10',1) INSERT INTO @temp (Tmstmp, Value) VALUES ('01/02/10',2) INSERT INTO @temp (Tmstmp, Value) VALUES ('01/03/10',11) INSERT INTO @temp (Tmstmp, Value) VALUES ('01/04/10',1) INSERT INTO @temp (Tmstmp, Value) VALUES ('01/05/10',3) INSERT INTO @temp (Tmstmp, Value) VALUES ('01/06/10',5) INSERT INTO @temp (Tmstmp, Value) VALUES ('01/07/10',11) INSERT INTO @temp (Tmstmp, Value) VALUES ('01/08/10',1) INSERT INTO @temp (Tmstmp, Value) VALUES ('01/09/10',2) ;With CTE as ( SELECT t1.Tmstmp as StartDate, t2.Tmstmp as EndDate, t1.Value as Start_Val, t2.Value as End_Val, Row_Number() Over(Partition By t1.TmStmp ORDER BY DateDiff(day,t1.TmStmp,t2.TmStmp)) as Seq FROM @temp t1 INNER JOIN @temp t2 ON t2.Tmstmp > t1.Tmstmp WHERE t1.Value = 1 AND t2.Value = 11 ) SELECT StartDate, EndDate, [2] as TmStmp2, [3] as TmStmp3, [4] as TmStmp4, [5] as TmStmp5 FROM ( SELECT a.StartDate, a.EndDat

Use power pivot to query the data in MS CRM


Hi All,

I want to query the data in MS CRM (entities) using power pivot from IT Share Point. Can any one guide me in this please.


PIVOT or UNPIVOT howto ...


Hi all

Does anybody knows PIVOT or UNPIVOT ?

I have the following Table:
ID    String
1    ZURN
2    SEP10
3    PUT
4    240.OE
1    UBSN
2    OKT10
3    CALL
4    150.OE

and from this I like to get this:

[Col1]          [Col2]           [Col3]       [Col4]
ZURN           SEP10           PUT          240.OE
UBSN           OKT10          CALL         150.OE

Thanks a lot for a quick feedback.

Best regards
Frank Uray



Hi Guys,

I have the following issue, I am trying to Pivot data from a table I have created


controle_ID, Antwoord_Code_N, ID_antwoordbewerkt, Antwoordbewerkt

616    67X02X04X02    1511690     Wel opmerkingen VI
616    60X01X01          74793        ja
616    60X01X02          74794        ja
616    60X01X03          1511707    IsNull
616    60X01X04          74796        Gecheckt
616    60X02X01          74797        ja
616    60X02X02          1511708    IsNull
616    60X02X03    74799    nee
616    60X02X04    74800    Gecheckt
616    60X02X05    1511709    IsNull


The pivot



Guys, please help me to unpivot data in SQL Server 2000. I have such table [tempdb].[dbo].[EmpCustOrders].

empid A      B      C       D
1       NULL 20     34     NULL
2       52     27     NULL NULL
3       20     NULL 22     30

I need in Write a query to unpivot the data, returning a row for each employee and customer, along with the order quantity.

The result should be:

empid       custid    qty
----------- --------- -----------
1           B         20
1           C      

dynamic pivot data....please help


I having a table with month wise data and data can be for any number of month for different account on ROWS..means data are in table per month per row basic...for 2 month 2 row ands for 3 month 3 row for each account...and so on...


for below data I need below output (2 MONTH),

SELECT 'XYZ', 'DEC', 10, 100 UNION ALL SELECT 'XYZ', 'JAN', 13, 300
SELECT 'PQR', 'DEC', 2, 57

Binding WPF Controls to an Entity Data Model

In this interview, programming writer, McLean Schofield, demonstrates how to bind WPF controls to an entity data model, using Visual Studio 2010 Beta 1. You can also learn more in the topic: Walkthrough: Binding WPF Controls to an Entity Data Model.

Surrogate vs Natural Primary Keys - Data Modeling Mistake 2 of 10

In case you're new to the series I've compiled a list of ten data modeling mistakes that I see over and over that I'm tackling one by one. I'll be speaking about these topics at the upcoming IASA conference in October, so I'm hoping to generate some discussion to at least confirm I have well founded arguments.

The last post in this series Referential Integrity was probably less controversial than this one. After all, who can argue against enforcing referential integrity? But as obvious as surrogate keys may be to some, there is a good deal of diversity of opinion as evidenced by the fact that people continue to not use them.

Editing the Data in a GridView

In addition to deleting a GridView's underlying data, another common need is to allow end users to edit the data displayed in a GridView. With ASP.NET 1.x's DataGrid control, editing the data is certainly possible, but requires creating three event handlers and writing a dozen or so lines of code. With the GridView and ASP.NET 2.0, it is possible to create an editable GridView without writing a single line of code! All of the necessary functionality is encapsulated within the GridView.

The GridView allows editing on a row-by-row basis. An editable GridView contains an additional column with an Edit button in each row. When the end user clicks on an Edit button that row becomes editable, causing the Edit button to change to Update and Cancel buttons and the other columns to become TextBoxes. The end user can then update one or more column values and click Update to save their changes.

Using ASP.NET 3.5's ListView and DataPager Controls: Displaying Data with the ListView

Prior to ASP.NET 3.5, developers who needed to display a set of records could choose between the GridView, DataList, and Repeater controls. The GridView provides rich data features, but has a very boxy layout; the DataList and Repeater allow for a more flexible layout, but lack the "point and click" data features found in the GridView, DetailsView, and FormView controls. The ListView bridges the gap between layout flexibility and built-in data features.

This article, the first in a series of articles on the ListView and DataPager controls, looks at the ListView's many available templates and illustrates how to display data. Read on to learn more!

Dynamic Data Templates in GridView ASP.NET 3.5

At a high level, Dynamic Data Templates can be seen as a system to very quickly build up a User Interface linked with the relevant data model, immediately enabling you to perform CRUD operations (Create, Read, Update, Delete) on the database using the specified data source. They provide a powerful method for building up these kinds of applications, and I'm going to focus on an overview of what Dynamic Data Templates actually are, and a few ways of customizing them to suit industry needs.

MS SQL Server: Search All Tables, Columns & Rows For Data or Keyword Query

If you need to search your entire database for specific data, this query will come in handy.

So when a client needs a custom report or some sort of custom development using Great Plains, most of the time I will have to track down the data in the system by running this query and find the table(s) it is in.

Displaying Data using ASP.NET 2.0 Repeater and C#

This tutorial will show you how to display data using the .NET Repeater Control, ASP.NET 2.0 and C#.NET

The Repeater control is a powerful tool and is easy to use.

First, you will need to import the System.Data.SqlClient namespace.

MVC architecture in ASP.Net using C# and Microsoft Data Access Application block

The Model-View-Controller (MVC) pattern separates the modeling of the domain, the presentation, and the actions based on user input into three separate classes [Burbeck92]:

Model. The model manages the behavior and data of the application domain, responds to requests for information about its state (usually from the view), and responds to instructions to change state (usually from the controller).

View. The view manages the display of information.

Controller. The controller interprets the mouse and keyboard inputs from the user, informing the model and/or the view to change as appropriate.

ASP.NET 4.0 Dynamic Data and Many to Many Entity Framework Entities

I did not play much with Dynamic Data controls in VS2008 and just made a cool discovery in VS2010 Beta 2. This may not even be new, but as I'm sitting 30,000+ feet over the Atlantic ocean, I don't have access to VS2008 at the moment to check.

Display data in gridview dynamicallay

I have AD domain which has 100000 users, I want to display all the users in gridview controls.Actually it is displaying users in gridview after parsing entire domain which takes around 30 min. I want to display the users while loading itself.It is continously getting user information from AD domain and getting each user details, I want to display the users information which already queried instead of waiting till getting all users information

asp.net display data in gridview

i want to display data in gridview binded to a sql datasource
but the problem part is i want to bind a field of that datasource to a dropdownlist and when a item is selected in that dropdownlist then correcposnding to that data will be displayed in gridview...
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend