Showing posts with label thisselect. Show all posts
Showing posts with label thisselect. 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 CASE T-SQL

Anyone have much experience with SQL Server T-SQL CASE statement?
I have some code that someone else wrote that looks like this:
SELECT m.messageID, m.isTop,
CASE @.Mode
WHEN 10 THEN m.subject
WHEN 12 THEN p.userID
END
FROM Messages m
INNER JOIN People p ON m.userID=p.userID
WHERE [blah][blah][blah].
What I need to do is return something that is not the userID, but still have
that name. This did not work:
SELECT m.messageID, m.isTop,
CASE @.Mode
WHEN 10 THEN m.subject
WHEN 12 THEN pr.userName As userID
END
FROM Messages m
INNER JOIN People p ON m.userID=p.userID
INNER JOIN Personalize pr ON m.userID=pr.userID
WHERE [blah][blah][blah].
SQL dies on the "As"... (or, if I leave out the "As", it dies on "userID")
Any idea how I can do this?
Thanks,
OwenCASE @.Mode
WHEN 10 THEN m.subject
WHEN 12 THEN pr.userName
END As UserID
"Owen Mortensen" <ojm.NO_SPAM@.acm.org> wrote in message
news:OWsmKlxPGHA.3460@.TK2MSFTNGP15.phx.gbl...
> Anyone have much experience with SQL Server T-SQL CASE statement?
> I have some code that someone else wrote that looks like this:
> SELECT m.messageID, m.isTop,
> CASE @.Mode
> WHEN 10 THEN m.subject
> WHEN 12 THEN p.userID
> END
> FROM Messages m
> INNER JOIN People p ON m.userID=p.userID
> WHERE [blah][blah][blah].
> What I need to do is return something that is not the userID, but still
> have that name. This did not work:
> SELECT m.messageID, m.isTop,
> CASE @.Mode
> WHEN 10 THEN m.subject
> WHEN 12 THEN pr.userName As userID
> END
> FROM Messages m
> INNER JOIN People p ON m.userID=p.userID
> INNER JOIN Personalize pr ON m.userID=pr.userID
> WHERE [blah][blah][blah].
> SQL dies on the "As"... (or, if I leave out the "As", it dies on "userID")
> Any idea how I can do this?
> Thanks,
> Owen
>|||Thanks. This variation on that theme actually worked:
CASE @.Mode WHEN 10 THEN m.subject END As subject,
CASE @.Mode WHEN 12 THEN pr.userName END As UserID
"Norman Yuan" <NotReal@.NotReal.not> wrote in message
news:utqlB9xPGHA.1216@.TK2MSFTNGP14.phx.gbl...
> CASE @.Mode
> WHEN 10 THEN m.subject
> WHEN 12 THEN pr.userName
> END As UserID
>
> "Owen Mortensen" <ojm.NO_SPAM@.acm.org> wrote in message
> news:OWsmKlxPGHA.3460@.TK2MSFTNGP15.phx.gbl...
>

Wednesday, March 21, 2012

Help with a query

Hi to all,
Im using a query like this:
select A.AfectadoApellidos as Apellido, A.AfectadoName,
A.AfectadoDNI,(Convert(varchar,TA. TipoAccionBreve)+Convert(varchar,Empresa
s.
EmpresaBreve)+Convert(varchar,E.ExpedienteIncidenciaID)) as Codigos
from Expedientes E
inner join Afectados A on E.AfectadoID=A.AfectadoID
inner join Empresas on E.EmpresaID=Empresas.EmpresaID
inner join TiposAccion TA on E.TipoAccionID=TA.TipoAccionID
group by A.AfectadoApellidos,
A.AfectadoName,A.AfectadoDNI,E.ExpedienteIncidenciaID,Empresas.EmpresaBreve,
TA.TipoAccionBreve
and im getting this result:
Apellido AfectadoName AfectadoDNI Codigos
-- -- -- --
Surname1 Name1 DNI1 CodeAAA
Surname2 Name2 DNI2 CodeBBB
Surname2 Name2 DNI2 CodeCCC
I would like to get anything like this
Apellido AfectadoName AfectadoDNI Codigos
-- -- -- --
Surname1 Name1 DNI1 CodeAAA
Surname2 Name2 DNI2 CodeBBB--CodeCCC
How could i get only a row for the name 2 surname 2?
--
Thanks a lot.
Regards.
JosemaCheck this.. For the concat string stuff...
[url]http://omnibuzz-sql.blogspot.com/2006/06/concatenate-values-in-column-in-sql.html[
/url]
-Omnibuzz (The SQL GC)
"Josema" wrote:

> Hi to all,
> Im using a query like this:
> select A.AfectadoApellidos as Apellido, A.AfectadoName,
> A.AfectadoDNI,(Convert(varchar,TA. TipoAccionBreve)+Convert(varchar,Empresa
s.EmpresaBreve)+Convert(varchar,E.ExpedienteIncidenciaID)) as Codigos
> from Expedientes E
> inner join Afectados A on E.AfectadoID=A.AfectadoID
> inner join Empresas on E.EmpresaID=Empresas.EmpresaID
> inner join TiposAccion TA on E.TipoAccionID=TA.TipoAccionID
> group by A.AfectadoApellidos,
> A.AfectadoName,A.AfectadoDNI,E.ExpedienteIncidenciaID,Empresas.EmpresaBrev
e,
> TA.TipoAccionBreve
>
> and im getting this result:
> Apellido AfectadoName AfectadoDNI Codigos
> -- -- -- --
> Surname1 Name1 DNI1 CodeAAA
> Surname2 Name2 DNI2 CodeBBB
> Surname2 Name2 DNI2 CodeCCC
>
> I would like to get anything like this
> Apellido AfectadoName AfectadoDNI Codigos
> -- -- -- --
> Surname1 Name1 DNI1 CodeAAA
> Surname2 Name2 DNI2 CodeBBB--CodeCCC
>
> How could i get only a row for the name 2 surname 2?
> --
> Thanks a lot.
> Regards.
> Josema|||If you don't need the concat and any row is fine, then you may use this..
select A.AfectadoApellidos as Apellido, A.AfectadoName,
A.AfectadoDNI,min(Convert(varchar,TA.TipoAccionBreve)+Convert(varchar,Empres
as.EmpresaBreve)+Convert(varchar,E.ExpedienteIncidenciaID)) as Codigos
from Expedientes E
inner join Afectados A on E.AfectadoID=A.AfectadoID
inner join Empresas on E.EmpresaID=Empresas.EmpresaID
inner join TiposAccion TA on E.TipoAccionID=TA.TipoAccionID
group by A.AfectadoApellidos, A.AfectadoName,A.AfectadoDNI
Hope this helps.
--
-Omnibuzz (The SQL GC)
http://omnibuzz-sql.blogspot.com/

Wednesday, March 7, 2012

help turning this Sql statement into a stored procedure

Hello, I need a little help turning this:
SELECT RequestNum FROM Tickets WHERE ReceiptDate>='" & FromDate & "' AND ReceiptDate<='" & ToDate & "'"
into a sproc because of the two different values (FromDate and ToDate) for the ReceiptDate field in the database.
I have this so far (problem areas are ??):
Dim AuditConnection As New SqlConnection(ConnString)
Dim AuditCommand As New SqlCommand("CreateAudit", AuditConnection)
AuditCommand.CommandType = CommandType.StoredProcedure
AuditCommand.Parameters.Add(New SqlParameter("@.??", SqlDbType.NVarChar)).Value = FromDate
AuditCommand.Parameters.Add(New SqlParameter("@.??", SqlDbType.NVarChar)).Value = ToDate
AuditConnection.Open()
Dim AuditResult As SqlDataReader = AuditCommand.ExecuteReader()
AuditGrid.DataSource = AuditResult
AuditGrid.DataBind()
AuditConnection.Close()
and:
CREATE PROCEDURE CreateAudit
??
??

AS
SELECT
RequestNum
FROM
Tickets
WHERE
??
AND
??
GO
I know I'm an idiot and this should be something simple. Arrrgh. Any help is appreciated immensely!!! :)

Dim AuditConnection As New SqlConnection(ConnString)
Dim AuditCommand As New SqlCommand("CreateAudit", AuditConnection)
AuditCommand.CommandType = CommandType.StoredProcedure
AuditCommand.Parameters.Add(New SqlParameter("@.FromDate", SqlDbType.DateTime)).Value = FromDate
AuditCommand.Parameters.Add(New SqlParameter("@.ToDate", SqlDbType.DateTime)).Value = ToDate
AuditConnection.Open()
Dim AuditResult As SqlDataReader = AuditCommand.ExecuteReader()
AuditGrid.DataSource = AuditResult
AuditGrid.DataBind()
AuditConnection.Close()
and:
CREATE PROCEDURE CreateAudit
@.FromDate datetime,
@.ToDate datetime

AS
SELECT
RequestNum
FROM
Tickets
WHERE ReceiptDate>=@.FromDate AND ReceiptDate<=@.ToDate

Presumes ReceiptDate, FromDate and ToDate are datetime types and not nvarchar|||aaaaahhhh, I see...THANK YOU!!!