- Home
- Anonymous
- Sign in
- Create
- Ask a question
- Spaces
- Site Issues (NOT FOR DATABASE QUESTIONS)
- Explore
- Topics
- Questions
- Users
- Badges

Hi I have a select state that if the NAME column is 4,5, or 6 then do a select else do another select. Below is my select statement..... SELECT (SUM(BUD1_BUD_AMOUNT_01)+ SUM(BUD1_BUD_AMOUNT_02)+ SUM(BUD1_BUD_AMOUNT_03)+ SUM(BUD1_BUD_AMOUNT_04)+ SUM(BUD1_BUD_AMOUNT_05)+ SUM(BUD1_BUD_AMOUNT_06)+ SUM(BUD1_BUD_AMOUNT_07)+ SUM(BUD1_BUD_AMOUNT_08)+ SUM(BUD1_BUD_AMOUNT_09)+ SUM(BUD1_BUD_AMOUNT_10)+ SUM(BUD1_BUD_AMOUNT_11)+ SUM(BUD1_BUD_AMOUNT_12)) / (SUM(BUD1_BUD_UNITS_01)+ SUM(BUD1_BUD_UNITS_02)+ SUM(BUD1_BUD_UNITS_03)+ SUM(BUD1_BUD_UNITS_04)+ SUM(BUD1_BUD_UNITS_05)+ SUM(BUD1_BUD_UNITS_06)+ SUM(BUD1_BUD_UNITS_07)+ SUM(BUD1_BUD_UNITS_08)+ SUM(BUD1_BUD_UNITS_09)+ SUM(BUD1_BUD_UNITS_10)+ SUM(BUD1_BUD_UNITS_11)+ SUM(BUD1_BUD_UNITS_12)) *-1 AS 'PLAN',NAME FROM [test].[dbo].[report] where name is not null and acct_unit in ('4111','4611') and account in ('3100','3101','3102','3103','3104','3105','3261') GROUP BY NAME ------------------- else select statement is.... SELECT (SUM(BUD1_BUD_AMOUNT_01)+ SUM(BUD1_BUD_AMOUNT_02)+ SUM(BUD1_BUD_AMOUNT_03)+ SUM(BUD1_BUD_AMOUNT_04)+ SUM(BUD1_BUD_AMOUNT_05)+ SUM(BUD1_BUD_AMOUNT_06)+ SUM(BUD1_BUD_AMOUNT_07)+ SUM(BUD1_BUD_AMOUNT_08)+ SUM(BUD1_BUD_AMOUNT_09)+ SUM(BUD1_BUD_AMOUNT_10)+ SUM(BUD1_BUD_AMOUNT_11)+ SUM(BUD1_BUD_AMOUNT_12)) / (SUM(BUD1_BUD_UNITS_01)+ SUM(BUD1_BUD_UNITS_02)+ SUM(BUD1_BUD_UNITS_03)+ SUM(BUD1_BUD_UNITS_04)+ SUM(BUD1_BUD_UNITS_05)+ SUM(BUD1_BUD_UNITS_06)+ SUM(BUD1_BUD_UNITS_07)+ SUM(BUD1_BUD_UNITS_08)+ SUM(BUD1_BUD_UNITS_09)+ SUM(BUD1_BUD_UNITS_10)+ SUM(BUD1_BUD_UNITS_11)+ SUM(BUD1_BUD_UNITS_12)) *-1 AS 'PLAN',NAME FROM [CRCUtilities].[dbo].[GLREPORTING] where name is not null and acct_unit in ('4111','4611') and account in ('3100','3101') GROUP BY NAME --------- Thanks in advance

tsqlcase-statement
Comment

It sounds as though you need something like: SELECT (SUM(....)) AS 'Plan', Name FROM test.dbo.report WHERE name is not null AND account in (...) AND ( ( name IN (4,5,6) AND acct_unit in ('4111', '4611') ) OR ( name NOT IN (4,5,6) AND acct_unit = '4111' ) ) Something like that. Thinking about it, that could probably be compressed further...

You need an IF Else statement. IF select something from table where (condition) value Begin END Else I can't tell from your query what is unique between the two since each has WHERE name is not null and acct_unit in ('4111','4611') and both included account in ('3100','3101').

**10** People are following this question.

Copyright 2022 Redgate Software.
Privacy Policy