473,480 Members | 2,019 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Return two rows from one rows data

I know this table is designed wrong for what I am doing but I hope I
can do it. I have a table like this.

Prod_A_Jan, Prod_A_Feb, Prod_B_Jan, Prod_B_Feb

I want a query that returns data like this (two rows of data)

"ProdA", Prod_A_Jan, Prod_A_Feb
"ProdB", Prod_B_Jan, Prod_B_Feb

I know two queries can get it but I want one. Any Help would be
great!!!

Sheila T.
Jul 20 '05 #1
3 1163
You can use a UNION query:

SELECT "ProdA", Prod_A_Jan AS Jan, Prod_A_Feb AS Feb
FROM MtTable
UNION ALL
SELECT "ProdB", Prod_B_Jan, Prod_B_Feb
FROM MtTable

--
Hope this helps.

Dan Guzman
SQL Server MVP

"George" <ge******@yahoo.com> wrote in message
news:e9**************************@posting.google.c om...
I know this table is designed wrong for what I am doing but I hope I
can do it. I have a table like this.

Prod_A_Jan, Prod_A_Feb, Prod_B_Jan, Prod_B_Feb

I want a query that returns data like this (two rows of data)

"ProdA", Prod_A_Jan, Prod_A_Feb
"ProdB", Prod_B_Jan, Prod_B_Feb

I know two queries can get it but I want one. Any Help would be
great!!!

Sheila T.

Jul 20 '05 #2
Hi,

try this one

SELECT "ProdA" AS Product, Prod_A_Jan AS Jan, Prod_A_Feb AS Feb
FROM TheTable
UNION
SELECT "ProdB" AS Product, Prod_B_Jan AS Jan, Prod_B_Feb AS Feb
FROM TheTable

-Jorgen

"George" <ge******@yahoo.com> skrev i en meddelelse
news:e9**************************@posting.google.c om...
I know this table is designed wrong for what I am doing but I hope I
can do it. I have a table like this.

Prod_A_Jan, Prod_A_Feb, Prod_B_Jan, Prod_B_Feb

I want a query that returns data like this (two rows of data)

"ProdA", Prod_A_Jan, Prod_A_Feb
"ProdB", Prod_B_Jan, Prod_B_Feb

I know two queries can get it but I want one. Any Help would be
great!!!

Sheila T.

Jul 20 '05 #3
Another approach using a derived table of literals which in most cases
eliminate multiple table scans :

SELECT val,
CASE val WHEN 'ProdA' THEN Prod_A_Jan
WHEN 'ProdB' THEN Prod_B_Jan
END,
CASE val WHEN 'ProdA' THEN Prod_A_Feb
WHEN 'ProdB' THEN Prod_B_Feb
END
FROM tbl
CROSS JOIN (
SELECT 'ProdA' UNION SELECT 'ProdB' ) D ( val ) ;

--
Anith
Jul 20 '05 #4

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

Similar topics

2
21848
by: Briniken | last post by:
How can a SQL statement be written to return a specified range of rows? For example: -- tblContact -- ( -- SSN char(9), -- FirstName varchar(50), -- LastName varchar(50) -- ) -- This...
1
2457
by: Ken | last post by:
I wrote a function to use in queries that takes a date and adds or subtracts a certain length time and then returns the new value. There are times when my function needs to return Null values. ...
4
33388
by: R.A. | last post by:
Hi, I have an web service method that accept an xml document and returns a different xml document. Based on the input xml I fill a dataset with information from a database. If the dataset has...
6
4810
by: jjturon | last post by:
Can anyone help me?? I am trying to pass a Select Query variable to a table using Dlookup and return the value to same select query but to another field. Ex. SalesManID ...
6
3954
by: kilter | last post by:
Anyone know of a routine that will return the number of rows and columns in a matrix?
24
2154
by: Earl | last post by:
I have all of my data operations in a separate library, so I'm looking for what might be termed "best practices" on a return type from those classes. For example, let's say I send an update from...
3
2956
by: weird0 | last post by:
The given should return A,B that is two rows because both accountnames A and B have the same user_Id=1 but it is only returning A when I performed the op. MessageBox.Show("AccountNames:" +...
2
3059
by: thuythu | last post by:
Please help me.... I used and Javascript to view the data. But when i click button open a popup windows, then select data and click save button. The popup close and return the main page, but the...
1
1663
by: hailua | last post by:
using System; using System.Data; using System.Data.SqlClient; using System.Data.SqlTypes; using Microsoft.SqlServer.Server; using System.Collections; public partial class UserDefinedFunctions...
0
867
by: roseate4amor | last post by:
dear all, i have a problem that i try to fix since the last few weeks. i need to add a new data in microsoft office database using dataset in visual basic. the problem is, when i insert new data...
0
7041
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
6908
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
7043
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7081
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...
1
6737
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...
0
5336
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
4481
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...
0
2984
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
563
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.