Solved

SQL Query Help

Posted on 2013-01-12
11
437 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
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
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 

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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
tempdb log keep growing 7 33
Select single row of data for each ID in Select Statement 7 26
SQL Query 2 31
Accessing variables in MySQL query 4 30
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
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 ?
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

856 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