[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

SQL Query Help

Posted on 2013-01-12
11
Medium Priority
?
463 Views
Last Modified: 2013-01-13
Hello there,

I get this error when i try to run this query.

Msg 102, Level 15, State 1, Line 23
Incorrect syntax near ')'.

SELECT COUNT(*) FROM
(SELECT 
 reportusercredential.id, 
 reportusercredential.username, 
 ru.id AS reportuserpermissionid, 
 reportusercredential.password, 
 reportusertype.id AS usertypeid, 
 Section.id AS sectionid,  
 reportusertype.name AS usertypename, 
 b.name AS branchname,  
 Section.name AS sectionname, 
 STUFF((SELECT ',' + name FROM Branch b WHERE ru.branchid = b.id FOR XML PATH ('')), 1, 1, '') AS branchname1
FROM 
 reportusercredential,
  reportuserpermission ru, 
 SECTION, 
 Branch b, 
 reportusertype 
WHERE 
 ru.reportuserid = reportusercredential.id 
 AND ru.sectionid=Section.id 
 AND ru.branchid = b.id 
 AND reportusertype.id = reportusercredential.usertypeid) 

Open in new window

0
Comment
Question by:zolf
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
11 Comments
 
LVL 12

Accepted Solution

by:
Habib Pourfard earned 2000 total points
ID: 38771194
You need to specify an alias:
SELECT COUNT(*) FROM (
SELECT reportusercredential.id
               ,reportusercredential.username
               ,ru.id AS reportuserpermissionid
               ,reportusercredential.password
               ,reportusertype.id AS usertypeid
               ,Section.id AS sectionid
               ,reportusertype.name AS usertypename
               ,b.name AS branchname
               ,Section.name AS sectionname
               ,STUFF((SELECT ',' + name FROM Branch b WHERE ru.branchid = b.id
                      FOR
                       XML PATH('')), 1, 1, '') AS branchname1
         FROM   reportusercredential
               ,reportuserpermission ru
               ,SECTION
               ,Branch b
               ,reportusertype
         WHERE  ru.reportuserid = reportusercredential.id
                AND ru.sectionid = Section.id
                AND ru.branchid = b.id
                AND reportusertype.id = reportusercredential.usertypeid) T

Open in new window

0
 

Author Comment

by:zolf
ID: 38771198
Oh ic,thanks a lot.

Now i have added GROUP BY to that query and i get another error.can you please help


Msg 8120, Level 16, State 1, Line 7
Column 'reportusercredential.username' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.


SELECT COUNT(*) FROM 

(SELECT  

 ruc.id, 
 
 ruc.username, 

 ru.id AS reportuserpermissionid, 

 ruc.password, 
 
 reportusertype.id AS usertypeid, 

 Section.id AS sectionid,  

 reportusertype.name AS usertypename, 
 
 b.name AS branchname,  

 Section.name AS sectionname, 

 STUFF((SELECT ',' + name FROM Branch b WHERE ru.branchid = b.id FOR XML PATH ('')), 1, 1, '') AS branchname1
 
 FROM reportusercredential ruc,

 reportuserpermission ru, 

 Section, 

  Branch b, 

 reportusertype  WHERE  ru.reportuserid = ruc.id 

  AND ru.sectionid=Section.id 

 AND ru.branchid = b.id 

 AND reportusertype.id = ruc.usertypeid AND (('1'='1')) GROUP BY ruc.id) as x

Open in new window

0
 
LVL 12

Expert Comment

by:Habib Pourfard
ID: 38771217
you have two options:
- include every field which exists on select clause in group by clause:
group by
ruc.id
,ruc.username
,ru.id AS reportuserpermissionid
,ruc.password
,reportusertype.id AS usertypeid
,Section.id AS sectionid
,....

- use aggregate functions like sum, max, ... for those fields which exists on select clause and not in group by clause:
MAX(ruc.username)
,MAX(ru.id)
,....
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:zolf
ID: 38771218
thanks,but how will i include the last field in the GROUP BY

STUFF((SELECT ',' + name FROM Branch b WHERE ru.branchid = b.id FOR XML PATH ('')), 1, 1, '') AS branchname1
0
 
LVL 12

Expert Comment

by:Habib Pourfard
ID: 38771229
GROUP BY ruc.id
               ,CAST (STUFF((SELECT ',' + name FROM Branch b WHERE ru.branchid = b.id FOR XML PATH('')), 1, 1, '') AS VARCHAR(MAX)))
0
 

Author Comment

by:zolf
ID: 38771246
Thanks for you rhelp. but it is not returning the result. i get error

Msg 144, Level 15, State 1, Line 29
Cannot use an aggregate or a subquery in an expression used for the group by list of a GROUP BY clause.

SELECT * FROM (SELECT *, ROW_NUMBER() OVER (ORDER BY x.id, usertypeid) AS rowID FROM (SELECT TOP 100 PERCENT   

 ruc.id,ruc.username,
 
ru.id AS reportuserpermissionid,ruc.password,

reportusertype.id AS usertypeid,

Section.id AS sectionid,

reportusertype.name AS usertypename,b.name AS branchname,Section.name AS sectionname,
 
STUFF((SELECT ',' + name FROM Branch b WHERE ru.branchid = b.id FOR XML PATH ('')), 1, 1, '') AS branchname1

		     FROM 
 
			   reportusercredential ruc, reportuserpermission ru,Section,Branch b,reportusertype

			 WHERE 

			    ru.reportuserid = ruc.id AND ru.sectionid=Section.id
 
				AND ru.branchid = b.id AND reportusertype.id = ruc.usertypeid AND (('1'='1'))

 			GROUP BY 

			   ruc.id,ruc.username,ru.id,ruc.password,reportusertype.id,Section.id,reportusertype.name,b.name,
 
			  Section.name,CAST (STUFF((SELECT ',' + name FROM Branch b WHERE ru.branchid = b.id FOR XML PATH('')), 1, 1, '') AS VARCHAR(MAX))

			 ) x) y WHERE y.rowID BETWEEN 1 AND 75

Open in new window

0
 
LVL 12

Expert Comment

by:Habib Pourfard
ID: 38771262
change usertypeid to x.usertypeid, it may solve the problem:
SELECT  *
FROM    (SELECT *
               ,ROW_NUMBER() OVER (ORDER BY x.id, x.usertypeid) AS rowID
         FROM   (SELECT ruc.id
                       ,ruc.username
                       ,ru.id AS reportuserpermissionid
                       ,ruc.password
                       ,reportusertype.id AS usertypeid
                       ,Section.id AS sectionid
                       ,reportusertype.name AS usertypename
                       ,b.name AS branchname
                       ,Section.name AS sectionname
                       ,STUFF((SELECT ',' + name FROM Branch b WHERE ru.branchid = b.id
                              FOR XML PATH('')), 1, 1, '') AS branchname1
                 FROM   reportusercredential ruc
                       ,reportuserpermission ru
                       ,Section
                       ,Branch b
                       ,reportusertype
                 WHERE  ru.reportuserid = ruc.id
                        AND ru.sectionid = Section.id
                        AND ru.branchid = b.id
                        AND reportusertype.id = ruc.usertypeid
                        AND (( '1' = '1'))
                 GROUP BY ruc.id
                       ,ruc.username
                       ,ru.id
                       ,ruc.password
                       ,reportusertype.id
                       ,Section.id
                       ,reportusertype.name
                       ,b.name
                       ,Section.name
                       ,CAST (STUFF((SELECT ',' + name FROM Branch b WHERE ru.branchid = b.id
                                    FOR XML PATH('')), 1, 1, '') AS VARCHAR(MAX))) x) y
WHERE   y.rowID BETWEEN 1 AND 75

Open in new window

0
 

Author Comment

by:zolf
ID: 38771265
no luck,when i run your query

Msg 144, Level 15, State 1, Line 35
Cannot use an aggregate or a subquery in an expression used for the group by list of a GROUP BY clause.
0
 
LVL 12

Expert Comment

by:Habib Pourfard
ID: 38771445
What about this one:
SELECT  *
FROM    (SELECT *
               ,ROW_NUMBER() OVER (ORDER BY x.id, x.usertypeid) AS rowID
         FROM   (SELECT ruc.id
                       ,ruc.username
                       ,ru.id AS reportuserpermissionid
                       ,ruc.password
                       ,reportusertype.id AS usertypeid
                       ,Section.id AS sectionid
                       ,reportusertype.name AS usertypename
                       ,b.name AS branchname
                       ,Section.name AS sectionname
                       ,STUFF((SELECT ',' + name FROM Branch b WHERE ru.branchid = b.id
                              FOR
                               XML PATH('')), 1, 1, '') AS branchname1
                 FROM   reportusercredential ruc
                       ,reportuserpermission ru
                       ,Section
                       ,Branch b
                       ,reportusertype
                 WHERE  ru.reportuserid = ruc.id
                        AND ru.sectionid = Section.id
                        AND ru.branchid = b.id
                        AND reportusertype.id = ruc.usertypeid
                        AND (( '1' = '1'))
                 GROUP BY ruc.id
                       ,ruc.username
                       ,ru.id
                       ,ruc.password
                       ,reportusertype.id
                       ,Section.id
                       ,reportusertype.name
                       ,b.name
                       ,Section.name) x) y
WHERE   y.rowID BETWEEN 1 AND 75

Open in new window

0
 

Author Comment

by:zolf
ID: 38771519
i get this error

Msg 8120, Level 16, State 1, Line 13
Column 'reportuserpermission.branchid' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
0
 
LVL 12

Expert Comment

by:Habib Pourfard
ID: 38773408
It depends on the logic, but this may solve:
SELECT  *
FROM    (SELECT *
               ,ROW_NUMBER() OVER (ORDER BY x.id, x.usertypeid) AS rowID
         FROM   (SELECT ruc.id
                       ,ruc.username
                       ,ru.id AS reportuserpermissionid
                       ,ruc.password
                       ,reportusertype.id AS usertypeid
                       ,Section.id AS sectionid
                       ,reportusertype.name AS usertypename
                       ,b.name AS branchname
                       ,Section.name AS sectionname
                       ,STUFF((SELECT ',' + name FROM Branch b WHERE MAX(ru.branchid) = b.id
                              FOR
                               XML PATH('')), 1, 1, '') AS branchname1
                 FROM   reportusercredential ruc
                       ,reportuserpermission ru
                       ,Section
                       ,Branch b
                       ,reportusertype
                 WHERE  ru.reportuserid = ruc.id
                        AND ru.sectionid = Section.id
                        AND ru.branchid = b.id
                        AND reportusertype.id = ruc.usertypeid
                        AND (( '1' = '1'))
                 GROUP BY ruc.id
                       ,ruc.username
                       ,ru.id
                       ,ruc.password
                       ,reportusertype.id
                       ,Section.id
                       ,reportusertype.name
                       ,b.name
                       ,Section.name) x) y
WHERE   y.rowID BETWEEN 1 AND 75

Open in new window

0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question