Solved

Assistance in Creating a View from 2 unrelated tables

Posted on 2010-11-17
4
333 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
  • 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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

773 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