473,412 Members | 2,262 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,412 software developers and data experts.

SQL Server - how to create a pivot from a select query

Does anybody knows a simple way to create a pivot of this result:

Expand|Select|Wrap|Line Numbers
  1. [0-1]  [1-2]  [2-3]  [3-4]
  2. 12      45      29    5
This result is created with query:

Expand|Select|Wrap|Line Numbers
  1.  SELECT [0-1]   = SUM(CASE WHEN AGE >= 0 AND AGE <= 1 THEN 1 ELSE 0 END),
  2.        [1-2]  = SUM(CASE WHEN AGE > 1 AND AGE <= 2 THEN 1 ELSE 0 END),
  3.        [2-3] = SUM(CASE WHEN AGE > 2 AND AGE <= 3 THEN 1 ELSE 0 END),
  4. [3-4] = SUM(CASE WHEN AGE > 3 AND AGE <= 4 THEN 1 ELSE 0 END)
  5.     FROM dbo.Persons
I want to show it like columns should become rows i.e it should be:

Expand|Select|Wrap|Line Numbers
  1. [0-1]    12
  2. [1-2]    45
  3. [2-3]    29
Please suggest a simple and easy to follow solution.
Aug 21 '13 #1

✓ answered by tofos

Hi,

try this:

Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.     CASE WHEN AGE >= 0 AND AGE <= 1 THEN '[1-2]'
  3.             WHEN AGE > 2 AND AGE <= 3 THEN '[2-3]'
  4.             WHEN AGE > 3 AND AGE <= 4 THEN '[3-4]'
  5.             ELSE '[>4]' END Range
  6.     ,count(CASE WHEN AGE >= 0 AND AGE <= 1 THEN '[1-2]'
  7.             WHEN AGE > 2 AND AGE <= 3 THEN '[2-3]'
  8.             WHEN AGE > 3 AND AGE <= 4 THEN '[3-4]'
  9.             ELSE '[>4]' END) Qty
  10. FROM dbo.Persons
  11. GROUP BY
  12.     CASE WHEN AGE >= 0 AND AGE <= 1 THEN '[1-2]'
  13.             WHEN AGE > 2 AND AGE <= 3 THEN '[2-3]'
  14.             WHEN AGE > 3 AND AGE <= 4 THEN '[3-4]'
  15.             ELSE '[>4]' END

4 1647
tofos
2
Hi,

try this:

Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.     CASE WHEN AGE >= 0 AND AGE <= 1 THEN '[1-2]'
  3.             WHEN AGE > 2 AND AGE <= 3 THEN '[2-3]'
  4.             WHEN AGE > 3 AND AGE <= 4 THEN '[3-4]'
  5.             ELSE '[>4]' END Range
  6.     ,count(CASE WHEN AGE >= 0 AND AGE <= 1 THEN '[1-2]'
  7.             WHEN AGE > 2 AND AGE <= 3 THEN '[2-3]'
  8.             WHEN AGE > 3 AND AGE <= 4 THEN '[3-4]'
  9.             ELSE '[>4]' END) Qty
  10. FROM dbo.Persons
  11. GROUP BY
  12.     CASE WHEN AGE >= 0 AND AGE <= 1 THEN '[1-2]'
  13.             WHEN AGE > 2 AND AGE <= 3 THEN '[2-3]'
  14.             WHEN AGE > 3 AND AGE <= 4 THEN '[3-4]'
  15.             ELSE '[>4]' END
Aug 21 '13 #2
Rabbit
12,516 Expert Mod 8TB
Please use code tags when posting code or formatted data.

By the way, there's no need for the case within the count, you can just use count(*).
Aug 21 '13 #3
Thank you tofos for your reply!
This shows the count as expected but the only thing is the sort order. The result is:
Range
[1-2]
[3-4]
[2-3]
[0-1]
[>4]

Can you suggest how can i show them as sorted ie. range [0-1] should come first followed by [1-2], [2-3] and so on.

Can i add one more column as sort order and set order by on that.

Please advise.

Thanks
Aug 22 '13 #4
tofos
2
Rabbit, thanks for advice.
Radha, how about this:

Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.     CASE WHEN AGE >= 0 AND AGE <= 1 THEN '[0-1]'
  3.             WHEN AGE > 1 AND AGE <= 2 THEN '[1-2]'
  4.             WHEN AGE > 2 AND AGE <= 3 THEN '[2-3]'
  5.             WHEN AGE > 3 AND AGE <= 4 THEN '[3-4]'
  6.             ELSE '[4...]' END Range
  7.     ,count(*) Qty
  8. FROM dbo.Persons
  9. GROUP BY
  10.     CASE WHEN AGE >= 0 AND AGE <= 1 THEN '[0-1]'
  11.             WHEN AGE > 1 AND AGE <= 2 THEN '[1-2]'
  12.             WHEN AGE > 2 AND AGE <= 3 THEN '[2-3]'
  13.             WHEN AGE > 3 AND AGE <= 4 THEN '[3-4]'
  14.             ELSE '[4...]' END
  15. ORDER BY
  16.     CASE WHEN AGE >= 0 AND AGE <= 1 THEN '[0-1]'
  17.             WHEN AGE > 1 AND AGE <= 2 THEN '[1-2]'
  18.             WHEN AGE > 2 AND AGE <= 3 THEN '[2-3]'
  19.             WHEN AGE > 3 AND AGE <= 4 THEN '[3-4]'
  20.             ELSE '[4...]' END
Aug 22 '13 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Memduh Durmaz | last post by:
Hi, I'm using DB2 UDB 7.2. Also I'm doing some tests on SQL Server 2000 for some statements to use efectively. I didn't find any solution on Sql Server about WITH ... SELECT structure of DB2. ...
2
by: Craig | last post by:
Acces 2000: I have several 'lookup' type tables with a standard field naming convention of id(pk) and name in a medium sized database. When I create a select query involving 3 or 4 of these tables...
2
by: Ravman | last post by:
How could I create a "select query" in Access that would find an employee whose first and last name begin with the same letter? Fields: Fname, Lname Table name: employee Thank you in advance.
4
by: Alan Lane | last post by:
Hello world: I'm including both code and examples of query output. I appologize if that makes this message longer than it should be. Anyway, I need to change the query below into a pivot table...
4
by: Ajay | last post by:
Hello all, I used to display reports in Excel earlier on my website. Now the client has requested that he would like see reports in Pivot table on the web . The backend is Sql2000. Can you please...
1
by: Razia | last post by:
Hello everyone I want to create a select query according to the values in a single dimension array Select * from <table Name> where <Array items> Can anybody give me some suggestions
1
by: Phil | last post by:
Is it possible to swap rows and columns in select query output so that each record's data is displayed in a column? I want to collect data each day and display it in a query with each day's date...
5
by: TomH | last post by:
How do I create one SELECT query in VB .NET that pulls from a SQL Server database and a MS Access database? The SQL server table has a unique identifier as a PK and the Access table has a...
5
mikek12004
by: mikek12004 | last post by:
doing a (not so simple) query -which works fine in sql server using PDO like this: // attempt a connection try { $pdo = new...
1
by: TZEM | last post by:
Hi - I'm new to Access and trying to create a complicated database that records info about system interfaces. I want to create a crosstab query that returns a field from the interface record, if a...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.