SQL 交叉引用相同的查询 table
SQL query to cross-reference the same table
我有一个下面的查询,我在其中使用左外连接来交叉引用相同的 table。基本上我想交叉引用 Min_Size_Code 字段来反映 Min_Actual_size 和 Max_Size_Code 字段来反映 Max_Actual_Size。但是,我没有得到我正在寻找的正确答案。
以下是我根据 SQL 查询
的回答
理想的答案应该如下
下面是我的代码
select A.week,A.style,A.Color, A.location, A.Actual_size, A.size_code, A.Min_size_code, A.Max_size_code,
min(B.Actual_size) over(partition by A.week,A.style,A.Color,A.location) Min_Actual_Size,
max(B.Actual_size) over(partition by A.week,A.style,A.Color,A.location) Max_Actual_Size
from
(
select week, style, Color, location, Actual_size, size_code,
min(size_code)over(partition by week,style,Color,location) min_size_code,
max(size_code)over(partition by week,style,Color,location) max_size_code
from TestSizeTable where Style = 'AB123' and color = 'WY4567'
and week = '202002' and location in ( '111')
) A
Left outer Join
TestSizeTable B
on ( A.Style = B.Style and A.Color = B.Color and A.Week = B.Week
and A.Location = B.Location)
where (B.size_code = A.min_size_code or B.size_code = A.max_size_code)
任何帮助都会很棒!!
我认为问题出在列actual_size的数据类型上。我怀疑它是 varcahr
并且在那种情况下 14 小于 2.
您的代码工作正常here。
请检查 varchar
列 (actual_size) 的更新解决方案 HERE.
这可能不是理想的方式,但我从下面修改后的代码中得到了答案。
Select A.week,A.style,A.Color, A.location, A.Actual_size, A.size_code, A.Min_size_code, A.Max_size_code,
Min_Actual_Size,Max_Actual_Size
from
(
select A.week,A.style,A.Color, A.location, A.Actual_size, A.size_code, A.Min_size_code, A.Max_size_code,
max(B.Actual_size) over(partition by A.week,A.style,A.Color,A.location) Max_Actual_Size,
(
select A.week,A.style,A.Color, A.location, A.Actual_size, A.size_code, A.Min_size_code, A.Max_size_code,
min(B.Actual_size) over(partition by A.week,A.style,A.Color,A.location) Min_Actual_Size,
from
(
select week, style, Color, location, Actual_size, size_code,
min(size_code)over(partition by week,style,Color,location) min_size_code,
max(size_code)over(partition by week,style,Color,location) max_size_code
from TestSizeTable where Style = 'AB123' and color = 'WY4567'
and week = '202002' and location in ( '111')
) A
Left outer Join
TestSizeTable B
on ( A.Style = B.Style and A.Color = B.Color and A.Week = B.Week
and A.Location = B.Location)
where B.size_code = A.min_size_code
) A
Left outer Join
TestSizeTable B
on ( A.Style = B.Style and A.Color = B.Color and A.Week = B.Week
and A.Location = B.Location)
where B.size_code = A.max_size_code
) A
我有一个下面的查询,我在其中使用左外连接来交叉引用相同的 table。基本上我想交叉引用 Min_Size_Code 字段来反映 Min_Actual_size 和 Max_Size_Code 字段来反映 Max_Actual_Size。但是,我没有得到我正在寻找的正确答案。
以下是我根据 SQL 查询
的回答理想的答案应该如下
下面是我的代码
select A.week,A.style,A.Color, A.location, A.Actual_size, A.size_code, A.Min_size_code, A.Max_size_code,
min(B.Actual_size) over(partition by A.week,A.style,A.Color,A.location) Min_Actual_Size,
max(B.Actual_size) over(partition by A.week,A.style,A.Color,A.location) Max_Actual_Size
from
(
select week, style, Color, location, Actual_size, size_code,
min(size_code)over(partition by week,style,Color,location) min_size_code,
max(size_code)over(partition by week,style,Color,location) max_size_code
from TestSizeTable where Style = 'AB123' and color = 'WY4567'
and week = '202002' and location in ( '111')
) A
Left outer Join
TestSizeTable B
on ( A.Style = B.Style and A.Color = B.Color and A.Week = B.Week
and A.Location = B.Location)
where (B.size_code = A.min_size_code or B.size_code = A.max_size_code)
任何帮助都会很棒!!
我认为问题出在列actual_size的数据类型上。我怀疑它是 varcahr
并且在那种情况下 14 小于 2.
您的代码工作正常here。
请检查 varchar
列 (actual_size) 的更新解决方案 HERE.
这可能不是理想的方式,但我从下面修改后的代码中得到了答案。
Select A.week,A.style,A.Color, A.location, A.Actual_size, A.size_code, A.Min_size_code, A.Max_size_code,
Min_Actual_Size,Max_Actual_Size
from
(
select A.week,A.style,A.Color, A.location, A.Actual_size, A.size_code, A.Min_size_code, A.Max_size_code,
max(B.Actual_size) over(partition by A.week,A.style,A.Color,A.location) Max_Actual_Size,
(
select A.week,A.style,A.Color, A.location, A.Actual_size, A.size_code, A.Min_size_code, A.Max_size_code,
min(B.Actual_size) over(partition by A.week,A.style,A.Color,A.location) Min_Actual_Size,
from
(
select week, style, Color, location, Actual_size, size_code,
min(size_code)over(partition by week,style,Color,location) min_size_code,
max(size_code)over(partition by week,style,Color,location) max_size_code
from TestSizeTable where Style = 'AB123' and color = 'WY4567'
and week = '202002' and location in ( '111')
) A
Left outer Join
TestSizeTable B
on ( A.Style = B.Style and A.Color = B.Color and A.Week = B.Week
and A.Location = B.Location)
where B.size_code = A.min_size_code
) A
Left outer Join
TestSizeTable B
on ( A.Style = B.Style and A.Color = B.Color and A.Week = B.Week
and A.Location = B.Location)
where B.size_code = A.max_size_code
) A