连接查询以获取所有常见和不常见的数据 [英] join query to get all common and uncommon data

查看:80
本文介绍了连接查询以获取所有常见和不常见的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好朋友......



i有两张桌子如下





  名称   Pds_1  
有效 empcode shift edatetime solid gplsolid
1 u1 < span class =code-leadattribute> B 2014-08-01 10:23:00 000 25 00000 32 00000
2 u1 B 2014-08-01 < span class =code-leadattribute> 10:25:00 。 000 36 00000 98 00000
3 u1 A 2014-08-01 10:30:00 000 < span class =code-leadattribute> 2 。 00000 65 00000
4 u1 A 2014-08-01 10:24:00 000 23 00000 654 00000
5 u1 C 2014-08-01 10:45:00 000 5 00000 4 00000
6 u1 C 2014-08-01 10:55:00 000 5 00000 4 00000


名称 Pds_2
有效 emp shift edatetime slurrydensity caustic ratio
1 u1 C 2014-08-01 10:24:00 000 5 00000 6 00000 7 00000
2 u1 C 2014-08-01 10:25:00 000 5 00000 2 00000 54 00000
3 u1 B 2014-08-01 01:34:00 000 5 00000 6 . 00000 5 00000
4 u1 A < span class =code-leadattribute> 2014-08-01 08:35:00 000 6 00000 5 。< span class =code-leadattribute> 00000 4 00000





我希望从pds_1和pds_2表中获取所有条目。我使用全外连接如下





 选择 p1.shift  as  p1,p1.gplsolid,p1.solid,
p2.shift as p2,p2.caustic,p2.ratio,p2.slurrydensity 来自 pds_1 p1 完整 < span class =code-keyword> outer join pds_2 p2
on p1.shift = p2.shift
其中 CONVERT varchar ,p1.edatetime, 103 convert varchar @ date 103 ))
CONVERT varchar ,p2.edatetime, 103 convert varchar @ date ,< span class =code-digit> 103 ))





但它给出了我不想要的重复行





PDS

Wagon铝土矿

%保湿球磨机饲料

+1英寸

最大:15%球磨机饲料

潮湿

%球轧机产品

TPH

m3 / hr球磨机产品

球磨机编号 球磨机产品

%固体球磨机产品

密度

C 4.00 5.00 C 6.00 7.00 5.00

C 4.00 5.00 C 2.00 54.00 5.00

C 4.00 5.00 C 6.00 7.00 5.00

C 4.00 5.00 C 2.00 54.00 5.00

B 32.00 25.00 B 6.00 5.00 5.00

A 65.00 2.00 A 5.00 4.00 6.00

B 98.00 36.00 B 6.00 5.00 5.00

A 654.00 23.00 A 5.00 4.00 6.00

解决方案

1)声明@date为DATE,

2)不要将日期转换为varchar!字符串不像你想象的那样比较。

3)如果你需要比较日期和日期,将两个日期转换为DATE然后使用相等的:

 其中 CAST(p1.edatetime  AS   DATE )=  @ date  
CAST(p2.edatetime AS DATE )= @ date


hello friends...

i have two table as follows


Table  name : Pds_1
valid   empcode shift   edatetime               solid       gplsolid
1       u1      B       2014-08-01 10:23:00.000 25.00000    32.00000
2       u1      B       2014-08-01 10:25:00.000 36.00000    98.00000
3       u1      A       2014-08-01 10:30:00.000 2.00000     65.00000
4       u1      A       2014-08-01 10:24:00.000 23.00000    654.00000
5       u1      C       2014-08-01 10:45:00.000 5.00000     4.00000
6       u1      C       2014-08-01 10:55:00.000 5.00000     4.00000


Table Name : Pds_2
valid   emp shift   edatetime               slurrydensity   caustic     ratio
1       u1  C       2014-08-01 10:24:00.000 5.00000         6.00000     7.00000
2       u1  C       2014-08-01 10:25:00.000 5.00000         2.00000     54.00000
3       u1  B       2014-08-01 01:34:00.000 5.00000         6.00000     5.00000
4       u1  A       2014-08-01 08:35:00.000 6.00000         5.00000     4.00000



and i want to get all entries from pds_1 and pds_2 tables. i used full outer join as follows


select p1.shift as p1,p1.gplsolid,p1.solid,
p2.shift as p2,p2.caustic,p2.ratio,p2.slurrydensity from pds_1 p1 full outer join pds_2 p2
on p1.shift = p2.shift
where CONVERT(varchar,p1.edatetime,103) in (convert(varchar,@date,103))
and CONVERT(varchar,p2.edatetime,103) in (convert(varchar,@date,103))



but it gives repeated row that i don't want


PDS
"Wagon Bauxite
% Moisture" "Ball Mill Feed
+1 inch
Max:15%" "Ball Mill Feed
Moist
%" "Ball Mill Product
TPH
m3/hr" "Ball Mill Product
Ball Mill No." "Ball Mill Product
% Solid" "Ball Mill Product
Density"
C 4.00 5.00 C 6.00 7.00 5.00
C 4.00 5.00 C 2.00 54.00 5.00
C 4.00 5.00 C 6.00 7.00 5.00
C 4.00 5.00 C 2.00 54.00 5.00
B 32.00 25.00 B 6.00 5.00 5.00
A 65.00 2.00 A 5.00 4.00 6.00
B 98.00 36.00 B 6.00 5.00 5.00
A 654.00 23.00 A 5.00 4.00 6.00

解决方案

1) declare @date as DATE,
2) do not convert dates to varchar! character strings do not compare as you might think.
3) if you need to compare date to date convert the two dates to DATE then use equal:

where CAST(p1.edatetime AS DATE) = @date
   and CAST(p2.edatetime AS DATE) = @date


这篇关于连接查询以获取所有常见和不常见的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆