Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

I have three select statements which have the bulk of their columns which are the same except a few. So here is an example

select
*orgid='0'
,'P' as OrgType*
, Lang
,*cast(Null as varchar) as BoardName
,cast(Null as varchar) as SchoolName*

,SUM(CASE WHEN IncludeColumn = 1 AND SecondInclude IN(5) THEN 1 ELSE 0 END) as Column1
,SUM(CASE WHEN IncludeColumn = 1 AND SecondInclude IN(6) THEN 1 ELSE 0 END) as Column2
,SUM(CASE WHEN IncludeColumn = 1 AND SecondInclude IN(4) THEN 1 ELSE 0 END) as Column3

FROM *TABLEA*
WHERE *[Include_All] = 1* 
Group By *[Lang]*



select
orgid=boardname
,'B' as OrgType
, Lang
, BoardName
,cast(Null as varchar) as SchoolName


,SUM(CASE WHEN IncludeColumn = 1 AND SecondInclude IN(5) THEN 1 ELSE 0 END) as Column1
,SUM(CASE WHEN IncludeColumn = 1 AND SecondInclude IN(6) THEN 1 ELSE 0 END) as Column2
,SUM(CASE WHEN IncludeColumn = 1 AND SecondInclude IN(4) THEN 1 ELSE 0 END) as Column3
FROM TABLEB
WHERE [Include_Board] = 1 
Group By [Lang], BoardName


select
orgid=SchoolName
,'S' as OrgType
, Lang
, BoardName
,SchoolName

,SUM(CASE WHEN IncludeColumn = 1 AND SecondInclude IN(5) THEN 1 ELSE 0 END) as Column1
,SUM(CASE WHEN IncludeColumn = 1 AND SecondInclude IN(6) THEN 1 ELSE 0 END) as Column2
,SUM(CASE WHEN IncludeColumn = 1 AND SecondInclude IN(4) THEN 1 ELSE 0 END) as Column3

FROM TABLEC
WHERE [Include_School] = 1 
Group By [Lang], BoardName

So as you can see, the calculated columns are the same in each "grouping". The problem is that I have about 70 calculated fields (which are the same) in all three select statements. This makes reading the script horrible as i have to endlessly scroll.

The main changes are the first couple of columns in the select statement and then the way it groups by at the end.

Right now my current solution is to set strings and execute them. IE, I'll have

header1 = "SELECT FROM all..." 
header2 = "SELECT FROM board.."
header3 = "SELECT FROM school.."
footer1 = "GROUP BY lang"
footer2 = "GROUP BY lang, board"
footer3 = "GROUP BY lang, school" 
Common = CalculatedFields

EXECUTE (header1 + common + footer1)
EXECUTE (header2 + common + footer2)

but this just makes my script all red because its one large string. Not only that I have to break it up into different strings because I have a lot of text.

Any ideas on how to best do this while maintaining readability and clarity?

share|improve this question
    
Are these: WHERE [Include_Board] = 1 all the same? I mean the right-hand side is always 1? And what do those asterisks mean? –  dezso Jun 5 '12 at 16:36
    
The right side is one. THe left side is dynamic. Ie, include_all, include_school and include_baord –  masfenix Jun 5 '12 at 17:41
    
I may see it too simple (or too complicated), but why cannot you define three views (one for each query)? Then your script would look like SELECT * FROM view_A; three times. –  dezso Jun 5 '12 at 17:50
    
Because if there is a change (lets say the spec changes in one of the "sum" columns) then I have to change it three times which is error prone. Espeically when you have about 100 calculated fields. –  masfenix Jun 5 '12 at 18:09
    
Well, I would say then that you should keep you definition outside your query, and 'include' it somehow (say from a table - OK, it sounds awful, I admit). –  dezso Jun 5 '12 at 19:09

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.