all groups > visual studio .net general > may 2007 >
You're in the

visual studio .net general

group:

SQL and help


SQL and help Emma Hope
5/24/2007 6:41:01 AM
visual studio .net general:
All,

Apologies if i have posted this in the wrong place, if i have, if someone
could direct me to the best place, i'd appreciate it.

I have Visual Basic & SQL Server Express. I have created a VB.net
application that is attached to a SQL server backend, my select statement is
really really complex and i wonder if there is a way of shortening it, this
is a highly simplified version.

Select number1, number2, number3, number4,
number1+number2 as sum12, number3+number4 as sum34,
number1+number2+number3+number4 as sum1234
From table1, table2

my fields sum12, sum34 and sum1234 are much more complicated than this in
real life and each one is around four or five lines of SQL code

as you can see sum1234 is actually the same as sum12+sum34 which is much
shorter than typing number1+number2+number3+number4 (especially in my code as
these are 4 to 5 lines long each) but when i try to execute the SQL of
sum12+sum34 as sum 1234, i am told that sum12 and sum34 don't exist.

Is there a way of making sql understand (all in one statement) that sum12
and sum34 do exist so that i can use them in sum1234 i.e. is there a SQL
'word' such as SELECT, UPDATE, CASE etc that will do this for me.

Although my code works without doing this it is extremely complicated and
hard to read. I am quite new to VB.net and don't yet understand views, stored
procedures etc, so i'd prefer a method that doesn't use anything like this
yet.

Thanks for your help.

My long and involved complex actual select statement is below but i would
say ignore it, it is just for those who might want to see how complex it
actually is, i am only half way through building it so far, so it will
eventually be four to five times longer

SELECT tblRecordCompany.CompanyID, tblRecordCompany.CompanyName,
tblRecordCompany.YearEnd, tblRecordCompany.PreTaxProfits,
tblRecordCompany.CompanyContribution,
tblRecordCompany.PensionTransfer, tblRecordCompany.RetainedProfit,
CASE WHEN PreTaxProfits <= TaxBandLower THEN
PreTaxProfits * TaxRateLower WHEN PreTaxProfits <= TaxBandMedium THEN
(TaxBandLower * TaxRateLower)
+ (PreTaxProfits - TaxBandLower) * TaxRateMedium WHEN
PreTaxProfits > TaxBandMedium THEN (TaxBandLower * TaxRateLower)
+ ((TaxBandMedium - TaxBandLower) * TaxRateMedium) +
(PreTaxProfits - TaxBandMedium) * TaxRateHigher ELSE 0 END AS CorporationTax,
tblRecordCompany.PreTaxProfits - (CASE WHEN
PreTaxProfits <= TaxBandLower THEN PreTaxProfits * TaxRateLower WHEN
PreTaxProfits <= TaxBandMedium
THEN (TaxBandLower * TaxRateLower) + (PreTaxProfits -
TaxBandLower)
* TaxRateMedium WHEN PreTaxProfits > TaxBandMedium
THEN (TaxBandLower * TaxRateLower) + ((TaxBandMedium - TaxBandLower)
* TaxRateMedium) + (PreTaxProfits - TaxBandMedium) *
TaxRateHigher ELSE 0 END) AS ProfitOnOrdinaryActivitiesAfterTax,
CASE WHEN (PreTaxProfits - CompanyContribution) <=
TaxBandLower THEN (PreTaxProfits - CompanyContribution)
* TaxRateLower WHEN (PreTaxProfits -
CompanyContribution) <= TaxBandMedium THEN (TaxBandLower * TaxRateLower)
+ ((PreTaxProfits - CompanyContribution) -
TaxBandLower) * TaxRateMedium WHEN (PreTaxProfits - CompanyContribution)
[quoted text, click to view]
((TaxBandMedium - TaxBandLower) * TaxRateMedium)
+ ((PreTaxProfits - CompanyContribution) -
TaxBandMedium) * TaxRateHigher ELSE 0 END AS CorporationTaxOppo,
(CASE WHEN PreTaxProfits <= TaxBandLower THEN
PreTaxProfits * TaxRateLower WHEN PreTaxProfits <= TaxBandMedium THEN
(TaxBandLower * TaxRateLower)
+ (PreTaxProfits - TaxBandLower) * TaxRateMedium WHEN
PreTaxProfits > TaxBandMedium THEN (TaxBandLower * TaxRateLower)
+ ((TaxBandMedium - TaxBandLower) * TaxRateMedium) +
(PreTaxProfits - TaxBandMedium) * TaxRateHigher ELSE 0 END)
- (CASE WHEN (PreTaxProfits - CompanyContribution) <=
TaxBandLower THEN (PreTaxProfits - CompanyContribution)
* TaxRateLower WHEN (PreTaxProfits -
CompanyContribution) <= TaxBandMedium THEN (TaxBandLower * TaxRateLower)
+ ((PreTaxProfits - CompanyContribution) -
TaxBandLower) * TaxRateMedium WHEN (PreTaxProfits - CompanyContribution)
[quoted text, click to view]
((TaxBandMedium - TaxBandLower) * TaxRateMedium)
+ ((PreTaxProfits - CompanyContribution) -
TaxBandMedium) * TaxRateHigher ELSE 0 END) AS CorporationTaxSaving
FROM tblRecordCompany INNER JOIN
tblDataDates ON tblRecordCompany.YearEnd =
tblDataDates.Date INNER JOIN
tblDataTaxRatesBands ON tblDataDates.TaxYear =
Re: SQL and help Emma Hope
5/24/2007 9:01:03 AM
I have already downloaded this, read the book that comes with it and looked
on the msdn site, i have already written a query that works and i know how to
do that, i have a simple query to ask anyone if the know of a SQL expression
that means i can shorten my code in sql server express

[quoted text, click to view]
Re: SQL and help pvdg42
5/24/2007 9:47:04 AM

[quoted text, click to view]
<snip>

It would appear that you need some SQL basic training. Some suggestions:

Look for tutorials that will help you with your queries:

http://msdn2.microsoft.com/en-us/library/ms189826.aspx

Install the free SQL Server Express Management Studio Express that will help
you with your databases and will allow you to try your queries without
needing an application:

http://msdn.microsoft.com/vstudio/express/sql/download/


AddThis Social Bookmark Button