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 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
I have a dataset which is of following nature. I would like to replace the names in "MAKE" column if the column contains "PQR" per unique country.
country MAKE
1 USA PQR
2 USA ABC
3 UK PQR
4 UK DEF
5 JPN DEF
6 JPN LMN
Desired Output:
country MAKE
1 USA PQR
2 USA PQR
3 UK PQR
4 UK PQR
5 JPN OTHERS
5 JPN OTHERS
One option is conditional aggregation with analytic functions:
SELECT
country,
CASE WHEN SUM(CASE WHEN MAKE = 'PQR' THEN 1 ELSE 0 END) OVER (PARTITION BY country) > 0
THEN 'PQR' ELSE 'OTHERS' END AS MAKE
FROM yourTable
ORDER BY
country;
Demo
I have two table employee and Native leave I want count employee’s Department wise and count employee in native
Employee Table:-
Empno Name Depart Addre Age Exisisting_No
-----------------------------------------
1 Abc Acc Kkk 20 1
2 Efg Hr Hhh 22 2
3 ijk Acc Yyy 21 3
4 Mno Hr Zzz 20 4
Native Leave Table:-
Exisisting_No Name From To Reason
---------------------------------
2 Efg 30/01/14 04/02/14 personal
Want Output Like:-
Depart EmpNo NL
---------------
Hr 2 1
Acc 2 0
So please any one can help me and sorry for my English
SELECT e.Dept
, count(Empno)
, count(NL.Exisisting_No)
FROM Employee e LEFT JOIN NativeLeave NL ON e.Exisisting_No = NL.Exisisting_No
GROUP BY e.Dept
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