Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Query

Author  Topic 

nirene
Yak Posting Veteran

98 Posts

Posted - 2007-09-12 : 00:13:59
I have 3 tables

GLmaster
Glcode Gldesc
GL01 Plant & Machinery
GL02 Land & Building
GL03 Staff Welfare
GL03 Conveyance

Slmaster
Glcode Slcode Sldesc
GL03 SL01 Tea Expenses
GL03 SL02 Food Expenses
GL03 SL03 Fitness Expenses
GL04 SL04 Bus fare
GL04 SL05 Train fare

Trans
Refno Glcode Slcode Amount
RF01 GL01 1000
RF02 GL04 SL04 400
RF03 GL02 2000
RF04 GL03 SL02 200

In a single query I want to fetch the values as
Refno Glcode Slcode Amount Desc
RF01 GL01 1000 Plant & Machinery
RF02 GL04 SL04 400 Train fare
RF03 GL02 2000 Land & Building
RF04 GL03 SL02 200 Food Expenses

Guide me in this regard

Thanks in advance

Nirene

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-09-12 : 01:29:14
Did you got your sample data and expected result correct ?



DECLARE @GLmaster TABLE
(
Glcode varchar(10),
Gldesc varchar(20)
)
INSERT INTO @GLmaster
SELECT 'GL01', 'Plant & Machinery' UNION ALL
SELECT 'GL02', 'Land & Building' UNION ALL
SELECT 'GL03', 'Staff Welfare' UNION ALL
SELECT 'GL04', 'Conveyance'

DECLARE @Slmaster TABLE
(
Glcode varchar(10),
Slcode varchar(10),
Sldesc varchar(20)
)
INSERT INTO @Slmaster
SELECT 'GL03', 'SL01', 'Tea Expenses' UNION ALL
SELECT 'GL03', 'SL02', 'Food Expenses' UNION ALL
SELECT 'GL03', 'SL03', 'Fitness Expenses' UNION ALL
SELECT 'GL04', 'SL04', 'Bus fare' UNION ALL
SELECT 'GL04', 'SL05', 'Train fare'

DECLARE @Trans TABLE
(
Refno varchar(10),
Glcode varchar(10),
Slcode varchar(10),
Amount int
)
INSERT INTO @Trans
SELECT 'RF01', 'GL01', NULL, 1000 UNION ALL
SELECT 'RF02', 'GL04', 'SL04', 400 UNION ALL
SELECT 'RF03', 'GL02', NULL, 2000 UNION ALL
SELECT 'RF04', 'GL03', 'SL02', 200

SELECT t.Refno, t.Glcode, t.Slcode, t.Amount, [DESC] = coalesce(g.Gldesc, s.Sldesc)
FROM @Trans t
left JOIN @GLmaster g
ON t.Glcode = g.Glcode
AND t.Slcode IS NULL
left JOIN @Slmaster s
ON t.Glcode = s.Glcode
AND t.Slcode = s.Slcode

/*
Refno Glcode Slcode Amount DESC
---------- ---------- ---------- ----------- --------------------
RF01 GL01 NULL 1000 Plant & Machinery
RF02 GL04 SL04 400 Bus fare
RF03 GL02 NULL 2000 Land & Building
RF04 GL03 SL02 200 Food Expenses
*/



KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

sunsanvin
Master Smack Fu Yak Hacker

1274 Posts

Posted - 2007-09-12 : 01:32:52
SELECT t.Refno, t.Glcode, t.Slcode, t.Amount, [DESC] = coalesce(g.Gldesc, s.Sldesc)
FROM Trans t
left JOIN GLmaster g
ON t.Glcode = g.Glcode
AND t.Slcode IS NULL
left JOIN Slmaster s
ON t.Glcode = s.Glcode
AND t.Slcode = s.Slcode

I TRIED TOO HARD.....
but KH is always best....
anyway i'm in the correct root i think

Vinod
Even you learn 1%, Learn it with 100% confidence.
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-09-12 : 01:37:20
your query is the same as mine


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

sunsanvin
Master Smack Fu Yak Hacker

1274 Posts

Posted - 2007-09-12 : 01:52:08
of cource i just copied ur query.....
but i truly tried hard to get like you.......
u r great KH

Vinod
Even you learn 1%, Learn it with 100% confidence.
Go to Top of Page

nirene
Yak Posting Veteran

98 Posts

Posted - 2007-09-17 : 03:19:03
Thanks a lot khtan
Go to Top of Page
   

- Advertisement -