Solved

Best Practice SQL. Is it possible to make this a view?

Posted on 2008-10-21
2
125 Views
Last Modified: 2012-05-05
I might be totally off here but I'm trying to avoid pointing to a specific row in the AccountGroup table. I want to be able to change the names of the groups but the Account Numbers has built in meanings according to standard chart of accounts in Europe.

AccountGroup contains AccountGroupNos like:
No: 1
Descr: 'Assets'
No: 11
Descr: 'My Machinery Assets of type 1'

Account contains the accountnos:

No: 1110
Descr: My Machine type 1
No: 1111
Desc: My Machine type 2

Now, is this good practice or should I in fact store the AccountGroupID in the Account based on the formula below each time I add an Account? Or if this is OK, is it possible to do the query without the #temp-table so I can put the code in a view?

I don't know if it makes a difference but the database is located in a .MDF SQL Express file and will run localy on the end users computer.

Tanks in advance!

/Tomas


SELECT a.AccountID, a.Accountno, a.AccountDescription, g.AccountGroupDescription 

INTO #TEMP

FROM Account a, AccountGroup g

WHERE left(a.accountno,2) = g.AccountGroupNo

SELECT AccountID, AccountNo, AccountDescription, a.AccountGroupDescription, g.AccountGroupDescription as MainAccountGroup

FROM #temp a, AccountGroup g

WHERE left(a.accountno,1) = g.AccountGroupNo

ORDER BY AccountNo

Open in new window

0
Comment
Question by:ekenman
2 Comments
 

Author Comment

by:ekenman
ID: 22765965
OK, I managed to get the question a little bit more accurate. The question remaining is if it is OK to do this? Is it good practice?
SELECT     MG.AccountGroupNo as MainGroupNo, MG.AccountGroupDescription as MainAccountGroupDescription, G.AccountGroupNo, G.AccountGroupDescription, Account.AccountNo, Account.AccountDescription

FROM         Account INNER JOIN

                      AccountGroup as G ON LEFT(Account.AccountNo, 2) = G.AccountGroupNo INNER JOIN

					  AccountGroup as MG ON LEFT(Account.AccountNo,1) = MG.AccountGroupNo 

Open in new window

0
 
LVL 39

Accepted Solution

by:
BrandonGalderisi earned 250 total points
ID: 22772424
Based upon your query above in http:#22765965, the AccountGroupNo should be in the Account table.  It also seems, since you have two joins to accountgroup, that an account is part of two groups.  once based upon the first and once based upon the second.  Perhaps this is a good candidate for group and subgroup.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

by Mark Wills PIVOT is a great facility and solves many an EAV (Entity - Attribute - Value) type transformation where we need the information held as data within a column to become columns in their own right. Now, in some cases that is relatively…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

910 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now