Solved

SQL Query Help

Posted on 2013-01-12
11
409 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
  • 6
  • 5
11 Comments
 
LVL 12

Accepted Solution

by:
Habib Pourfard earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:zolf
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:zolf
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
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

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now