473,387 Members | 1,693 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,387 software developers and data experts.

A Query-Transpose of Data

I am supplying you with Sample Data:-

Initial Classcode SampleSize Average
------- ---------- -------------------------------
ADK SSC 22 3.6800000000000002
ADK TSC 17 2.7599999999999998
ADK TSM 5 3.5499999999999998
ANB FCA 31 3.23
ANB FCB 50 3.0499999999999998
ANB FCC 30 3.0899999999999999
ANB SCA 35 3.02
ANB SCB 9 3.4300000000000002
ANB TCA 30 2.77
ANB TCB 6 1.8799999999999999
APG MCH 10 3.8300000000000001
APG TSCH 9 4.21
AUG FCC 30 3.5499999999999998
AUG SCA 28 2.7800000000000002
AUG SCB 29 3.4300000000000002
AUG SCC 30 2.8999999999999999
AUG TCA 30 2.8599999999999999
AUG TCB 29 2.1200000000000001
AVK TSP 12 3.6200000000000001
BKK FS 32 2.52
BKK TSM 5 3.3799999999999999
BSK SSP 28 3.1200000000000001
BSK TSP 12 3.0600000000000001
------- ---------- -------------------------------

These are the averages of teachers grouped by initial.
Maximum 7 averages are related to each teacher.
Ignore the column SampleSize.
Using this output is it possible to get output like this:

Initial Class1 Avg1 Class2 Avg2 Class3 Avg3 Class4 Avg4..
ADK TSA 1.4 TSB 2.5 TSC 4.5 SSC 5.0..
ANB SSA 1.4 SSB 2.5 NULL NULL NULL NULL..
APG .........................
AVK ....................
BKK ....................
BSK .....................
Since the maximum class nos are 7, those having less than classes will

contain NULL in the class and average field.

Is it possible to carry out this in single query?

-Sameer

Jul 23 '05 #1
1 4581
Sameer (sa******@gmail.com) writes:
These are the averages of teachers grouped by initial.
Maximum 7 averages are related to each teacher.
Ignore the column SampleSize.
Using this output is it possible to get output like this:

Initial Class1 Avg1 Class2 Avg2 Class3 Avg3 Class4 Avg4..
ADK TSA 1.4 TSB 2.5 TSC 4.5 SSC 5.0..
ANB SSA 1.4 SSB 2.5 NULL NULL NULL NULL..
APG .........................
AVK ....................
BKK ....................
BSK .....................
The averages in the output does not seem to agree with the averages
in the result. But there is no indication of any calculation.
Since the maximum class nos are 7, those having less than classes will
contain NULL in the class and average field.

Is it possible to carry out this in single query?


Single query may be possible, but I leave that to the purists. I would
use a temp table:

INSERT #temp (initial, class, average, average)
SELECT initial, class average, (SELECT COUNT(*)
FROM tbl b
WHERE a.class <= b.class)
FROM tbl

Then it a seven-way self-join:

SELECT a.initial, a.class, a.avg, b.class, b.avg, ...
FROM #temp a
LEFT JOIN #temp b ON a.initial = b.initial
AND b.classno = 2
LEFT JOIN #temp c ON a.initial = c.initial
AND c.classno = 2
...
WHERE a.classno = 1
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Harvey | last post by:
Hi, I try to write an asp query form that lets client search any text-string and display all pages in my web server that contain the text. I have IIS 6.0 on a server 2003. The MSDN site says...
4
by: Diamondback | last post by:
I have two tables, WIDGETS and VERSIONS. The WIDGETS table has descriptive information about the widgets while the VERSIONS table contains IDs relating to different iterations of those widgets...
0
by: starace | last post by:
I have designed a form that has 5 different list boxes where the selections within each are used as criteria in building a dynamic query. Some boxes are set for multiple selections but these list...
7
by: stig | last post by:
hi. coming from postgresql, i am used to textual references to most of the things i do with the database. i feel a little lost with all the graphical. i have few questions regarding MS SQL 2000...
7
by: Bernard Lebel | last post by:
Hello, I'm stumbled at a serious problem, and quite frankly getting desparate. This is a rather long-winded one so I'll try to get straight to the point. I have this Python program, that...
3
by: diego | last post by:
Greetings everyone! Is there a way to stop query analyzer from processing remaining query statements? Let's say i have the following query in query analyzer: query statements if condition...
3
by: Bill Hutchison | last post by:
I have a query that returns different results (3508 rows for snapshot, 6288 for dynaset) and that is the only thing I change to get the different results. When I try to make a table from the...
4
by: Doris | last post by:
It does not look like my message is posting....if this is a 2nd or 3rd message, please forgive me as I really don't know how this site works. I want to apologize ahead of time for being a novice...
4
by: Stan | last post by:
I am using MS Office Access 2003 (11.5614). My basic question is can I run a query of a query datasheet. I want to use more that one criteria and can not get that query to work. I thought I...
6
by: jsacrey | last post by:
Hey everybody, got a secnario for ya that I need a bit of help with. Access 97 using linked tables from an SQL Server 2000 machine. I've created a simple query using two tables joined by one...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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...

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.