all groups > sql server programming > may 2007 >
You're in the

sql server programming

group:

Query Killing me


Query Killing me Hulicat
5/12/2007 8:34:10 AM
sql server programming:
I have the following query that executes:


select count(*) as 'Total', location_id,
problem_type.problem_type_name 'Problem Type'
from job_ticket inner join problem_type on
dbo.problem_type.problem_type_id =
job_ticket.problem_type_id where
(report_date between '1/01/2005' and '1/31/2007')-- and location_id
='6'
group by problem_type.PROBLEM_TYPE_NAME , location_id

output example:
total Location_id problem_type
50 8 add user


Output desired example:
total Location_id problem_type
50 California add user


Problem I need to diplay location_id as the "location"

I need to tie location_id to the table "Location" and display the
field Location_name

I tried the following with no success:


declare @client varchar (30)
declare @type varchar (30)
set @client=6
set @type= (SELECT location_name 'Client' FROM location where
location_id =cast (@client as varchar (20)))

Error:
Subquery returned more than 1 value. This is not permitted when the
subquery follows =, !=, <, <= , >, >= or when the subquery is used as
an expression.


I am stuck on this any help would be appreciated.

Thanks,
Re: Query Killing me Razvan Socol
5/12/2007 8:51:54 AM
Hello, Hulicat

Try something like this:

select count(*) as Total, location_name, problem_type_name
from job_ticket j
inner join problem_type p on p.problem_type_id = j.problem_type_id
inner join location l where l.location_id = j.location_id
where (report_date between '1/01/2005' and '1/31/2007')
group by p.problem_type_name, l.location_name

Razvan
Re: Query Killing me Hulicat
5/12/2007 9:34:09 AM
[quoted text, click to view]

Thank you very much Razvan; I just had to make a minor change to your
solution on line 4 and it works.
Again thanks for showing me how to do that...I am still learning T-SQL
and I could not figure our or find the solution.
Re: Query Killing me Hulicat
5/12/2007 9:37:52 AM
[quoted text, click to view]

Thanks for showing\teaching me how to do that...It's greatly
appreciated.
Re: Query Killing me David Portas
5/12/2007 4:47:18 PM
[quoted text, click to view]

Please post DDL and sample data. Please tell us what version of SQL Server
you are using. Based on the information you have given there is no way of
telling what the key of the location table is (the error message indicates
that location_id isn't unique). Without knowing the keys involved any answer
you get will be pure guesswork.

--
David Portas, SQL Server MVP

Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.

SQL Server Books Online:
http://msdn2.microsoft.com/library/ms130214(en-US,SQL.90).aspx
--

AddThis Social Bookmark Button