Solved

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

Posted on 2008-10-21
2
126 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PERFORMANCE OF SQL QUERY 13 72
SQL Backup skipping a few tables 7 43
get most recent and second most recent date in SQL Server 24 90
Need help debbuging stored procedure 21 41
Introduction This article will provide a solution for an error that might occur installing a new SQL 2005 64-bit cluster. This article will assume that you are fully prepared to complete the installation and describes the error as it occurred durin…
In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

813 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

9 Experts available now in Live!

Get 1:1 Help Now