Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 200
  • Last Modified:

Syntax for 2 select statements

I need to make 1 select statement from these 2 statements-

select page_name, count(*) as Usage from masspricing_log where page_name = 'standard' group by page_name

select page_name, count(*) as Usage from masspricing_log where page_name = 'mass' group by page_name
0
sl1nger
Asked:
sl1nger
1 Solution
 
ptjcbCommented:
SELECT page_name, count(*)
FROM usage
WHERE page_name = 'standard'
OR page_name = 'mass'
GROUP BY page_name
0
 
Aneesh RetnakaranDatabase AdministratorCommented:
SELECT page_name, count(*)
FROM usage
WHERE page_name in( 'standard', 'mass')
GROUP BY page_name
0
 
ptjcbCommented:
This will return the page name and a count of every row from the usage table where the page name is equal to standard or the page name is equal to mass.

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Tejal06Commented:
SELECT
page_name,
(select count(*) from masspricing_log where page_name = 'standard' group by page_name) as Standard,
(select count(*) from masspricing_log where page_name = 'mass' group by page_name) as Mass
FROM masspricing_log
0
 
sl1ngerAuthor Commented:
ptjcb - It works in my sql editor, but when I put in my asp page it breaks with this error:  System.Data.SqlClient.SqlException: Incorrect syntax near ')'.

Any tips?
0
 
ptjcbCommented:
There is only the (*) in count, I doubt that that would have an affect. Could you show us more info from the asp code?
0
 
sl1ngerAuthor Commented:
strQuery = "SELECT page_name, count(*) as Usage FROM(masspricing_log)" & _
 "WHERE page_name = 'standard' OR page_name = 'mass' GROUP BY page_name"
0
 
ptjcbCommented:
strQuery = "SELECT page_name, count(*) as Usage FROM(masspricing_log)" & _
 "WHERE page_name = 'standard' OR page_name = 'mass' GROUP BY page_name"

***
strQuery = "SELECT page_name, count(*) as Usage FROM masspricing_log" & _
 "WHERE page_name = 'standard' OR page_name = 'mass' GROUP BY page_name"

You don't need the parenthesis around the table name.
0
 
sl1ngerAuthor Commented:
When I run a simple select statement it works fine, but when I use the above I get this error

Incorrect syntax near ')'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: Incorrect syntax near ')'.

Source Error:
Line 92:
Line 93:             da.Fill(table)

 
0
 
sl1ngerAuthor Commented:
now a different error when I removed the () around my table

Incorrect syntax near '='.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: Incorrect syntax near '='.

Source Error:

Line 92:
Line 93:             da.Fill(table)

 
0
 
ptjcbCommented:
You may need to add escape quotes around 'Standard' and 'Mass'.

I apologize but I'm not an ASP developer, so this is out of my range.


strQuery = "SELECT page_name, count(*) as Usage FROM masspricing_log" & _
 "WHERE page_name = "'standard'" OR page_name = "'mass'" GROUP BY page_name"
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now