Count multiple occurrences across 3 tables and create/insert into 4th table - sql-server

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

Related

Are these tables in 3rd Normal form?

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

Conditional union and edit rows as columns

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

Sql Server Order By combine two values as one

I have following data table
id - name - city
- 1 George Seattle
- 2 Sam Boston
- 3 John Los Angeles
- 4 Amy Milwaukee
- 5 Eric Chicago
- 6 Nick New York
here i want to consider chicago and Seattle as same city
so the order i want is
id - name - city
- 1 George Seattle
- 5 Eric Chicago
- 6 Nick New York
- 4 Amy Milwaukee
- 3 John Los Angeles
- 2 Sam Boston
so what condition i should use when using order by city Desc
You can use a case when then condition in the order by clause
select id,name, city from
yourtable
order by
(case when city=N'Chicago' then N'Seattle' else city end) desc
I'd suggest that if you need to do this often then consider using a mapping table

Find T-SQL to Transpose A Table By Chunks

I am looking for the cleanest, shortest, simplest, and most efficient way to do this using T-SQL. I hope that a single way is all of those things, but I realize that it may not be.
I have tried (unsuccessfully) using PIVOT, but what I think I need is to PIVOT by chunks AND without aggregating
I queried a large table to obtain this result:
(the result is ordered by year by the Number descending. the Number is the Number of occurrences of people born with that Name in the given year)
Name Number Year
John 9655 1880
William 9532 1880
James 5927 1880
Charles 5348 1880
George 5126 1880
John 8769 1881
William 8524 1881
Charles 5442 1881
George 4664 1881
James 4636 1881
John 9557 1882
James 9298 1882
William 5892 1882
George 5193 1882
Charles 5092 1882
I want to turn the above result into this:
1880 1881 1882
John John John
William William James
James Charles William
Charles George George
George James Charles
You can use PIVOT to get the result, the only thing that you will need to do is add a row_number() that is generated by partitioning the data by year ordered by the number. This unique sequence then allows you to return multiple rows for each year when you apply the aggregate function. Also since you want to convert string data you need to use either the max or min aggregate function:
select [1880], [1881], [1882]
from
(
select name, year,
row_number() over(partition by year
order by number desc) seq
from yourtable
) d
pivot
(
max(name)
for year in ([1880], [1881], [1882])
) p;
See SQL Fiddle with Demo. Returns:
| 1880 | 1881 | 1882 |
|---------|---------|---------|
| John | John | John |
| William | William | James |
| James | Charles | William |
| Charles | George | George |
| George | James | Charles |

Trigger to auto-increment ID based on certain criteria in TSQL

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

Resources