Click here to Skip to main content
15,894,405 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi,

I have a table as below

ID HRS METHOD
------------------
1 2.5 A
1 5.0 B
2 0.5 A
2 1.5 B
3 3.25 A


I want to fetch records as following.

Desired Output:
ID HRS(A) HRS(B)
-------------------
1 2.5 5.0
1 0.5 1.5
1 3.25 NA

I used the follwing query but, I get null values

SQL
SELECT EDT_RACNUM_ID,
        CASE
             WHEN EDT_PHASE_CD = '2300' THEN EDT_HOURS_NUM
             --ELSE EDT_HOURS_NUM
         END AS 'EDT_HOURS_NUM1',
         CASE
             WHEN EDT_PHASE_CD = '2301' THEN EDT_HOURS_NUM
             --ELSE EDT_HOURS_NUM
         END AS 'EDT_HOURS_NUM2'
    FROM P9T_ESTIMATE_DTL
    ORDER BY EDT_RACNUM_ID


Ouput I get when I run the SQL(NULL values not desired):
ID HRS(A) HRS(B)
-----------------------
1 9.1 NULL
1 6.0 NULL
1 NULL 6.3
1 NULL 1.2

Appreciaite your help. Please tell me if this is possible or not? If yes, can you please provide the sample SQL.

Thanks,
Posted
Updated 4-Oct-13 7:23am
v3

Thanks everyone for your help.

I figured it out using a Pivot.

Thanks again for taking time for trying :)
 
Share this answer
 
Comments
Member 13301449 12-Dec-17 0:53am    
Can You Show how to use pivot to this problem
This query handles the case in which one method (either A or B) is missing for an ID value. It does that by using SELECT DISTINCT and selecting the Hours(A) value and the Hours(B) value for each column for the unique ID. If a value is missing for either A or B, then the result is NULL. If you want something other than NULL to display, use the ISNULL() function and CAST statement. See example of that at the bottom of this solution.

SQL
select distinct t1.id as ID,
(Select hours from table_1 As T3 where T3.id=t1.id and T3.method='A') as [Hours(A)],
(Select hours from table_1 As T4 where T4.id=t1.id and T4.method='B') as [Hours(B)]
from Table_1 as T1

Results
Quote:
ID Hours(A) Hours(A)
1 2.50 5.00
2 0.50 1.50
3 3.25 NULL



SQL
select distinct t1.id as ID, 
ISNULL(CAST((Select hours from table_1 As T3 where T3.id=t1.id and T3.method='A') as varchar(6)),'N/A') as [Hours(A)],
ISNULL(CAST((Select hours from table_1 As T4 where T4.id=t1.id and T4.method='B') as varchar(6)),'N/A') as [Hours(A)] 
from Table_1 as T1 

Results
Quote:
ID Hours(A) Hours(A)
1 2.50 5.00
2 0.50 1.50
3 3.25 N/A


Tested: SQL Server Express 2012
______________________________________________________________________________________________
SQL Create Table statement
CREATE TABLE [dbo].[Table_1](
	[ID] [int] NULL,
	[Hours] [numeric](8, 2) NULL,
	[Method] [nchar](1) NULL
) ON [PRIMARY]

SQL Insert statements to create test data
SQL
insert into table_1 (id,hours,method) values(1,2.5,'A');
insert into table_1 (id,hours,method) values(1,5.0,'B');
insert into table_1 (id,hours,method) values(2,0.5,'A');
insert into table_1 (id,hours,method) values(2,1.5,'B');
insert into table_1 (id,hours,method) values(3,3.25,'A');
 
Share this answer
 
v6
Please try this one. Except for "NA" you will get 0.00 there

SQL
SELECT ID, SUM(HRS_A) HRS_A, SUM(HRS_B)HRS_B
FROM
(
    SELECT ID,
            CASE WHEN METHOD = 'A' THEN HRS ELSE 0 END HRS_A,
            CASE WHEN METHOD = 'B' THEN HRS ELSE 0 END HRS_B

    FROM TABLE_1

) A
GROUP BY ID
 
Share this answer
 
I am not sure if this is exactly what you are looking for, but as far as "Methods" are always "A" and "B" it SHOULD work (don't have the environment to test it right now):

SQL
SELECT a.ID, a.HRS, b.HRS 
FROM YOURTABLE a, YOURTABLE b
WHERE a.ID=b.ID AND a.METHOD='A' AND b.METHOD='B'


It basically joins the table with itself and then looks for the "A" and "B" methods.
 
Share this answer
 
You can convert various elements in a single column to individual fields by using joins, the fields being related by some common factor that would allow you to correlate them.

For example, in a table where pets are listed by owner

SELECT A.petsName as DOGS, B.petsName as Cats
FROM ANIMALS as A INNER JOIN ANIMALS AS B
ON A.owner = B.owner
WHERE A.petType = 'DOG' and B.petType='cat'

For a time clock, one can have a datetime record for clocking in, another for clocking out, and an employee ID. This method would allow you to create a single record with an intime and outtime field

You'll need to decide what to do for fields that only have a single member of your criteria (a LEFT JOIN and ISNULL() may be an option).
 
Share this answer
 
v2
select a.ID as 'HRS(A)',(b.ID) as 'HRS(B)' from HRS a,HRS b where a.ID like '%A%' and b.ID like '%B%'
 
Share this answer
 
SQL
select
    ID,
    cast( max(hours) as varchar) HrsA,
    case when max(hours)=min(hours) then 'NA' else cast( min(hours) as varchar) HrsB
From tblName
group by ID

Happy coding!
:)
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900