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

Rows to Column Names

I am struggling on this issue and was hoping if anyone out there can
help me.
Here is the setup: I have a table with the following data:
TableName: TranDetail
MerchID ItemName Price
------------------
101 A 5
101 B 3.5
101 C 0
102 B 7.6
102 C 4
102 E 65
102 G 4
103 K 35

Table Design
MerchID int
ItemName varchar(50)
Price float

What I would like is a report that looks like this
MerchID A B C E G K
101 5 3.5 0
102 7.6 4 65 4
103 35

This report can change on every run depending on data in table
TranDetail. The column name in report depends on ItemName in table
TranDetail. As seen in the above report, there is no data for Items D,
F, H, I, J and hence they do not show up in the report.

What I need: Code for a stored procedure that can get me this data.

Thanks for your help...

DBA in despair!

Jul 23 '05 #1
7 1956
Stu
Pivot queries are difficult, but not impossible to build in SQL Server;
here's my stab at it:

DECLARE @SQL varchar(8000)
SET @SQL = 'SELECT MerchID, '

DECLARE @ItemName varchar(50)

DECLARE C CURSOR LOCAL FAST_FORWARD
FOR SELECT DISTINCT ItemName
FROM TranDetail
ORDER BY ItemName

OPEN C

FETCH NEXT FROM C INTO @ItemName

SET @SQL = 'SELECT MerchID, '

WHILE @@FETCH_STATUS = 0
BEGIN

SET @SQL = @SQL + @ItemName +'=SUM(CASE WHEN
ItemName='''+@ItemName+''' THEN Price END), '

FETCH NEXT FROM C INTO @ItemName
END

SET @SQL = LEFT(@SQL, LEN(@SQL)-1)
SET @SQL = @SQL + ' FROM TranDetail GROUP BY MerchID'

CLOSE C

DEALLOCATE C

EXEC (@SQL)
I basically build a dynamic SQL statement using a cursor (yes, I know,
cursors are bad, but they can be useful). However, there is a
limitation; the total constructed SQL statement can only be 8000
characters. If your ItemName is long (doesn't appear to be), and you
have a lot of them, the SQL statement itself may fail on you.

HTH,
Stu

Jul 23 '05 #2
Why not do reports in your reporting app? Almost any of them will do a
crosstab without any programming required.

--
David Portas
SQL Server MVP
--
Jul 23 '05 #3
Dear

Yeh david is right, if it is possible you must use some reporting tool
which normally do the thing for you! an other option is Microsoft
Excell.

1. Excell Menu > Data > Import External Data.
2. Excell Menu > Date > PivotTable and Pivitchart Wizird.
3. Excell Menu > Help > Microsoft Excell Help or F1 Key :) :) :) :)
Microsoft Excell is best and easiest tool available in market for
ad-hoc Data reporting.

cheers.

Saghir Taj (MCDBA)
www.dbnest.com: The Nest of DB Professionals.
www.Resumedump.com: Career Partner
www.siliconways.net : Design to Suit your IT needs.
Jul 23 '05 #4
Dear

I wrongly spell data as date so you dont need to search for date menu
in excell ... lolzzz.... sorry buddies.

************Excell Menu > Data > PivotTable and Pivitchart
Wizird*******************
Thanks.
Saghir Taj (MCDBA)
www.dbnest.com: The Nest of DB Professionals.
www.Resumedump.com: Career Partner
www.siliconways.net : Design to Suit your IT needs

Jul 23 '05 #5
Using curosors is fine but total length of my data is way more than
8000 characters. There are more than 150 item names in the real
database and each name is about 20 characters long!

Jul 23 '05 #6
Thanks for your reply!

This is an automated report that runs on a monthly basis so there is no
front end to it. I was hoping to run the SP from a dotnet app and then
export the data to excel and email results. We may also decide to
display the results on a webpage too - so fast execution of the SP is
critical also.

Thanks again!

Vishal

Jul 23 '05 #7
SQLJunkie (vs******@gmail.com) writes:
This is an automated report that runs on a monthly basis so there is no
front end to it. I was hoping to run the SP from a dotnet app and then
export the data to excel and email results. We may also decide to
display the results on a webpage too - so fast execution of the SP is
critical also.


If fast execution is critical, you should definitely take the rowset
without thrills from SQL Server, and then transpose it your .Net client.
C#/VB is much better fitted for such operations than SQL Server.

Or, hey, import the data into Excel as is, and have Excel do the
crosstab. A poster here recently claimed that Excel was the best tool
in town for pivots. (I have done pivots myself in Excel though.) Excel
has an OLE interface, but I would expect standard routines being
available to manipulate Excel books from .Net.
--
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 #8

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

Similar topics

22
by: Bryan Guilliams | last post by:
I'm trying to come up with an elegant, simple way to compare two consecutive values from the same table. For instance: SELECT TOP 2 datavalues FROM myTable ORDER BY timestamp DESC That...
3
by: Tome73 | last post by:
How can I easily add the rows of DataTable1 to the rows of DataTable2. Both queries are from the same table. I can always use the column names with myRow, but I was wishing for a shortcut. When I...
1
by: Yama | last post by:
Hi, I am really confused. I have created a strong typed dataset for Northwind database Customer table. Now I am loading it with a stream of XML (ADO style) with the following: Customers _cust...
9
by: YONETANI Tomokazu | last post by:
Hi. You can use the following SQL to construct rows with column names on the fly, rather than from an existing table like sysibm.sysdummy1: SELECT * FROM TABLE ( VALUES (0, 1, 2), (3, 4, 5), (6,...
6
by: Jumping Matt Flash | last post by:
The code i'm writing is using VB .NET and is for a web service returning a dataset, which is in turn to be used by an ASP .NET application displaying a datagrid. I'm currently populating a...
1
by: Ruediger Herrmann | last post by:
Hello again, as I am new to PostgreSQL (great pice of softwork btw) this probably won't be the last question. I access PostgreSQL from Java via the PostgreSQL JDBC driver. I am currently...
1
by: Carlo DLS | last post by:
All, I am trying to create a table, be it the simple <tabletag or maybe a data control object like GridView where what I want to happen is, I want that table to automatically generate an extra...
0
by: MathewJose | last post by:
Hi, I have a DatagridView in windows form.It has got a column which has some names that are populated from master table in database.Now i need to copy a set of data against these names. I...
0
by: Steve Holden | last post by:
Vlastimil Brom wrote: Ultimately that depends where the table and column names come from. If they are user inputs then you are still vulnerable to SQL injection, but usually that's not the case...
1
by: brionesl | last post by:
Hi, I have seen other threads with the same question but I'm not too familiar with Access and their solutions often include writing VB code or very complex SQL statements. What I want to do is...
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: 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
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.