Conditions On Latest Record

May 18, 2006

I have a table that has records layed out as so:

Table:
fd_Id INT IDENTITY (1, 1)
fd_User VARCHAR(30)
fd_Effective DATETIME

Data could be as follows:
1 | "user1" | 6/20/2001
2 | "user2" | 6/1/2002
3 | "user2" | 6/5/2002
4 | "user2" | 6/5/2002
5 | "user2" | 2/1/2002
6 | "user3" | 9/1/2003
7 | "user3" | 10/2/2002
8 | "user4" | 1/1/2005

What I need to retrieve from that table is the SINGLE LATEST item of
each fd_User.

Results:
1 | "user1" | 6/20/2001
3 | "user2" | 6/5/2002 (or 4 | "user2" | 6/5/2002) since the dates are
the same but only 1 of them
6 | "user3" | 9/1/2003
8 | "user4" | 1/1/2005

View 3 Replies


ADVERTISEMENT

Create Output Which Just Reflects Latest Conditions When Joining Back To Contract

Oct 10, 2013

col 1 is the pk for this table called Conditions - this table is related to contract.

Col 1 - PKContract TypeType ID
973300 711917 C30
973301 711917 C32
973302 711917 C31
1152323 711917 C30
1152324 711917 C31
1152325 711917 C32

A contract can many conditions so 1:M...Col 2 is the contract reference and the linking join to contract

Now - a condition related to an contract can have many re-trys and the causes the pk to increment. As you can see there are three conditions related to first attempt and then another three conditions.I want to create an ouput which just reflects the latest conditions when joining back to contract - Is this possible?I have requested to application providers to provide flag, but this will take some time...

View 8 Replies View Related

Latest Record

Sep 20, 2007

My table are
Customer: customerId ,name
Order: orderId, customerId, product,date
I want to display latest order  from the customer
 
 
 

View 2 Replies View Related

Latest Record For Particular ID

Jan 24, 2014

I have table has column called [last modified date] it has time too , i need to get the [latest modified date] for particular ID , there are many updates to the ID and i just need the latest date.

[PAX ID] | [Last Modified Date]
1 2013-01-02 18:23:00
1 2013-01-02 11:42:00
3 2013-01-01 09:00:00
3 2013-01-08 16:05:00
5 2013-01-01 09:09:00

View 2 Replies View Related

T-SQL (SS2K8) :: Selecting The Top Record With Certain Conditions?

May 14, 2014

The situation is that we have resources (trucks) that perform shifts. Shifts consists of actions. A resource can perform multiple shifts.

For every resource we want to find the record that:

- Is 'younger' than the last realized action.

- Has actionkind pickup, deliver or clean

I have constructed a solution with CTE and row_number but I was curious if there would be other alternatives. The fact that I'm joining a CTE onto itself and subject the outcome to a partition makes me think there are sharper ways.

Note that the action id in the data below is also sorted but in practice this need not be the case. The sorting key is prevalent.

output of the query is

id_action id_resource actionKindCode
4665 4 clean
34540 96 pickup
24000 901 clean
declare @mytable table (
id_action int,
id_shift int,

[code]....

View 6 Replies View Related

Get Record With Latest Date

Jun 4, 2007

 if there are 2 records with different date  how to write query for ----> get record with latest date 

View 4 Replies View Related

Group By And Latest Record

Aug 8, 2013

Here's what I'm trying to do

TransIDMemberIDFundIDBuyPriceDate
1501101$1042013-01-24
2501102$1012013-04-23
3501102$1182013-02-04
4501102$982013-05-19
5501103$532013-05-21
6501103$392013-07-09

I'm trying to get the latest buy price for each fund a member bought.

So in the above example, I'm trying to formulate a query to retrieve all the latest bought price for memberid 105:

Desire Query Result

TransIDMemberIDFundIDBuyPriceDate
1501101$1042013-01-24
4501102$982013-05-19
6501103$392013-07-09

So far, I've tried using Group By clause on FundID and BuyPrice and MAX() function for the date, but I can' get the TransID.

View 3 Replies View Related

How To Get Latest Date Record

Apr 10, 2014

I want to get latest updated date on each transid and only for status =approved .

-------------------
out put would be
------------
idtransid date status
31013/1/2014 approved
61031/2/2014 approved

table
-----------
idtransid date status
11011/1/2014 approved
21012/1/2014 close
31013/1/2014 approved
41021/2/2014 approved
51022/2/2014 close
61031/2/2014 approved

View 1 Replies View Related

Get Record With Latest DateTime

Oct 1, 2007



Hi,

Can anyone please let me know the query to get the latest record based on datetime cloumn?
For example, a table with three records:

Name | Application | Start Time

User A | Outlook | 10/01/2007 1:30 AM
User A | Internet Explorer | 10/02/2007 12:30 AM
User B | Internet Explorer | 9/20/2007 8:30 PM

The query gets the below:

User A | Outlook | 10/01/2007 1:30 AM
User B | Internet Explorer | 9/20/2007 8:30 PM

Thanks in advance.

View 1 Replies View Related

Query To Fetch Latest Record

Jun 29, 2012

Table has details like below:

NAME UPATE-TIMESTAMP
==== ===============
mary time1
mary time2
mary time3
tom time1
tom time2
tom time3
tom time4
richard time1
richard time2

Output Expected:

NAME UPATE-TIMESTAMP
==== ===============
mary time3
tom time4
richard time2

In summary, the requirement is to fetch the latest upated record for each unique NAME.

View 1 Replies View Related

Latest Record For Each Reference Field

Feb 19, 2014

I have a table that contains a Ref field and a TransactionDate field. For each Ref field there are mutliple Transactions. I am trying to put together a query that selects the Latest Transaction date for each Ref field in the table.

I tried:

SELECT *
FROM tabMediaTransaction
WHERE (TransactionDate =
(SELECT MAX(TransactionDate) AS Expr1
FROM tabMediaTransaction AS tabMediaTransaction_1))

But this only returns 1 record for 1 Ref field. I need 1 record for each Ref field

View 7 Replies View Related

Unable To Get Latest Date Record?

Apr 10, 2014

i want to get latest updated date on each transid and only for status =approved .

-------------------
out put would be
------------
idtransid date status
31013/1/2014 approved
61031/2/2014 approved
table
-----------

idtransid date status
11011/1/2014 approved
21012/1/2014 close
31013/1/2014 approved
41021/2/2014 approved
51022/2/2014 close
61031/2/2014 approved

View 2 Replies View Related

T-SQL (SS2K8) :: Selecting Latest Record With Info

Aug 27, 2014

I have created the following SQL snippet that is a very simple mock-up illustrating the problem (I hope!) that I am facing:

-- create table
if object_id('tempdb..#tmpdelnotes') is not null
drop table #tmpdelnotes

create table #tmpdelnotes(
DelNote int identity (1,1) ,
DelDate date not null,
Item int not null,
Customer int not null)

[code]...

What I need to retrieve is a unique list of item numbers with information about the latest (DelDate) delivery note. The "Clumsy workaround" works, but is not very pretty when doing multiple table joins. Is it really necessary to use a derived table for this kind of query? Window functions can only exist in the SELECT and ORDER BY clauses, which is understandable since the calculations take place (I would guess) after the aggregations in the HAVING clause.

View 2 Replies View Related

Transact SQL :: Retrieve Latest Record Of Serial Number With Multiple Entries

Sep 30, 2015

I work for an organization that repairs serialized devices. Each time a device is repaired it's serial number is recorded in a database table along with the date it was repaired along with other information about the device. There are multiple cases where a unit has been repaired more than once.

I am trying to write a query that will return the serial only once and that record will be the record of the latest repair date. To sum it up,

Return a list of serials where if a serial exists more than once in the table, return only the instance of the serial record(s) with the max(created_dt). The end result will be a list of distinct serial numbers.

Here is my Query. The problem I believe is in my sub-query but I am not sure how to structure it.

SELECT
S.Id
, RMA
, PinSerial
, L4Serial
, L4Model

[Code] ....

View 3 Replies View Related

JOIN Efficiency Using Multiple ON Conditions Versus WHERE Conditions

Jan 10, 2008

My question is fairly simple. When I join between two tables, I always use the ON syntax. For example:


SELECT

*
FROM

Users

JOIN UserRoles

ON (Users.UserRoleId = UserRoles.UserRoleId)


No problems there. However, if I then decide to further filter the selection based on some trait of the UserRole, I have two options: I can add the condition as a WHERE statement, or I can add the condition within the ON block.

--Version 1:

SELECT

*
FROM

Users

JOIN UserRoles

ON (Users.UserRoleId = UserRoles.UserRoleId)
WHERE

UserRoles.Active = 'TRUE'


-- Version 2

SELECT

*
FROM

Users

JOIN UserRoles

ON (Users.UserRoleId = UserRoles.UserRoleId

AND UserRoles.Active = 'TRUE')


So, the question is, which is faster/better, if either? The Query Analyzer shows the two queries have the exact same execution plan, which makes sense, since they're both joining the same tables. However, I'm wondering if adding the condition in the ON statement results in fewer rows the JOIN statement initially needs to join up, thus reducing the overall initial size of the results table before the WHERE conditions are applied.

So is there a difference, performance wise? I imagine that if Users had a thousand records, and UserRoles had 10 records, then the JOIN would create a cartesian product of the two tables, resulting in 10,000 records in the table before the WHERE conditions are applied. However, if only three of the UserRoles is set to Active, would that mean that the resulting table, before applying WHERE conditions, would only contain 3000 records?

Thanks for whatever information you can provide.

View 7 Replies View Related

Conditions, Expressions

Aug 7, 2007

I have a table
CREATE TABLE [dbo].[CmnLanguage]( [Id] [char](2) NOT NULL CONSTRAINT PkCmnLanguage_Id PRIMARY KEY, [EnglishName] [varchar](26) NOT NULL, [NativeName] [nvarchar](26) NOT NULL, [DirectionType] [smallint] NOT NULL, [IsVisible] [bit] NOT NULL, [CreatedDateTime] [datetime] NOT NULL DEFAULT GETDATE(), [ModifiedDateTime] [datetime] NULL)
We will use these 3 queries
select * from CmnLanguage where IsVisible = 0select * from CmnLanguage where IsVisible = 1select * from CmnLanguage
I want to make a method which handles these queries.
But at the back end on Stored Procedures
We have to write 3 queries
Which I don't want to do.
I want to minimize the queries and conditions
and want to just write one for these 3
Can any one do it?

View 2 Replies View Related

HELP...."OR" Conditions In SQL Statements

May 31, 2000

Folks,

I'm having some real problems using the OR condition in a very simple SQL statement and could use your help or insight on where the problem lies, or perhaps a workaround.

I have a large flat table in a SQL 7 database with 10 million + records called "HISTORY". I have not installed either service pack 1 or 2. I'm attempting to run a query that references the following four fields which are all non-clustered keys:

EQUIPMENT_NO TEXT 12
CHASSIS_IN TEXT 12
CHASSIS TEXT 12
SVC_DATE_TIME SMALLDATETIME

Here's the SQL statement:

SELECT * FROM HISTORY WHERE (HISTORY.EQUIPMENT_NO = 'XYZ123' OR HISTORY.CHASSIS = 'XYZ123' OR HISTORY.CHASSIS_IN = 'XYZ123') AND SVC_DATE_TIME >= '01/15/00 00:00:00 AM' AND SVC_DATE_TIME <= '02/28/00 23:59:59 PM'
ORDER BY EQUIPMENT_NO

This query takes 11 min. 5 sec. inder the Query Analyzer and ultimately returns the 31 desired records.

If you remove the SVC_DATE_TIME criteria, about 350 records are returned in a matter of seconds. I've also tried variations on the date syntax such as '01/15/00', etc. with no change in the amount of time to execute.

Other queries such as a simple AND condition combining EQUIPMENT_NO and SVC_DATE_TIME are snappy.

Are there known problems/bugs with "OR" conditions in queries that anyone is aware of, particularly with parentheses; am I composing this query incorrectly? Is there some alternate syntax that would work as expected? I can't see where the query shouldn't execute quickly as expected, particularly with all indexed fields involved. I'm stumped! Lend me your expertise. Thanks much.

Clark R. Farabaugh, Jr.
Financial Systems Analyst
VIT
Norfolk, VA

View 8 Replies View Related

Regarding Aggregate Conditions ..

Nov 29, 2007

Hai frns small help needed.


I have a table called sample and i have the following requirement. i.e i need sum(credit) group by ssn no.

One special condition is as follows:


For each distinct ssn if "flag" has the same CX value,then out of all the records with the same CX value, the highest "credit" value is added to the sum for that "ssn" and the rest are ignored.
If while adding "credit" to the sum and if "credit" value is equal to zero then "sum" value is used for summing else "credit" value is used.
Can any one help me out in trying this logic. I have tried but i could'nt able embed the conditions inbetween the Sql statetment.

Here is the query is used

select * from sample

idssncreditflagsem
11010C90
21014C93
31014.5C92
41013.5C11
51024.2C33
61030C12


select ssn,flag,sum(case credit when 0 then sem else credit end) as sum from sam2
group by ssn,flag


ssn flag sum_val
101C13.5
103C12.0
102C34.2
101C98.5

The above output is wrong one.


Expected output

101 4.5+3.5=8.0
102 4.2
103 2.0


Any help would be appreciated

Regards,

View 5 Replies View Related

Where Clause With Conditions

Mar 19, 2012

Code:
Drop table #table
Drop table #table_with_groupid
-- Prepare test data
CREATE TABLE #table
([Admissions_key] bigint NOT NULL PRIMARY KEY,
MRN nvarchar(10) NOT NULL,

[Code] ....

How can I compare dates with conditions. I only want to Mark C where the difference between adm_datetime and prevsep_datetime is <= 1 otherwise E as well

where datediff(MINUTE,tg.adm_datetime,tg.pre_sep_date)< =1 ??

is it correct ? where should I put this to implement correctly ?

View 1 Replies View Related

Case When Conditions

Oct 14, 2014

I have the following:

MAX(Vernum) AS test,
MAX(Case WHEN A2.AttrID = 2 AND A2.DefID = 10057945 THEN A2.ValStr END) AS TYPE_DOCUMENT

This works not perfect. In many cases I have more then one row and my query takes the max value of column Valstr. Thats is not exactly what I want. I'd like to have the value of Column Valstr of the row where column Vernum has the maximum value.

I've tried many things like:

MAX(Vernum) AS test,
MAX(Case WHEN A2.AttrID = 2 AND A2.DefID = 10057945 AND A2.Vernum=test THEN A2.ValStr END) AS TYPE_DOCUMENT

OR

MAX(Vernum) AS test,
MAX(Case WHEN A2.AttrID = 2 AND A2.DefID = 10057945 AND A2.Vernum=MAX(A2.Vernum) THEN A2.ValStr END) AS TYPE_DOCUMENT

View 1 Replies View Related

Multiple AND Conditions

Sep 12, 2006

Hi All.

Is there a way to have multiple AND conditions on the same field in a database.

EXAMPLE

SELECT * FROM tbl
WHERE field1 = 1 AND field1 = 2 AND field1 = 5

Thanks

View 9 Replies View Related

Can We Put 2 Conditions In Inner Join

Oct 24, 2006

pls:
1/ can we do it this way:
inner join Table2 ON table1.fld1=table2.fld21 AND table1.fld12=table2.fld22
2/also:
what s the difference between join , iner join and left join
Thanks .

View 4 Replies View Related

Procedure Has Many If Else Conditions

Jul 16, 2007

ALTER PROC usp_t_insup_cpa1

@Idint,
@SupervisorIdint,
@BookmarkerIdint,
@PreparerIdint,
@FirmNovarchar(20),
@FirmNamevarchar(30),
@FirstNamevarchar(20),
@MiddleNamevarchar(20),
@LastNamevarchar(20),
@TaxYearvarchar(20),
@TaxSoftwarevarchar(20),
@HomePhonevarchar(20),
@WorkPhonevarchar(20),
@Faxvarchar(20),
@PrimaryEmailvarchar(30),
@SecondaryEmailvarchar(30),
@CountryIdint,
@Statevarchar(20),
@Zipcodevarchar(20),
@Statusint,
@OperatorChar(1) = '',
@RESULTINT OUTPUT
-------------------------
AS

IF @Operator = 'I'
BEGIN

IF NOT EXISTS(SELECT * FROM CPA WHERE PrimaryEmail=@PrimaryEmail or PrimaryEmail=@SecondaryEmail or SecondaryEmail=@PrimaryEmail or SecondaryEmail=@SecondaryEmail )
BEGIN
--select * from o_login
Begin transaction InsCPA

INSERT INTO CPA(SupervisorId,BookmarkerId,PreparerId,FirmNo,FirmName,FirstName,MiddleName,LastName,TaxYear,TaxSoftware,HomePhone,WorkPhone,Fax,PrimaryEmail,SecondaryEmail,CountryId,State,Zipcode,Status)
VALUES(@SupervisorId,@BookmarkerId,@PreparerId,@FirmNo,@FirmName,@FirstName,@MiddleName,@LastName,@TaxYear,@TaxSoftware,@HomePhone,@WorkPhone,@Fax,@PrimaryEmail,@SecondaryEmail,@CountryId,@State,@Zipcode,@Status)

--Error handling
IF @@ERROR <> 0
BEGIN
-- Returns 0 to the calling program to indicate failure.
ROLLBACK TRAN InsCPA
SET @RESULT = 0
END
ELSE
BEGIN
--DECLARE @ID1 INTEGER
-- Returns 1 to the calling program to indicate success.
COMMIT TRAN InsCPA
SET @RESULT = 1
END
END
ELSE
BEGIN
-- Return 2 to the calling program to indicate record already exists.
set @RESULT = 2
END
END

ELSE IF(@Operator='U')
BEGIN
declare @pemail as varchar(30)
declare @semail as varchar(30)
declare @firm as varchar(20)
select @pemail=PrimaryEmail,@semail=SecondaryEmail,@firm=FirmNo from CPA WHERE Id = @Id
--select * from CPA

if(@pemail=@PrimaryEmail) or(@semail=@PrimaryEmail)--or((@semail=@SecondaryEmail)and (@semail=@PrimaryEmail)))
begin

print 'prim1'
if(@semail=@SecondaryEmail)or (@pemail=@SecondaryEmail)
begin
print 'sec1'
if(@firm=@FirmNo)
begin
print'firm'
BEGIN TRANSACTION UpdateCPA
UPDATE CPA
SET SupervisorId=@SupervisorId,
BookmarkerId=@BookmarkerId,
PreparerId=@PreparerId,
FirmNo=@FirmNo,
FirmName=@FirmName,
FirstName=@FirstName,
MiddleName=@MiddleName,
LastName=@LastName,
TaxYear=@TaxYear,
TaxSoftware=@TaxSoftware,
HomePhone=@HomePhone,
WorkPhone=@WorkPhone,
Fax=@Fax,
PrimaryEmail=@PrimaryEmail,
SecondaryEmail=@SecondaryEmail,
CountryId=@CountryId,
State=@State,
Zipcode=@Zipcode,
Status=@Status
WHERE Id = @Id
UPDATE EMPLOYEE
SET FirmNo=@FirmNo
WHERE FirmNo=@firm


IF @@ERROR <> 0
BEGIN
-- Returns 0 to the calling program to indicate failure.
ROLLBACK TRAN UpdateCPA
SET @RESULT = 0

END
ELSE
BEGIN
-- Returns 1 to the calling program to indicate success.
print'1'
COMMIT TRAN UpdateCPA
SET @RESULT = 1

END
END
else
begin
IF NOT EXISTS(SELECT * FROM CPA WHERE FirmNo=@FirmNo)
BEGIN
print'fd'
BEGIN TRANSACTION UpdateCPA
UPDATE CPA
SET SupervisorId=@SupervisorId,
BookmarkerId=@BookmarkerId,
PreparerId=@PreparerId,
FirmNo=@FirmNo,
FirmName=@FirmName,
FirstName=@FirstName,
MiddleName=@MiddleName,
LastName=@LastName,
TaxYear=@TaxYear,
TaxSoftware=@TaxSoftware,
HomePhone=@HomePhone,
WorkPhone=@WorkPhone,
Fax=@Fax,
PrimaryEmail=@PrimaryEmail,
SecondaryEmail=@SecondaryEmail,
CountryId=@CountryId,
State=@State,
Zipcode=@Zipcode,
Status=@Status
WHERE Id = @Id
UPDATE EMPLOYEE
SET FirmNo=@FirmNo
WHERE FirmNo=@firm
IF @@ERROR <> 0
BEGIN
-- Returns 0 to the calling program to indicate failure.
ROLLBACK TRAN UpdateCPA
SET @RESULT = 0

END
ELSE
BEGIN
-- Returns 1 to the calling program to indicate success.
print'1'
COMMIT TRAN UpdateCPA
SET @RESULT = 1

END
end
ELSE
BEGIN
-- Returns 1 to the calling program to indicate success.
print'4'
--COMMIT TRAN UpdateCPA
SET @RESULT = 4

END
end
end
else
begin
IF NOT EXISTS(SELECT * FROM CPA WHERE PrimaryEmail=@SecondaryEmail or SecondaryEmail=@SecondaryEmail)
BEGIN
if(@firm=@FirmNo)
begin
BEGIN TRANSACTION UpdateCPA
UPDATE CPA
SET SupervisorId=@SupervisorId,
BookmarkerId=@BookmarkerId,
PreparerId=@PreparerId,
FirmNo=@FirmNo,
FirmName=@FirmName,
FirstName=@FirstName,
MiddleName=@MiddleName,
LastName=@LastName,
TaxYear=@TaxYear,
TaxSoftware=@TaxSoftware,
HomePhone=@HomePhone,
WorkPhone=@WorkPhone,
Fax=@Fax,
PrimaryEmail=@PrimaryEmail,
SecondaryEmail=@SecondaryEmail,
CountryId=@CountryId,
State=@State,
Zipcode=@Zipcode,
Status=@Status
WHERE Id = @Id
UPDATE EMPLOYEE
SET FirmNo=@FirmNo
WHERE FirmNo=@firm

IF @@ERROR <> 0
BEGIN
-- Returns 0 to the calling program to indicate failure.
ROLLBACK TRAN UpdateCPA
SET @RESULT = 0

END
ELSE
BEGIN
-- Returns 1 to the calling program to indicate success.
print'1'
COMMIT TRAN UpdateCPA
SET @RESULT = 1

END
END
else
begin
IF NOT EXISTS(SELECT * FROM CPA WHERE FirmNo=@FirmNo)
BEGIN
BEGIN TRANSACTION UpdateCPA
UPDATE CPA
SET SupervisorId=@SupervisorId,
BookmarkerId=@BookmarkerId,
PreparerId=@PreparerId,
FirmNo=@FirmNo,
FirmName=@FirmName,
FirstName=@FirstName,
MiddleName=@MiddleName,
LastName=@LastName,
TaxYear=@TaxYear,
TaxSoftware=@TaxSoftware,
HomePhone=@HomePhone,
WorkPhone=@WorkPhone,
Fax=@Fax,
PrimaryEmail=@PrimaryEmail,
SecondaryEmail=@SecondaryEmail,
CountryId=@CountryId,
State=@State,
Zipcode=@Zipcode,
Status=@Status
WHERE Id = @Id
UPDATE EMPLOYEE
SET FirmNo=@FirmNo
WHERE FirmNo=@firm
IF @@ERROR <> 0
BEGIN
-- Returns 0 to the calling program to indicate failure.
ROLLBACK TRAN UpdateCPA
SET @RESULT = 0

END
ELSE
BEGIN
-- Returns 1 to the calling program to indicate success.
print'1'
COMMIT TRAN UpdateCPA
SET @RESULT = 1

END
end
ELSE
BEGIN
-- Returns 1 to the calling program to indicate success.
print'44'
--COMMIT TRAN UpdateCPA
SET @RESULT = 4

END
end
/*
--select * from o_login
Begin transaction InsCPA

UPDATE CPA
SET SupervisorId=@SupervisorId,
BookmarkerId=@BookmarkerId,
PreparerId=@PreparerId,
FirmNo=@FirmNo,
FirmName=@FirmName,
FirstName=@FirstName,
MiddleName=@MiddleName,
LastName=@LastName,
TaxYear=@TaxYear,
TaxSoftware=@TaxSoftware,
HomePhone=@HomePhone,
WorkPhone=@WorkPhone,
Fax=@Fax,
PrimaryEmail=@PrimaryEmail,
SecondaryEmail=@SecondaryEmail,
CountryId=@CountryId,
State=@State,
Zipcode=@Zipcode,
Status=@Status
WHERE Id = @Id
--Error handling
IF @@ERROR <> 0
BEGIN
-- Returns 0 to the calling program to indicate failure.
ROLLBACK TRAN InsCPA
SET @RESULT = 0
END
ELSE
BEGIN
--DECLARE @ID1 INTEGER
-- Returns 1 to the calling program to indicate success.
print'11'
COMMIT TRAN InsCPA
SET @RESULT = 1
END*/
END
ELSE
BEGIN
print 'sec same'
-- Return 2 to the calling program to indicate record already exists.
set @RESULT = 3
END
end
end
else
begin
IF NOT EXISTS(SELECT * FROM CPA WHERE PrimaryEmail=@PrimaryEmail or SecondaryEmail=@PrimaryEmail)
BEGIN
/*--select * from o_login
Begin transaction InsCPA

UPDATE CPA
SET SupervisorId=@SupervisorId,
BookmarkerId=@BookmarkerId,
PreparerId=@PreparerId,
FirmNo=@FirmNo,
FirmName=@FirmName,
FirstName=@FirstName,
MiddleName=@MiddleName,
LastName=@LastName,
TaxYear=@TaxYear,
TaxSoftware=@TaxSoftware,
HomePhone=@HomePhone,
WorkPhone=@WorkPhone,
Fax=@Fax,
PrimaryEmail=@PrimaryEmail,
SecondaryEmail=@SecondaryEmail,
CountryId=@CountryId,
State=@State,
Zipcode=@Zipcode,
Status=@Status
WHERE Id = @Id
--Error handling
IF @@ERROR <> 0
BEGIN
-- Returns 0 to the calling program to indicate failure.
ROLLBACK TRAN InsCPA
SET @RESULT = 0
END
ELSE
BEGIN
--DECLARE @ID1 INTEGER
-- Returns 1 to the calling program to indicate success.
print'111'
COMMIT TRAN InsCPA
SET @RESULT = 1
END*/
if(@firm=@FirmNo)
begin
BEGIN TRANSACTION UpdateCPA
UPDATE CPA
SET SupervisorId=@SupervisorId,
BookmarkerId=@BookmarkerId,
PreparerId=@PreparerId,
FirmNo=@FirmNo,
FirmName=@FirmName,
FirstName=@FirstName,
MiddleName=@MiddleName,
LastName=@LastName,
TaxYear=@TaxYear,
TaxSoftware=@TaxSoftware,
HomePhone=@HomePhone,
WorkPhone=@WorkPhone,
Fax=@Fax,
PrimaryEmail=@PrimaryEmail,
SecondaryEmail=@SecondaryEmail,
CountryId=@CountryId,
State=@State,
Zipcode=@Zipcode,
Status=@Status
WHERE Id = @Id

UPDATE EMPLOYEE
SET FirmNo=@FirmNo
WHERE FirmNo=@firm

IF @@ERROR <> 0
BEGIN
-- Returns 0 to the calling program to indicate failure.
ROLLBACK TRAN UpdateCPA
SET @RESULT = 0

END
ELSE
BEGIN
-- Returns 1 to the calling program to indicate success.
print'1'
COMMIT TRAN UpdateCPA
SET @RESULT = 1

END
END
else
begin
IF NOT EXISTS(SELECT * FROM CPA WHERE FirmNo=@FirmNo)
BEGIN
BEGIN TRANSACTION UpdateCPA
UPDATE CPA
SET SupervisorId=@SupervisorId,
BookmarkerId=@BookmarkerId,
PreparerId=@PreparerId,
FirmNo=@FirmNo,
FirmName=@FirmName,
FirstName=@FirstName,
MiddleName=@MiddleName,
LastName=@LastName,
TaxYear=@TaxYear,
TaxSoftware=@TaxSoftware,
HomePhone=@HomePhone,
WorkPhone=@WorkPhone,
Fax=@Fax,
PrimaryEmail=@PrimaryEmail,
SecondaryEmail=@SecondaryEmail,
CountryId=@CountryId,
State=@State,
Zipcode=@Zipcode,
Status=@Status
WHERE Id = @Id

UPDATE EMPLOYEE
SET FirmNo=@FirmNo
WHERE FirmNo=@firm
IF @@ERROR <> 0
BEGIN
-- Returns 0 to the calling program to indicate failure.
ROLLBACK TRAN UpdateCPA
SET @RESULT = 0

END
ELSE
BEGIN
-- Returns 1 to the calling program to indicate success.
print'1'
COMMIT TRAN UpdateCPA
SET @RESULT = 1

END
end
ELSE
BEGIN
-- Returns 1 to the calling program to indicate success.
print'2'
--COMMIT TRAN UpdateCPA
SET @RESULT = 2

END
end
END
ELSE
BEGIN
print 'prim same'
-- Return 2 to the calling program to indicate record already exists.
set @RESULT = 2
END
end
end

Above procedure has many if else conditions
Is there any way to write
procs other than this process

Malathi Rao

View 1 Replies View Related

Playing About With The AND And OR Conditions

Jul 26, 2007

Hi folks, basically I'm looking for this sort of structure

WHERE (caseA AND caseB) OR (caseC AND caseD) OR (CaseA AND caseD)

but I can't seem to be able to group the AND conditions together any time I try put brackets in SQL Server Enterprise manager removes them on me,

any help would be great,

thanks

View 1 Replies View Related

Where Conditions, Encryption

May 5, 2007

1. Are stored procedures WITH ENCRYPTION slower than the ones withoutencryption?2. Should i put most restrictive conditions first or last in WHERE? Inwhich order does MSSQL execute conditions? Or MSSQL determents whatwould be best and does not bother with the way i sorted conditions?for example:SELECT *FROM [users]WHERE[user_id] = 1 AND[baned] = 0Is "[user_id] = 1" or "[baned] = 0" going to be executed first?

View 2 Replies View Related

Count If Conditions Met

Jul 20, 2005

I am trying to do a summary SQL query. I have 3 fields. If one filed isnull and the other is not null, I want to count how many records thereare. I also want to count the opposite way then count both fields ifthey are both not null. Can I do this within the same query? Helpappreciated.Thanks,Steve*** Sent via Developersdex http://www.developersdex.com ***Don't just participate in USENET...get rewarded for it!

View 2 Replies View Related

Not All Where Conditions Required

Mar 17, 2008

I am trying to do a select with the closest match.



Code Snippet


CREATE TABLE [#Person]

(

[PersonId] tinyint IDENTITY(1,1) NOT NULL

,[Height] char(2) NOT NULL

,[Weight] char(3) NOT NULL

,[Age] varchar(3) NOT NULL

,[HairColor] varchar(7) NOT NULL

,[EyeColor] varchar(7) NOT NULL

);


INSERT INTO [#Person] ([Height],[Weight],[Age],[HairColor],[EyeColor])

VALUES ('71', '150', '23', 'Brown', 'Blue');

INSERT INTO [#Person] ([Height],[Weight],[Age],[HairColor],[EyeColor])

VALUES ('70', '190', '22', 'Blonde', 'Brown');

INSERT INTO [#Person] ([Height],[Weight],[Age],[HairColor],[EyeColor])

VALUES ('69', '140', '20', 'Black', 'Brown');

INSERT INTO [#Person] ([Height],[Weight],[Age],[HairColor],[EyeColor])

VALUES ('65', '150', '21', 'Brown', 'Green');


SELECT * FROM [#Person]

WHERE

([Height] > '66' AND [Height] < '72')

AND ([Weight] > '150' OR [Weight] < '180')

AND ([Age] > '20' OR [Age] < '25')

AND ([HairColor] IN ('Brown', 'Blonde', 'Black'))

AND ([EyeColor] IN ('Blue', 'Brown', 'Green'));


DROP TABLE [#Person];




This simple example works great. But what if I wanted everybody who met 4 of the 5 conditions? I tried to think of a counting solutions, and then order by the count but I could quite get there. Any help would be greatly appreciated. My live data is actual sales records with 20 some odd record types. I am hoping any solutions I find will scale well.

View 8 Replies View Related

How Do I Implement If Then Else Conditions

May 28, 2007

Hi All,



I'm very new to Integration Services. Self Learned this ETL tool based on my prior ETL tool knowledge.



Can you tell me how do write IF Then Else conditions for every column in my source and redirect to a single output?





Hey correction here ... i'm using flat file as my source





Thanks in Advance,

Suresh N

View 7 Replies View Related

Conditions With Trigger

Dec 5, 2007

Hi,

I want to create a trigger that that is invoked when a certain condition is fulfilled. For instance:

I have 2 tables, PS and US with similar table structure. PS table contains a column called 'status'. Whenever the status column is updated from 2 to either 23, 24, 25 or 26, a new row, which contains the same data within the updated row of PS, will be inserted into the table US.

Can this be done? Thanks in advance.

View 4 Replies View Related

Commiting A Trigger Under Certain Conditions

Sep 18, 2007

 I have a sitemapcache that caches nodes with a sqlcachedependency. Everything is working fine but one thing. Every time you visit a forum, the viewcount is changed, therefore raising the trigger and dropping my cache object. How do i make it so that the trigger is only fired if I update the Title or Description field?  ALTER TRIGGER [dbo].[sp_Forums_Topics_AspNet_SqlCacheNotification_Trigger] ON [dbo].[sp_Forums_Topics] FOR INSERT, UPDATE,DELETE AS BEGINSET NOCOUNT ONEXEC dbo.AspNet_SqlCacheUpdateChangeIdStoredProcedure N'sp_Forums_Topics'
END  

View 1 Replies View Related

[SQL] Alias Not Working In Conditions

May 28, 2008

Hi,I have some SQL statements like that SELECT ID, getData(ID) as Alias
FROM table
WHERE Alias = value

GO

SELECT ID, count(something) as amount
FROM table
GROUP BY amount  Why does the alias not work?? In "order by" clause it does

View 2 Replies View Related

Using IF ElSE Like Conditions In An Select Statement

Mar 17, 2006

Hi All,
This is my problem. I need the out put of a sql select statement to be "true" or "false" depending on the actual columns value is positive or negative. Does any one how to do this.
Thanks in advance,
-VJ

View 1 Replies View Related

Select Max Value From Prior Where Conditions

Apr 7, 2006

I can't figure this out for the life of me. Wanted to know if it's possible to select certain date conditions in a query, then later reference those conditions and to only select the max of them.

I need to do this dynamically as I do not know what the max value is. I've provided an example below:

Select var1
From table1
where
(
(Date1 = '11/30/2005')
OR
(Date1 = '12/31/2005')
)
and Date1 = (Max of previously selected values e.g. '12/31/2005')

What I can't figure out is how to dynamically retrieve the max of 11/31/2005 and 12/31/2005. Any ideas are greatly appreciated.

View 1 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved