How to arrange data in sql server table - I edited the input , Please share your thoughts

I tried many query to achieve the expected result , I couldn't find any solution.

Actual:-

ID | EmpDailyFee | EmpMonthlyFee | CompDailyFee | CompMnthlyFee 1 NULL 12 NULL NULL 1 50 NULL NULL NULL 1 60 NULL NULL NULL 2 50 NULL NULL NULL 3 NULL 30 NULL NULL

Expected :-

ID | EmpDailyFee | EmpMonthlyFee | CompDailyFee | CompMnthlyFee 1 50 12 NULL NULL 1 60 12 NULL NULL 2 50 NULL NULL NULL 3 NULL 30 NULL NULL

-------------Problems Reply------------

This looks like a table smell to me since the rows are sharing the same ID value but with different EmpDailyFee/EmpMonthlyFee values. But in this particular case you can get your expected output like this:

SELECT t1.ID, t1.EmpDailyFee, t2.EmpMonthlyFee
FROM @Test t1
INNER JOIN @Test t2 ON t1.ID = t2.ID
WHERE t1.EmpDailyFee IS NOT NULL AND t2.EmpMonthlyFee IS NOT NULL
UNION ALL
SELECT t1.ID, t1.EmpDailyFee, t1.EmpMonthlyFee
FROM @Test t1
WHERE (t1.EmpDailyFee IS NOT NULL OR t1.EmpMonthlyFee IS NOT NULL) AND t1.ID NOT IN
(
SELECT t3.ID
FROM @Test t3
INNER JOIN @Test t2 ON t3.ID = t2.ID
WHERE t3.EmpDailyFee IS NOT NULL AND t2.EmpMonthlyFee IS NOT NULL
)

This has been tested on SQL Fiddle

Note: The reason why I did not include CompDailyFee and CompMnthlyFee is because the values of both the actual and expected results were NULL. I am trying to write a "simple-as-possible" query based on what OP has provided.

If you're trying to insert or update data this could be the solution, customizing it on your needings

IF EXISTS (SELECT 1 FROM Employee WHERE ID = 1)
UPDATE Employee SET EmpMonthlyFee= @Value WHERE ID = 1
ELSE
INSERT Employee(ID,EmpDailyFee,EmpMonthlyFee,CompDailyFee,CompMnthlyFee)
VALUES(1,NULL,@value,NULL,NULL)

declare @t table (ID int,Empfee int,monthlyfee int,compdailyfee int,Cmpnymonthlyfee int)

insert into @t (ID,Empfee,monthlyfee,compdailyfee,Cmpnymonthlyfee)
values (1,NULL,12,NULL,NULL),
(1,50,NULL,NULL,NULL),
(1,60,NULL,NULL,NULL)
;with cte as (
select t.ID,t.Empfee,
tt.monthlyfee,
t.compdailyfee,
t.Cmpnymonthlyfee,
ROW_NUMBER()OVER(PARTITION BY t.ID,tt.monthlyfee ORDER BY t.ID,tt.monthlyfee)RN
from @t t
CROSS APPLY @t tt
where t.Empfee IS NULL OR tt.monthlyfee IS NOT NULL)

select C.ID,
C.Empfee,
C.monthlyfee,
C.compdailyfee,
C.Cmpnymonthlyfee from cte c
where c.Empfee IS NOT NULL AND c.monthlyfee IS NOT NULL

Can be done using COALESCE as well.

SELECT T1.ID,
T2.EmpDailyFee,
COALESCE(T1.EmpMonthlyFee , T2.EmpMonthlyFee) EmpMonthlyFee,
COALESCE(T1.CompDailyFee , T2.CompDailyFee) CompDailyFee ,
COALESCE(T1.CompMnthlyFee , T2.CompMnthlyFee) CompMnthlyFee

FROM

(SELECT * FROM Tab WHERE EmpDailyFee IS NULL)T1
JOIN
(SELECT * FROM Tab WHERE EmpDailyFee IS NOT NULL)T2
ON T1.ID = T2.ID

I have gone one step ahead and assumed that values from the first row will take priority if not null(As for the 3rd column in OP). This part can be neglected if not required.

Category:sql server Views:1 Time:2018-05-16

Related post

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.112 (s). 11 q(s)