0

I am fairly new to SQL, so please bare that in mind.

I have have a table with data that is divided into two centers "Toronto & Montreal" for a total of 78 rows (39 per center), with multiple columns. I want to get the national total (sum) of X column for each respective center for a specific daterange (month). For example the Jan 2018 total of full time employees between Montreal + Toronto Centers combined. The logic statement for example would be to add Toronto + Montreal Jan 2018 results to yield me the national total. Although with my beginner skills I am having trouble writing a sql query that can be executed without syntax error.

select sum(fte), daterange, center
from (
select  'Toronto' as Center,
        sum(fte) as total fte
from dbo.example
where daterange = '2015-11-01'
group by total fte

union 

select  'Montreal' as Center,
        sum(fte) as total fte
from dbo.example
where daterange = '2015-11-01'
group by total fte
)temptable
group by total fte

The above query is giving me a error "Incorrect syntax near 'fte'." fte is a column within my table.

Please advise me what I am doing wrong.

Cheers!

6
  • 1
    You typically GROUP BY the columns you select, except those who are arguments to set functions (e.g. SUM.) Commented Mar 9, 2018 at 15:12
  • No need for that UNION. Simple use WHERE to pick the centers. Then GROUP BY center! Commented Mar 9, 2018 at 15:13
  • You need to delimit total fte, like 'total fte' Commented Mar 9, 2018 at 15:13
  • The query makes no sense . . . "Toronto" and "Montreal" woul dhave the same values. Also, your table name is dbo.example; the dbo schema is usually associated with SQL Server and not MySQL. Commented Mar 9, 2018 at 15:14
  • 1
    I prefer to keep my mind fully clothed, thanks. Commented Mar 9, 2018 at 15:16

2 Answers 2

3

The query should look like this:

select Center,
  sum(fte) as "[total fte]"
from dbo.example
where daterange = '2015-11-01'
  and Center in ('Toronto','Montreal')
group by Center

Understanding that in the attribute Center exist these Strings.

Sign up to request clarification or add additional context in comments.

1 Comment

Just put some quotes around that column alias.
0
select Center,
  sum(convert(float,fte)) as total_fte
from dbo.example
where daterange = '2016-11-01'
  and Center in ('Toronto','Montreal')
group by Center

Works ^ thanks everyone

1 Comment

You should probably about floats especially since I think these are dollar amounts.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.