Solved

SQL Query Help

Posted on 2013-01-12
11
447 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 

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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
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.

740 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