左加入相同的表基础

时间:2017-06-19 09:08:27

标签: sql sql-server left-join

我试图创建下表:

JpId | JpName | JpValue | JpEndvalue JpEndValue来自同一个表的JpValue(TimeStamp + 1day),但我得到了这个结果:

表:

JpId JpName JpTimeStamp JpValue

1   MAGIC   2017-06-15  151
2   BASIC   2017-06-15  152
3   MINI    2017-06-15  153
4   SUPER   2017-06-15  154
5   MAGIC   2017-06-16  161
6   BASIC   2017-06-16  162
7   MINI    2017-06-16  163
8   SUPER   2017-06-16  164

查询

SELECT jp1.JpId
    ,jp1.JpName
    ,jp1.JpValue
    ,jp2.JpValue AS 'JpEndValue'
FROM jackpot_web_report jp1
LEFT JOIN jackpot_web_report jp2
    ON jp2.JpTimeStamp = '2017-06-16'
WHERE jp1.JpTimeStamp = '2017-06-15';

结果:

1   MAGIC   151 161
1   MAGIC   151 162
1   MAGIC   151 163
1   MAGIC   151 164
2   BASIC   152 161
2   BASIC   152 162
2   BASIC   152 163
2   BASIC   152 164
3   MINI    153 161
3   MINI    153 162
3   MINI    153 163
3   MINI    153 164
4   SUPER   154 161
4   SUPER   154 162
4   SUPER   154 163
4   SUPER   154 164

预期结果:

1   MAGIC   151 161
2   BASIC   152 162
3   MINI    153 163
4   SUPER   154 164

1 个答案:

答案 0 :(得分:2)

我似乎错过了@BindingAdapter("layout_marginBottom") public static void setMarginBottom(View view, int margin) { ViewGroup.LayoutParams params = view.getLayoutParams(); if (params instanceof ViewGroup.MarginLayoutParams) { ViewGroup.MarginLayoutParams margins = (ViewGroup.MarginLayoutParams) params; margins.bottomMargin = margin; view.requestLayout(); } } 条件:

jp2.JpName = jp1.JpName

您还可以在加入条件中添加SELECT jp1.JpId ,jp1.JpName ,jp1.JpValue ,jp2.JpValue AS 'JpEndValue' FROM jackpot_web_report jp1 LEFT JOIN jackpot_web_report jp2 ON jp2.JpTimeStamp = '2017-06-16' AND jp2.JpName = jp1.JpName WHERE jp1.JpTimeStamp = '2017-06-15';

DATEADD