Showing posts with label condition. Show all posts
Showing posts with label condition. Show all posts

Thursday, March 29, 2012

Cursor versus Temporary Table

Hi All,
I am writing a stored procedure in which I need to read some records from a table which satisfy given condition, fetch the last read record and check its value.

e.g.
SELECT *
FROM TestRequestState
WHERE StateId = '11'
ORDER BY TestReqNo.

I want to read the last record of each TestReqNo and check some values from that row. For this I was thiking of reading the above Select using a cursor and then using FETCH LAST to read the last row into some variable.

But this seems to be a round about way and also i have been reading that cursor will slow the execution. IS there any other better way. Should I use temp table instead, will that be more efficient ?

Please let me know your suggestions.
Thanks,
SnigdhaYou can reverse the order and just select the first record:

SELECT TOP 1 *
FROM TestRequestState
WHERE StateId = '11'
ORDER BY TestReqNo DESC|||Hi,
Thanks a lot for this solution, but there is still a little problem. The above query returns the last record of the last TestRequestNO. But I wanted to the last record of each TestRequestNO. For e.g.:

TestRequestNo TestRequestStateId
TR123 11
TR123 3
TR123 5
TR123 12

TR155 11
TR155 3
TR155 5

TR007 11
TR007 12
TR007 3

Now, out of this orderd set of TestRequestNos I want the last record of each of the TestRequestNo: TR123, TR155, TR007.

Thanks,
Snigdha

|||

So, something along these lines?

USE Northwind

SELECT t1.* FROM [order details] t1
WHERE t1.Quantity=
(SELECT MAX(Quantity) FROM [order details] t2
WHERE t1.orderid=t2.orderid)
ORDER BY t1.orderid

SELECT t1.* FROM [order details] t1 INNER JOIN
(SELECT orderid, MAX(Quantity) AS maxdate FROM [order details] GROUP BY orderid) t2
ON t1.orderid = t2.orderid
AND t1.Quantity = t2.maxdate
ORDER BY t1.orderid
--
Frank Kalis
Microsoft SQL Server MVP
http://www.insidesql.de
Ich unterstütze PASS Deutschland e.V. (http://www.sqlpass.de)

|||Hey,
Thanks a loooot...
the first query worked perfectly the way I wanted it !!!! :)

There are minor things remaining which I think I should be able to handle.
Thanks a lot once again,
Snigdha

Sunday, March 11, 2012

Cumulative Total

I try to sum a value in the TABLE FOOTER if this record falls into a
particular condition. But the result is zero.
The formular is as follow:
=Sum(IIf(Fields!EQP_LEN.Value = 20 And Fields!EQP_TYPE.Value = "GP",
Fields!CN_WGT.Value, 0))
If I the formular like this:
=Sum(IIf(Fields!EQP_LEN.Value = 20 And Fields!EQP_TYPE.Value = "GP", 1, 0))
It works !!!
What's wrong ?What's the datatype of Fields!CN_WGT.Value? I would assume it is not a
System.Int32, but 0 is a System.Int32.
You might want to try 0.0 instead of 0:
=Sum(IIf(Fields!EQP_LEN.Value = 20 And Fields!EQP_TYPE.Value = "GP",
Fields!CN_WGT.Value, 0.0))
Alternatively:
=Sum(IIf(Fields!EQP_LEN.Value = 20 And Fields!EQP_TYPE.Value = "GP",
CDbl(Fields!CN_WGT.Value), 0.0))
--
This posting is provided "AS IS" with no warranties, and confers no rights.
"May Liu" <MayLiu@.discussions.microsoft.com> wrote in message
news:26102E03-4F38-4F38-872E-633F5E02B78B@.microsoft.com...
> I try to sum a value in the TABLE FOOTER if this record falls into a
> particular condition. But the result is zero.
> The formular is as follow:
> =Sum(IIf(Fields!EQP_LEN.Value = 20 And Fields!EQP_TYPE.Value = "GP",
> Fields!CN_WGT.Value, 0))
> If I the formular like this:
> =Sum(IIf(Fields!EQP_LEN.Value = 20 And Fields!EQP_TYPE.Value = "GP", 1,
0))
> It works !!!
> What's wrong ?|||Thanks, it works now !!!
"Robert Bruckner [MSFT]" wrote:
> What's the datatype of Fields!CN_WGT.Value? I would assume it is not a
> System.Int32, but 0 is a System.Int32.
> You might want to try 0.0 instead of 0:
> =Sum(IIf(Fields!EQP_LEN.Value = 20 And Fields!EQP_TYPE.Value = "GP",
> Fields!CN_WGT.Value, 0.0))
> Alternatively:
> =Sum(IIf(Fields!EQP_LEN.Value = 20 And Fields!EQP_TYPE.Value = "GP",
> CDbl(Fields!CN_WGT.Value), 0.0))
> --
> This posting is provided "AS IS" with no warranties, and confers no rights.
>
>
> "May Liu" <MayLiu@.discussions.microsoft.com> wrote in message
> news:26102E03-4F38-4F38-872E-633F5E02B78B@.microsoft.com...
> > I try to sum a value in the TABLE FOOTER if this record falls into a
> > particular condition. But the result is zero.
> > The formular is as follow:
> > =Sum(IIf(Fields!EQP_LEN.Value = 20 And Fields!EQP_TYPE.Value = "GP",
> > Fields!CN_WGT.Value, 0))
> >
> > If I the formular like this:
> > =Sum(IIf(Fields!EQP_LEN.Value = 20 And Fields!EQP_TYPE.Value = "GP", 1,
> 0))
> > It works !!!
> >
> > What's wrong ?
>
>

Sunday, February 19, 2012

CTE Optimization

Hi,
Below is the traditional use of CTE for retrieving nodes of a tree. My
question is that when I use a condition like 'where lvl<=2' the execution
plan shows that filtering the result is the final phase of execution. Does
it mean that if I have a deep level of hierarchies in my table, the
performance will not be good? Will it prepare all of the records and then
filters the result?
Thanks in advance,
Leila
--
USE Northwind
GO
WITH MyChart(EmployeeID,EmpName,BossID,BossNa
me,lvl) AS
(SELECT EmployeeID,FirstName,EmployeeID,FirstNam
e, 1
FROM Employees WHERE EmployeeID=2
UNION ALL
SELECT Emp.EmployeeID,Emp.FirstName,MyChart.EmployeeID,
MyChart.EmpName, MyChart.lvl+1
FROM Employees Emp INNER JOIN MyChart
ON Emp.ReportsTo=MyChart.EmployeeID
)
SELECT * FROM MyChart
where lvl<=2Yes -- why don't you put your filter in the recursive query instead?
Adam Machanic
Pro SQL Server 2005, available now
http://www.apress.com/book/bookDisplay.html?bID=457
--
"Leila" <Leilas@.hotpop.com> wrote in message
news:uAXNjWzKGHA.740@.TK2MSFTNGP12.phx.gbl...
> Hi,
> Below is the traditional use of CTE for retrieving nodes of a tree. My
> question is that when I use a condition like 'where lvl<=2' the execution
> plan shows that filtering the result is the final phase of execution. Does
> it mean that if I have a deep level of hierarchies in my table, the
> performance will not be good? Will it prepare all of the records and then
> filters the result?
> Thanks in advance,
> Leila
> --
> USE Northwind
> GO
> WITH MyChart(EmployeeID,EmpName,BossID,BossNa
me,lvl) AS
> (SELECT EmployeeID,FirstName,EmployeeID,FirstNam
e, 1
> FROM Employees WHERE EmployeeID=2
> UNION ALL
> SELECT Emp.EmployeeID,Emp.FirstName,MyChart.EmployeeID,
> MyChart.EmpName, MyChart.lvl+1
> FROM Employees Emp INNER JOIN MyChart
> ON Emp.ReportsTo=MyChart.EmployeeID
> )
> SELECT * FROM MyChart
> where lvl<=2
>