Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Assistance in Creating a View from 2 unrelated tables

Posted on 2010-11-17
4
Medium Priority
?
369 Views
Last Modified: 2012-05-10
Hi Experts,

Create View VWCurrentGgl

as


Select GlYear, GlPeriod,'' as AccountType, '' as ExpenseType, Company, '' as GlCode, '' as Description,0 as PeriodMovement
from dbo.GenControl

Union all

Select Convert(Int,'') as GlYear, Convert(Int,'') as GlPeriod ,AccountType, ExpenseType, Company,GlCode, Description, CurrentBalance - PrevPerEndBal  As PeriodMovement
from dbo.GenMaster

The Results are the screen shot attached...

How can i get the GlYear and GlPeriod to populate all the way down as opposed to the zero's (if I need to change my thinking from a view to something else please let me know and assist)
View.JPG
0
Comment
Question by:Mikeyman_01
[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
  • 2
4 Comments
 
LVL 17

Expert Comment

by:OriNetworks
ID: 34160948
Select GlYear, GlPeriod,'' as AccountType, '' as ExpenseType, Company, '' as GlCode, '' as Description,0 as PeriodMovement
from dbo.GenControl

Union all

Select Convert(Int,'2010') as GlYear, Convert(Int,'2') as GlPeriod ,AccountType, ExpenseType, Company,GlCode, Description, CurrentBalance - PrevPerEndBal  As PeriodMovement
from dbo.GenMaster

Open in new window

0
 
LVL 17

Expert Comment

by:OriNetworks
ID: 34160962
i'm not sure what the point of the first record is if it has no relation. There has to be some calculation or reasoning for knowing what year and period to display. Maybe a stored procedure would be better?

@Year int
@Period int

Select @Year as GlYear, @Period as GlPeriod,AccountType, ExpenseType, Company,GlCode, Description, CurrentBalance - PrevPerEndBal  As PeriodMovement
from dbo.GenMaster

0
 
LVL 1

Accepted Solution

by:
VBisMe earned 2000 total points
ID: 34161108
Basing my results on that the GenControl table appears to have a single record, you should join the 2 tables rather than unioning them:

Select gc.GlYear, gc.GlPeriod, gm.AccountType, gm.ExpenseType, gm.Company, gm.GlCode, gm.Description, gm.CurrentBalance - gm.PrevPerEndBal  As PeriodMovement
from dbo.GenMaster gm
INNER JOIN dbo.GenControl gc ON 1 = 1  --Dummy join

OR

Select (SELECT glYear FROM dbo.GenControl) AS GlYear,
       (SELECT GlPeriod FROM dbo.GenControl) as GlPeriod,
       AccountType, ExpenseType, Company, GlCode, Description, CurrentBalance - PrevPerEndBal  As PeriodMovement
from dbo.GenMaster
0
 

Author Closing Comment

by:Mikeyman_01
ID: 34161178
Brilliant answer.. Thank you
0

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

After restoring a Microsoft SQL Server database (.bak) from backup or attaching .mdf file, you may run into "Error '15023' User or role already exists in the current database" when you use the "User Mapping" SQL Management Studio functionality to al…
Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

722 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