Showing posts with label active. Show all posts
Showing posts with label active. Show all posts

Thursday, March 29, 2012

Help with COUNT(*)

Dear SQL,

I want to count the number of records, so I tried this:


SELECT COUNT(*) AS RecordCount
FROM Categories
WHERE Active = 1
ORDER BY Show_Order ASC

But it gives me an error:
error 8126: Column name 'Categories.Show_Order' is invalid in the ORDER BY clause because it is not contained in an aggregate function and there is no GROUP BY clause.

How can I make it work ? (I must ORDER it...)SELECT COUNT(*) will return a single row with a single column containing an integer value. What are you expecting an ORDER BY to sort??

Terri|||As the error says, you cant do a count without a group. Here is my suggestion:


SELECT COUNT(fieldname) AS RecordCount FROM Categories WHERE active = 1 GROUP BY fieldname ORDER BY show_order

Instead of counting all columns just use one field. A Count requires a grouping even if there isnt anything to group. For instance you have a field called id that is a primary key. Group by. Order is ALWAYS at the end and the default is ASC so no need for ASC.|||As the error says, you cant do a count without a group.
That's not accurate. You can certainly do a COUNT without explicitly giving a grouping.

What the error is saying is that if you want use an ORDER BY clause, the expression being ordered by must exist in the resultset. With this in mind, the example you've given will not work because show_order does not exist in the resultset.

And, in order to add an expression to the resultset using an aggregate function such as COUNT, you need a GROUP BY clause.

In this case, this is likely what is needed but I am not sure because more information is needed from the original poster:


SELECT Show_Order, COUNT(*) AS RecordCount
FROM Categories
WHERE Active = 1
GROUP BY Show_Order
ORDER BY Show_Order ASC

And also, I think that explicitly indicating the sort direction ("ASC") is good practice because you never know when default behaviors might change. But that's a personal preference. :-)

Terri|||Hello again & thank god 4 this forum :-)

I have now succeeded in returning the number of records
but as Terri said - when I use GROUP BY it seems to limit my recordset to only one record,

I need this SP to be very efficient, so I like to SELECT only once, as U can see on the SP (below),
right now I use another SELECT at the end to determine the number of records to return...

Please let me know how to improve it or how to COUNT the records on the first selection
Thanks in advanced, Yovav.


/*================================================================================*/
/* Get categories (All / Titles / Subtitles) */
/*================================================================================*/
CREATE PROCEDURE Admin_Categories_Get

/*
' Usage example:
' ~~~~~~~~~~~
AdoCmd.CommandType = adCmdStoredProc
AdoCmd.CommandText = "Admin_Categories_Get"

' Return parameter comes first and can be used after recordset is closed
AdoCmd.Parameters.Append AdoCmd.CreateParameter("RETURN", adInteger, adParamReturnValue, 4)

' 0 (All), 1 (All active) 2 (All active compact), 10 (All titles), 11 (All active titles), 20 (All subtitles), 21 (All active subtitles)
AdoCmd.Parameters.Append AdoCmd.CreateParameter("@.ShowType", adTinyInt, adParamInput, 1, 0)

Set CategoriesRS = AdoCmd.Execute

CategoriesRS.Close
Response.Write("Return value = " &CStr(AdoCmd.Parameters.Item("RETURN").Value))
*/

@.ShowType tinyint

AS

DECLARE @.RecordCount int

IF @.ShowType = 0 -- (All)

SELECT *
FROM Categories
ORDER BY Show_Order ASC

ELSE
IF @.ShowType = 1 -- (All active)

SELECT *
FROM Categories
WHERE Active = 1 /* True */
ORDER BY Show_Order ASC

ELSE
IF @.ShowType = 2 -- (All active compact)

SELECT Category_ID, Title, Name_Eng, Name_Heb
FROM Categories
WHERE Active = 1 /* True */
ORDER BY Show_Order ASC

ELSE
IF @.ShowType = 10 -- (All titles)

SELECT *
FROM Categories
WHERE Title = 1 /* True */
ORDER BY Show_Order ASC
ELSE
IF @.ShowType = 11 -- (All active titles)

SELECT *
FROM Categories
WHERE Active = 1 /* True */ AND Title = 1 /* True */
ORDER BY Show_Order ASC

ELSE
IF @.ShowType = 20 -- (All subtitles)

SELECT *
FROM Categories
WHERE Title = 0 /* False */
ORDER BY Show_Order ASC
ELSE
IF @.ShowType = 21 -- (All active subtitles)

SELECT *
FROM Categories
WHERE Active = 1 /* True */ AND Title = 0 /* False */
ORDER BY Show_Order ASC

-- Count *ALL* records on table Categories
SELECT @.RecordCount = COUNT(*) FROM Categories

RETURN @.RecordCount
GO

|||...

oh dear goodness.

In the spirit of the holidays..

Why don't you just send in Active and Title as parameters? It appears to me that they're bits, and two bits are smaller in size than one integer.|||it wont help, coz sometimes I need to do things according to the ShowType
+
my main problem was how to return the COUNT of records together with the recordset...

Tuesday, March 27, 2012

Help with Cluster

I inherited a SQL 2000 Enterprise active/passive cluster under AS. Both
nodes run on identical HP DL580's with 4 Xeon processors and 8GB memory.
The former dba attempted to setup AWE memory but missed a couple of fine
points. The first thing missed: the boot.ini only has the /PAE and no /3GB
switch. The second thing missed: sp_configure 'awe enabled' is set to 0.
As a result, SQL gets only 2GB of virtual memory and 0GB of PAE memory.

My question is how do I fix this on an active/passive cluster. Here is what
I think. Add the /3GB switch to the passive node and reboot the passive
node. Now it seems I am in trouble. If I pause the active node, I cause
the passive node to wake up, but the memory images do not match. If I start
with the active node and add the /3GB switch and reboot, I fail-over to the
passive. Everything is still OK, the memory matches until I reboot, then
the memory maps do not match.

So, do I break the cluster, install the /3GB switch on each box and then
recreate the cluster? Isn't there an easier way?

Also, can I enable AWE before I begin the /3GB shuffle. I seems I could
avoid a second dance.

Please help.

GaryGary,

I can answer one of your questions.

> Also, can I enable AWE before I begin the /3GB shuffle. I seems I could
> avoid a second dance.

You do not need to use the "/3GB" switch in order to use AWE memory.
You may find an explanation of each switch helpful:

The "/PAE" switch allows Windows to use memory above the 4GB native
limit. So this switch is needed to enable Windows to address memory
above 4GB. (Additionally, you must use a Server Operating System that
supports more than 4GB. You're using Windows 2000 Advanced Server,
which allows up to 8GB to be addressed.)

The "/3GB" switch allows Windows applications to use 3GB out of the
4GB Windows supports natively thereby leaving 1GB of memory for the
Windows OS. By default (i.e., not using the "/3GB" switch),
applications will only use 2GB out of the first 4GB on the server and
leave 2GB for the Windows OS.

You have a total of 8GB of RAM. If you don't use the "/3GB" switch,
then SQL Server will still have 6GB of RAM available to use. You may
prefer to leave the 2GB available to the OS rather than take it for
SQL Server. But at you stated, you must set the "awe enabled" option
from "0" to "1" and restart the SQL Server service. In case you are
not aware, AWE does not dynamically manage memory like SQL Server does
by default, so if you have more than one instance of SQL Server
running on each node in the cluster, you may not want to use AWE
memory.

You may find the following link helpful:

AWE Memory SQL Server Performance Tuning Tips
http://www.sql-server-performance.com/awe_memory.asp

HOW TO: Configure Memory for More Than 2 GB in SQL Server
http://support.microsoft.com/defaul...KB;EN-US;274750

Large Memory Support Is Available in Windows 2000 and Windows Server
2003
http://support.microsoft.com/defaul...b;EN-US;q283037

SQL Server Books Online Topics "Managing AWE Support", "Effects of min
and max server memory"; "Memory Architecture"; "Memory Architecture";
"Server Memory Options"; "SQL Server Memory Pool"

I hope you find this information helpful.

Regards,

Matt Patterson
HP Services
Business Critical Enterprise Team
Microsoft Database Supportsql