Solved

Assistance in Creating a View from 2 unrelated tables

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Server (2008) Declare Variable (Date) and Set value 6 44
SQL Improvement  ( Speed) 14 33
sql how to count case when 4 25
sql 2016 data tools breakdown.. 1 13
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

856 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