Showing posts with label temptable. Show all posts
Showing posts with label temptable. Show all posts

Monday, March 26, 2012

Query Problem

This has to be obvious but I can't see the problem. I am executing the
query:
=======================================
Insert Into TempTable (email) Values (Select Distinct N1.Email From Names N1
Inner Join Bands B1 On B1.BandDirector = N1.NameID Where B1.BandClass In (
'A', 'AAA', 'AAAAA'))
=======================================
But I get a syntax error "near Select". Can someone straighten me out.
Thanks
WayneTry this
Insert Into TempTable (email) (Select Distinct N1.Email From Names N1
Inner Join Bands B1 On B1.BandDirector = N1.NameID Where B1.BandClass
In (
'A', 'AAA', 'AAAAA'))
Madhivanan|||Take out the VALUES keyword. You do not, and actually cannot, include that
keyword when performing an INSERT ... SELECT.
Richard
"Wayne Wengert" wrote:

> This has to be obvious but I can't see the problem. I am executing the
> query:
> =======================================
> Insert Into TempTable (email) Values (Select Distinct N1.Email From Names
N1
> Inner Join Bands B1 On B1.BandDirector = N1.NameID Where B1.BandClass In (
> 'A', 'AAA', 'AAAAA'))
> =======================================
>
> But I get a syntax error "near Select". Can someone straighten me out.
>
> Thanks
>
> Wayne
>
>|||This will do
Insert Into TempTable (email)
(Select Distinct N1.Email From Names N1
Inner Join Bands B1 On B1.BandDirector = N1.NameID Where B1.BandClass In (
'A', 'AAA', 'AAAAA'))
Roji. P. Thomas
Net Asset Management
https://www.netassetmanagement.com
"Wayne Wengert" <wayneDONTWANTSPAM@.wengert.com> wrote in message
news:u8lSldZHFHA.1176@.TK2MSFTNGP12.phx.gbl...
> This has to be obvious but I can't see the problem. I am executing the
> query:
> =======================================
> Insert Into TempTable (email) Values (Select Distinct N1.Email From Names
> N1
> Inner Join Bands B1 On B1.BandDirector = N1.NameID Where B1.BandClass In (
> 'A', 'AAA', 'AAAAA'))
> =======================================
>
> But I get a syntax error "near Select". Can someone straighten me out.
>
> Thanks
>
> Wayne
>|||You can't have a SELECT statement in the values clause.
Is this statement intended to insert one row or multiple rows.
Madhivanan's solution works for 1 row. The following will insert as
many rows as returned by the query:
INSERT INTO TempTable (email)
SELECT DISTINCT N1.Email
FROM Names N1
INNER JOIN Bands B1
ON B1.BandDirector = N1.NameID
WHERE B1.BandClass In ('A', 'AAA', 'AAAAA')
David Portas
SQL Server MVP
--|||Thanks all. I didn't realize that you could not use "Values" in that case.
Wayne
"Wayne Wengert" <wayneDONTWANTSPAM@.wengert.com> wrote in message
news:u8lSldZHFHA.1176@.TK2MSFTNGP12.phx.gbl...
> This has to be obvious but I can't see the problem. I am executing the
> query:
> =======================================
> Insert Into TempTable (email) Values (Select Distinct N1.Email From Names
N1
> Inner Join Bands B1 On B1.BandDirector = N1.NameID Where B1.BandClass In (
> 'A', 'AAA', 'AAAAA'))
> =======================================
>
> But I get a syntax error "near Select". Can someone straighten me out.
>
> Thanks
>
> Wayne
>

Query problem

I having trouble with the query below. I'm trying to
return the number of calls base on severity. The
#TempTable has 3 severities listed (1,2,&3). There are
no calls for severity 1, but i still want it to return a
record with a count of 0. A sample of what the query is
returning is at the bottom of this message. Please help.
select
Call.severity,
count(call.callno) as callcount
from
Call
left join Severity
On Call.Severity = Severity.severityID
where
datetimesubmitted between '01/01/2004' and '01/31/2004'
and
status <> 'CANCELLED'
group by Call.severity
severity callcount
-- --
2 4
3 25Since you've posted no DDL or sample data I can't say for sure, but you can
try:
select
Severity.severityID,
SUM(CASE WHEN Call.Severity = Severity.severityID THEN 1 ELSE 0 END) as
callcount
from
Severity
left join Call
On Call.Severity = Severity.severityID
where
datetimesubmitted between '01/01/2004' and '01/31/2004'
and
status <> 'CANCELLED'
group by Severity.severityID
"Vic" <vduran@.specpro-inc.com> wrote in message
news:179e501c421a7$41aa2060$a001280a@.phx.gbl...
> I having trouble with the query below. I'm trying to
> return the number of calls base on severity. The
> #TempTable has 3 severities listed (1,2,&3). There are
> no calls for severity 1, but i still want it to return a
> record with a count of 0. A sample of what the query is
> returning is at the bottom of this message. Please help.
>
> select
> Call.severity,
> count(call.callno) as callcount
> from
> Call
> left join Severity
> On Call.Severity = Severity.severityID
> where
> datetimesubmitted between '01/01/2004' and '01/31/2004'
> and
> status <> 'CANCELLED'
> group by Call.severity
>
> severity callcount
> -- --
> 2 4
> 3 25