基于单个公共列加入三个表并选择最大日期值

时间:2015-11-10 12:35:46

标签: sql sql-server join

我有像这样定义的SQL Server表:

create table users
(
    user_id int NOT NULL IDENTITY(1,1),
    name varchar(200),
    username varchar(200),
    password varchar(50),
    encrypted_password varchar(100),
    email varchar(30),
    phone_no int,
    address varchar(200),
    PRIMARY KEY(user_id)    
)

create table electricity
(
    table_name VARCHAR(50),
    electricity_bill_id int NOT NULL IDENTITY(1,1),
    billing_month_year varchar(50),
    units_consumed int,
    user_id int,
    account_number varchar(100),
    amount varchar(100),
    due_date  varchar(100),  
    PRIMARY KEY(electricity_bill_id),
    FOREIGN KEY(user_id) REFERENCES users(user_id)     
);

insert into electricity (table_name, billing_month_year, units_consumed, user_id, account_number, amount, due_date) 
values ('Electricity','2015-05-22',13,1,'acc01',2500,'2015-06-22');

create table water
(
    table_name VARCHAR(10),
    water_bill_id int NOT NULL IDENTITY(1,1),
    billing_month_year_date varchar(50),
    units_consumed int,
    user_id int,
    account_number varchar(100),
    amount varchar(100),
    due_date varchar(100), 
    PRIMARY KEY(water_bill_id),
    FOREIGN KEY(user_id) REFERENCES users(user_id)    
)

insert into water(table_name, billing_month_year_date, units_consumed, user_id, account_number, amount, due_date)
values ('Water','2015-04-22',17,1,'acc01',2500,'2015-05-14');

create table telephone
(
    table_name VARCHAR(10),
    telecom_bill_id int NOT NULL IDENTITY(1,1),
    bill_Period varchar(100),
    user_id int,
    account_number varchar(100),
    amount varchar(100),
    issued_date varchar(50),
    due_date varchar(50), 
    PRIMARY KEY(telecom_bill_id),
    FOREIGN KEY(user_id) REFERENCES users(user_id)    
)

insert into telephone(table_name, bill_Period, user_id, account_number, amount, issued_date, due_date)
values ('Telephone', '2015-04-22', 1, 'acc01', 2500, '2015-05-14', '2015-05-18');

问题是我想根据我需要传递给查询的electricity加入watertelephoneuser id表。

我还要从三个表中选择 maximum due_date 。但是,我有一些问题。

请有人帮助我。

2 个答案:

答案 0 :(得分:1)

一种方法是使用Array.Reverse(parsedlinks); for (int i = parsedlinks.Count - 1; (i >= 0 && !bgw.CancellationPending); i--)

outer apply

对于所有三个表上的select u.*, e.*, w.*, t.* from users u outer apply (select top 1 e.* from electricity e where e.user_id = u.user_id order by e.due_date desc ) e outer apply (select top 1 w.* from water w where w.user_id = u.user_id order by w.due_date desc ) w outer appy (select top 1 t.* from telephone t where t.user_id = u.user_id order by t.due_date desc ) t where u.user_id = @user_id; 索引,查询应该也具有非常好的性能。

答案 1 :(得分:1)

如果我理解你的问题,你想做这样的事情:

SELECT MAX(e.due_date) AS ElectricityDueDate, MAX(w.due_date) AS WaterDueDate, MAX(t.due_date) AS TelephoneDueDate 
FROM user u, electricity e, water w, telephone t
WHERE u.user_id=@user_id AND
e.user_id=u.user_id AND
w.user_id=u.user_id AND
t.user_id=u.user_id AND
GROUP BY u.user_id

但是,要这样做,您应该以这种方式将due_date类型更改为DATE 你可以计算最大值。