Solved

Sql statment

Posted on 2012-03-11
7
259 Views
Last Modified: 2012-03-15
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
0
Comment
Question by:beckyng
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 11

Expert Comment

by:SANDY_SK
ID: 37708452
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
0
 

Author Comment

by:beckyng
ID: 37708471
sorry, no result
0
 
LVL 9

Expert Comment

by:suvmitra
ID: 37708482
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;
0
What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

 
LVL 11

Expert Comment

by:SANDY_SK
ID: 37708574
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

0
 
LVL 32

Accepted Solution

by:
Ephraim Wangoya earned 500 total points
ID: 37708592
try this

select A.VoucherID, A.EntryID, Amount,  Project,  Expenses, Dept
from (select VoucherID, Max(EntryID) EntryID, Max(Project)Project, Max(Expenses) Expenses, Max(Dept) Dept
      from Table1
      group by VoucherID) A
left join (select VoucherID, MAX(EntryID) EntryID, MAX(Amount) Amount 
			from Table2
			group by VoucherID) B on A.VoucherID = B.VoucherID

Open in new window

0
 
LVL 1

Expert Comment

by:tomHamill
ID: 37709614
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?
0
 
LVL 9

Expert Comment

by:suvmitra
ID: 37727802
Since my way is also resulting the recordset you wanted and you wanted a VIEW right?... can you clarify of not considering mine? Thanks.
0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question