Avatar of beckyng
beckyng
 asked on

Sql statment

Hi, May I get your help?
Table1
VoucherID       EntryID          Project      Expenses     Dept
123                   1                     A01           Travel          Null
123                   1                     Null          Null              Fin
Table2
VoucherID      EntryID      Amount
123                   1                100

How can I create a view as follows:
 VoucherID       EntryID    Amount  Project  Expenses Dept
123                     1               100         A01       Travel      Fin

Thanks
Becky
Microsoft SQL ServerMicrosoft DevelopmentMicrosoft SQL Server 2008

Avatar of undefined
Last Comment
suvmitra

8/22/2022 - Mon
SANDY_SK

Here is the query,

select t1.voucherId,t1.EntryId,t1.Project,t3.Amount,t1.Expenses,t2.Dept from table1 as t1 
JOIN  table1 t2 on t1.voucherId = t2.voucherId
AND t1.EntryId = t2.EntryId
AND t1.Project=t2.Project
AND t1.Expenses IS NOT NULL AND t2.Dept IS NOT NULL
INNER JOIN Table2 t3 ON t2.voucherId=t3.VoucherId

Open in new window


see if it helps
beckyng

ASKER
sorry, no result
suvmitra

CREATE VIEW MyView AS
select tbl1.VoucherID, tbl1.EntryID, tbl2.Amount, tbl1.Project, tbl1.Expenses, tbl1.Dept
from tbl1
left join tbl2
on tbl1.VoucherID = tbl2.VoucherID
where tbl1.Dept is null;

CREATE VIEW MyView2 AS
select tbl1.VoucherID, tbl1.EntryID, tbl2.Amount, tbl1.Project, tbl1.Expenses, tbl1.Dept
from tbl1
left join tbl2
on tbl1.VoucherID = tbl2.VoucherID
where tbl1.Dept is not null;

select MyView.VoucherID, MyView.EntryID, MyView2.Amount, MyView.Project, MyView.Expenses, MyView2.Dept
from MyView
left join MyView2
on MyView.VoucherID = MyView2.VoucherID;
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
SANDY_SK

just remove the project comparison....

like this

select t1.voucherId,t1.EntryId,t1.Project,t3.Amount,t1.Expenses,t2.Dept from table1 as t1 
JOIN  table1 t2 on t1.voucherId = t2.voucherId
AND t1.EntryId = t2.EntryId
AND t1.Expenses IS NOT NULL AND t2.Dept IS NOT NULL
INNER JOIN Table2 t3 ON t2.voucherId=t3.VoucherId

Open in new window

ASKER CERTIFIED SOLUTION
Ephraim Wangoya

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
tomHamill

It's hard to answer this without knowing more about what else might be in these fields and how else you might need to select data from them.

With just your three rows of sample data, a fairly simple "GROUP BY" query should work.

SELECT
  Table1.VoucherID, Table1.EntryID, Table2.Amount, Max(Table1.Project) AS Project, Max(Table1.Expenses) AS Expense, Max(Table1.Dept) As Dept
FROM
  Table1 INNER JOIN Table2 ON
    Table1.VoucherID = Table2.VoucherID AND
    Table1.EntryID = Table2.EntryID
GROUP BY
  Table1.VoucherID, Table1.EntryID, Table2.Amount

BUT THIS ASSUMES that there is never more than 1 record in Table1 where Project (or Expenses... or Dept) is not null.  That doesn't seem very realistic to me - but you know best.

What do you want to see if your Table1 data is more like this:

VoucherID       EntryID          Project      Expenses     Dept
123                   1                     A01           Travel          Null
123                   1                     Null          Null              Fin
123                   1                     Null          Meals           MIS

With my query the results would be Expenses = Travel and Dept = MIS.  Is that acceptable?
suvmitra

Since my way is also resulting the recordset you wanted and you wanted a VIEW right?... can you clarify of not considering mine? Thanks.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.