Solved

Assistance in Creating a View from 2 unrelated tables

Posted on 2010-11-17
4
359 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 500 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

This is basically a blog post I wrote recently. I've found that SARGability is poorly understood, and since many people don't read blogs, I figured I'd post it here as an article. SARGable is an adjective in SQL that means that an item can be fou…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

617 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