Solved

SQL Query Help

Posted on 2013-01-12
11
453 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 500 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

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 ?
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

688 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