I want to query a table once as the time to query this table is about 8-12 mins and querying it twice is simply too long. There is a flag in the table for identifying internal vs external results. I want the sum of column A & Sum of B where this flag is internal and the sum of column B & C & D where this flag is external.. right now its two separate queries joined together…
This is an example of the first query
Select
CONVERT(varchar(10), right([week_name],10), 111) as DatePeriod --weekly
,[Group] as [Group]
,SUM([columnA]) as [columnA_int]
,SUM([columnB]) as [columnB_int]
from myTbl
where internal_external = 'internal'
group by
CONVERT(varchar(10), right([week_name],10), 111),[Group]
second query
Select
CONVERT(varchar(10), right([week_name],10), 111) as DatePeriod --weekly
,[Group] as [Group]
,SUM([columnB]) as [columnB_ext]
,SUM([columnC]) as [columnC_ext]
,SUM([columnC]) as [columnC_ext]
from myTbl
where internal_external = 'external'
group by
CONVERT(varchar(10), right([week_name],10), 111),[Group]
so ideally one query all 5 columns and only hit the DB up once.
Try it like this:
Or, if you want to forgo the internal_external column: