I have a quick question. This SQL executes fine in Sybase, but returns the following error in MS SQL Server
"Cannot perform an aggregate function on an expression containing an aggregate or a subquery." The problem is clear to me. The aggregate function Max() is being performed on the results of another aggregate function sum(). If I take out the MAX() function it works fine, but with incorrect results. The solution is not so clear to me
Do you have any thoughts about how to accomplish this using MS SQL Server?
thank
-Craig
Planned_Qty = MAX(isnull((select sum(t_Table_b.Planned_Qty
FROM t_Mfg_BOM t_Table_
WHER
t_Table_a.My_Location_Code = t_Table_b.My_Location_Code AN
t_Table_a.Mfg_Order_Nbr = t_Table_b.Mfg_Order_Nbr AN
t_Table_a.Mfg_Order_Operation_Nbr = t_Table_b.Mfg_Order_Operation_Nbr AN
t_Table_a.Item_Nbr = t_Table_b.Item_Nbr AN
((t_Table_a.BOM_Nbr = t_Table_b.BOM_Nbr) o
(t_Table_a.BOM_Nbr = t_Table_b.Alt_BOM_Nbr) o
(t_Table_a.Alt_For_BOM_Nbr = t_Table_b.BOM_Nbr) O
(t_Table_b.Alt_For_BOM_Nbr = t_Table_a.Alt_For_BOM_Nbr))),1))Put the SELECT with the subquery in a derived table and then GROUP it:
SELECT planned_qty = MAX(qty), ...
FROM
(SELECT ISNULL(
(SELECT SUM(t_Table_b.Planned_Qty)
FROM t_Mfg_BOM, t_Table_b
WHERE...)
,1)
FROM ...) AS X(qty, ...)
--
David Portas
SQL Server MVP
--sql
Showing posts with label returns. Show all posts
Showing posts with label returns. Show all posts
Thursday, March 22, 2012
Cannot perform an aggregate function on an expression containing an aggregate or a sub
I have a quick question. This SQL executes fine in Sybase, but returns the
following error in MS SQL Server:
"Cannot perform an aggregate function on an expression containing an aggrega
te or a subquery." The problem is clear to me. The aggregate function Max(
) is being performed on the results of another aggregate function sum(). If
I take out the MAX() funct
ion it works fine, but with incorrect results. The solution is not so clear
to me.
Do you have any thoughts about how to accomplish this using MS SQL Server?
thanks
-Craig-
Planned_Qty = MAX(isnull((select sum(t_Table_b.Planned_Qty)
FROM t_Mfg_BOM t_Table_b
WHERE
t_Table_a.My_Location_Code = t_Table_b.My_Location_Code AND
t_Table_a.Mfg_Order_Nbr = t_Table_b.Mfg_Order_Nbr AND
t_Table_a.Mfg_Order_Operation_Nbr = t_Table_b.Mfg_Order_Operation_Nbr AND
t_Table_a.Item_Nbr = t_Table_b.Item_Nbr AND
((t_Table_a.BOM_Nbr = t_Table_b.BOM_Nbr) or
(t_Table_a.BOM_Nbr = t_Table_b.Alt_BOM_Nbr) or
(t_Table_a.Alt_For_BOM_Nbr = t_Table_b.BOM_Nbr) OR
(t_Table_b.Alt_For_BOM_Nbr = t_Table_a.Alt_For_BOM_Nbr))),1))Put the SELECT with the subquery in a derived table and then GROUP it:
SELECT planned_qty = MAX(qty), ...
FROM
(SELECT ISNULL(
(SELECT SUM(t_Table_b.Planned_Qty)
FROM t_Mfg_BOM, t_Table_b
WHERE...)
,1)
FROM ...) AS X(qty, ...)
David Portas
SQL Server MVP
--
following error in MS SQL Server:
"Cannot perform an aggregate function on an expression containing an aggrega
te or a subquery." The problem is clear to me. The aggregate function Max(
) is being performed on the results of another aggregate function sum(). If
I take out the MAX() funct
ion it works fine, but with incorrect results. The solution is not so clear
to me.
Do you have any thoughts about how to accomplish this using MS SQL Server?
thanks
-Craig-
Planned_Qty = MAX(isnull((select sum(t_Table_b.Planned_Qty)
FROM t_Mfg_BOM t_Table_b
WHERE
t_Table_a.My_Location_Code = t_Table_b.My_Location_Code AND
t_Table_a.Mfg_Order_Nbr = t_Table_b.Mfg_Order_Nbr AND
t_Table_a.Mfg_Order_Operation_Nbr = t_Table_b.Mfg_Order_Operation_Nbr AND
t_Table_a.Item_Nbr = t_Table_b.Item_Nbr AND
((t_Table_a.BOM_Nbr = t_Table_b.BOM_Nbr) or
(t_Table_a.BOM_Nbr = t_Table_b.Alt_BOM_Nbr) or
(t_Table_a.Alt_For_BOM_Nbr = t_Table_b.BOM_Nbr) OR
(t_Table_b.Alt_For_BOM_Nbr = t_Table_a.Alt_For_BOM_Nbr))),1))Put the SELECT with the subquery in a derived table and then GROUP it:
SELECT planned_qty = MAX(qty), ...
FROM
(SELECT ISNULL(
(SELECT SUM(t_Table_b.Planned_Qty)
FROM t_Mfg_BOM, t_Table_b
WHERE...)
,1)
FROM ...) AS X(qty, ...)
David Portas
SQL Server MVP
--
Monday, March 19, 2012
Cannot obtain recordset from stored procedure that contains Exec c
Hopefully someone can help. Using vbscript and ADO, I can call a very simpl
e
stored procedure that returns a recordset w/o any problems. However, when I
call a stored procedure that stuffs records into another table using a SQL
string built and the Exec command and then select the records from that
table--ADO always reports the recordset is closed.
The stored procedure works fine in Query Analyzer. It also doesn't matter
if table is temp or permanent. My workaround is to first call the stored
procedure followed by a second call to get the records. My goal is to use a
temp table in the stored procedure.
Psuedo code of stored procedure (which also uses cursor):
declare @.sqlstr varchar (100)
set @.sqlstr = 'insert into tmptable select field from anothertable'
exec(@.sqpstr)
select * from tmptableAre you setting SET NOCOUNT ON at the begining of the sp?
AMB
"reedholm" wrote:
> Hopefully someone can help. Using vbscript and ADO, I can call a very sim
ple
> stored procedure that returns a recordset w/o any problems. However, when
I
> call a stored procedure that stuffs records into another table using a SQL
> string built and the Exec command and then select the records from that
> table--ADO always reports the recordset is closed.
> The stored procedure works fine in Query Analyzer. It also doesn't matter
> if table is temp or permanent. My workaround is to first call the stored
> procedure followed by a second call to get the records. My goal is to use
a
> temp table in the stored procedure.
> Psuedo code of stored procedure (which also uses cursor):
> declare @.sqlstr varchar (100)
> set @.sqlstr = 'insert into tmptable select field from anothertable'
> exec(@.sqpstr)
> select * from tmptable
>
e
stored procedure that returns a recordset w/o any problems. However, when I
call a stored procedure that stuffs records into another table using a SQL
string built and the Exec command and then select the records from that
table--ADO always reports the recordset is closed.
The stored procedure works fine in Query Analyzer. It also doesn't matter
if table is temp or permanent. My workaround is to first call the stored
procedure followed by a second call to get the records. My goal is to use a
temp table in the stored procedure.
Psuedo code of stored procedure (which also uses cursor):
declare @.sqlstr varchar (100)
set @.sqlstr = 'insert into tmptable select field from anothertable'
exec(@.sqpstr)
select * from tmptableAre you setting SET NOCOUNT ON at the begining of the sp?
AMB
"reedholm" wrote:
> Hopefully someone can help. Using vbscript and ADO, I can call a very sim
ple
> stored procedure that returns a recordset w/o any problems. However, when
I
> call a stored procedure that stuffs records into another table using a SQL
> string built and the Exec command and then select the records from that
> table--ADO always reports the recordset is closed.
> The stored procedure works fine in Query Analyzer. It also doesn't matter
> if table is temp or permanent. My workaround is to first call the stored
> procedure followed by a second call to get the records. My goal is to use
a
> temp table in the stored procedure.
> Psuedo code of stored procedure (which also uses cursor):
> declare @.sqlstr varchar (100)
> set @.sqlstr = 'insert into tmptable select field from anothertable'
> exec(@.sqpstr)
> select * from tmptable
>
Sunday, March 11, 2012
Cannot map tinyint to a SSIS data type in resultset
If I have an executesql task that returns a result set, if the result set contains a tinyint, I can't find what SSIS data type to declare the variable as. They all cause errors.
Answered my own question have a look at my post
http://www.sqljunkies.com/WebLog/simons/archive/2006/02/24/tinyint_in_SSIS.aspx
Cannot map tinyint to a SSIS data type in resultset
If I have an executesql task that returns a result set, if the result set contains a tinyint, I can't find what SSIS data type to declare the variable as. They all cause errors.
Answered my own question have a look at my post
http://www.sqljunkies.com/WebLog/simons/archive/2006/02/24/tinyint_in_SSIS.aspx
Thursday, February 16, 2012
Cannot Get Parameter to Display in Page Header
I have started a simple report that has two datasets. The first dataset just
returns all of the production orders. The second dataset is the main source
of the reports data and has production order as its parameter. For this
parameter, I have chosen the first dataset as the report parameter's source.
This works fine, and I am provided with a drop down box with all of the
production orders in the system. However, when I attempt to add a text box
to my existing Page Header that references the value of this report
parameter, the preview mode doesn't show my header at all. As soon as I
remove the text box, the header displays ok. The expression for the header
is =Parameters!ProdOrdNum.Value.
Am I missing something simple here? Thanks!Also, if I cut and paste the text box from the Header into the Body it
displays fine.
returns all of the production orders. The second dataset is the main source
of the reports data and has production order as its parameter. For this
parameter, I have chosen the first dataset as the report parameter's source.
This works fine, and I am provided with a drop down box with all of the
production orders in the system. However, when I attempt to add a text box
to my existing Page Header that references the value of this report
parameter, the preview mode doesn't show my header at all. As soon as I
remove the text box, the header displays ok. The expression for the header
is =Parameters!ProdOrdNum.Value.
Am I missing something simple here? Thanks!Also, if I cut and paste the text box from the Header into the Body it
displays fine.
Subscribe to:
Posts (Atom)