I would like to write a trigger so that when a new record comes in, if certain columns of it contains the same content as the previous record, one particular ID column for all existing entries would increase by 1 and the new entry would be 0, and if there is no existing record, the new record is automatically assigned to have ID 0 in that column.
For example, say my existing data is like this:
FirstName LastName InvoiceID Amt
-----------------------------------
John Doe 1 $5
Bill Jane 0 $6
John Doe 0 $3
Now say I want to insert John Doe with $3.5 of invoice, then the record would automatically becomes:
FirstName LastName InvoiceID Amt
-----------------------------------
John Doe 1 $5
Bill Jane 0 $6
John Doe 2 $3
John Doe 0 $3.5
Now if I need to insert someone new, say Jane Smith for $2, it would become:
FirstName LastName InvoiceID Amt
--------------------------------------
John Doe 1 $5
Bill Jane 0 $6
John Doe 2 $3
John Doe 0 $3.5
Jane Smith 0 $2
May I know how should write such trigger in T-SQL?
Thanks!
Here's what I would do, rather than having the trigger:
create table T (TID int IDENTITY(1,1) not null,FirstName varchar(10) not null,LastName varchar(10) not null,Amt decimal(18,4) not null)
go
create view V
as
select
FirstName,
LastName,
ROW_NUMBER() OVER (PARTITION BY FirstName,LastName ORDER BY TID desc) - 1 as InvoiceID,
Amt
from
T
go
And then set up your initial data:
insert into T(FirstName,LastName,Amt) values
('John','Doe',5),
('Bill','Jane',6)
go
insert into T(FirstName,LastName,Amt) values
('John','Doe',3)
And the new John Doe row:
insert into T(FirstName,LastName,Amt) values
('John','Doe',3.5)
And finally the Jane Smith row:
insert into T(FirstName,LastName,Amt) values
('Jane','Smith',2)
And then select from the view:
FirstName LastName InvoiceID Amt
---------- ---------- -------------------- ---------------------------------------
Bill Jane 0 6.0000
Jane Smith 0 2.0000
John Doe 0 3.5000
John Doe 1 3.0000
John Doe 2 5.0000
And so we have no concern that InvoiceID might be incorrect because we never store it.
select * from V
Related
users
id
first_name
last_name
email
1
John
Doe
john.doe#john.doe.com
2
Mary
Doe
mary.doe#mary.doe.com
user_details
id
user_id
ethnicity_id
education_id
location_id
1
John
1
1
1
2
Mary
4
3
2
locations
id
location
lat
long
1
London, UK
53.2321
-1.2321
2
Berlin, Germany
52.2321
-1.5345
I always be impressed on what I found so far on this site. This time it is my turn to have a question...
I have the following tables :
Appt :
ApptId
ResourceId
12345
abcde
12345
bcdef
23456
defgh
34567
cdefg
Resource:
ResourceId
ResourceType
abcde
Person
bcdef
Place
cdefg
Place
defgh
Person
Place :
PlaceId
ResourceId
PlaceName
zzzzz
bcdef
Boston
yyyyy
cdefg
Dallas
Person:
PersonId
ResourceId
PersonName
wwwww
abcde
Smith
xxxxx
defgh
Doe
and I try to have this :
ApptId
ResourceName
PlaceName
12345
Smith
Boston
23456
Doe
34567
Dallas
So for the moment I am able to have this :
ApptId
ResourceName
PlaceName
12345
Smith
12345
Boston
23456
Doe
34567
Dallas
But I failed to aggregate Resource and Place if they are about the same Appointment.
Hope this is clear
You likely want to have SELECT statement with two left outer joins, as follows:
SELECT Appt.ApptId, Person.PersonName, Place.PlaceName
FROM Appt
LEFT OUTER JOIN Person ON Appt.ResourceId = Person.ResourceId
LEFT OUTER JOIN Place ON Appt.ResourceId = Place.ResourceId
Using SQL Server, what is the best way to count multiple recurring items in 3 tables, and create a 4th table with the sums? See example:
Sample Data:
table 1 table 2 table 3
name name name value
John Doe John Doe John Doe v1
Mary Smith Mary Smith John Doe v2
John Doe John Doe Mary Smith v1
Mary Smith John Doe v3
John Doe Mary Smith v1
Mary Smith v2
John Doe v2
Expected Results:
Result table of counts
name t1 t2 t3v1 t3v2 t3v3
John Doe 3 2 1 2 1
Mary Smith 2 1 2 1 0
I'm having some issues with a SQL query. I need to order the results of a select statement by street address.
I started with:
SELECT * FROM addressTable ORDER BY streetAddress ASC
but this would return
1 bob street
10 bob street
2 bob street
To rectify this, I changed my statement to:
SELECT * FROM database ORDER BY LEN(StreetAddress), StreetAddress ASC
I thought this was ideal as it was now returning:
1 bob street
2 bob street
10 bob street
However I hadn't anticipated apartment and flat numbers, which are in the format of 1a 1b 210a 210b etc, so I found my query would return like so:
210 bob street
211 bob street
212 bob street
213 bob street
210a bob street
210b bob street
Instead of:
210 bob street
210a bob street
210b bob street
211 bob street
212 bob street
213 bob street
What is the simplest query I can use to achieve this?
Try this:
ORDER BY CAST(LEFT(StreetAddress, PATINDEX('%[^0-9]%', StreetAddress) - 1) AS INT),
StreetAddress
The idea is to extract the leading number value, cast it to INT, sort by it, then sort by the field itself. It should work provided StreetAddress values are like the ones posted.
I can't work out how to do this:
Surname Firstname EmpID ListName
--------------------------------
Smith john 123 list1
Smith john 123 list2
Jones James 124 list1
I need
Surname Firstname EmpID ListName1 ListName2
--------------------------------------------
Smith john 123 list1 List 2
Jones James 124 list1 *null*
Just add a number for the rows, with something like this:
select row_number() over (partition by Surname Firstname EmpID order by ListName) as RN, ...
And then use that row number for deciding what data to show where with something like:
select Surname Firstname EmpID,
max(case when RN = 1 then ListName end) as ListName1,
max(case when RN = 2 then ListName end) as ListName2
from (