我很难解释,所以我会尽力让我的例子进行大部分讨论。假设我有一个像这样的表:
dbo.ExampleTable
===================================
ID Year Data1 Data2
====== ======== ========= =========
12 2016 FOO BAR
13 2016 FOO MAN
14 2016 SAW BAR
20 2017 FOO BAR
21 2017 FOO MAN
27 2017 SAW BAR
29 2017 CHU CAR
44 9999 FOO BAR
48 9999 FOO MAN
51 9999 SAW BAR
52 9999 CHU CAR
一些注意事项:
我想从该数据中创建一个表,如下所示:
ID_9999 ID_2016 ID_2017
=========== =========== ===========
44 12 20
48 13 21
51 14 27
52 NULL 29
因此,从本质上讲,对于Year = 9999的Data1和Data2的每个唯一配对,我想创建一个包含Year = 9999的配对ID以及Year = 2016和也是Year = 2017。此外,如果2016或2017不包含该数据配对,则我希望它们的值为NULL。
这是我到目前为止得到的查询:
SELECT tbl9999.ID ID_9999,
tbl2016.ID ID_2016,
tbl2017.ID ID_2017
FROM dbo.ExampleTable tbl9999
LEFT JOIN dbo.ExampleTable tbl2016
ON tbl9999.Data1 = tbl2016.Data1
AND tbl9999.Data2 = tbl2016.Data2
LEFT JOIN dbo.ExampleTable tbl2017
ON tbl9999.Data1 = tbl2017.Data1
AND tbl9999.Data2 = tbl2017.Data2
WHERE tbl9999.Year=9999
AND tbl2016.Year=2016
AND tbl2017.Year=2017
这似乎可以正常工作,但是它会生成一个这样的表:
ID_9999 ID_2016 ID_2017
=========== =========== ===========
44 12 20
48 13 21
51 14 27
*请注意,在上面的示例中,该行缺少具有null值的行。有什么方法可以更改查询以包含该空值,以便在示例中使用它?
如果我缺少任何信息或需要澄清的任何信息,请告诉我。预先感谢!
编辑: 我能够自己找到答案!这是我用来达到预期效果的代码:
SELECT [9999] [ID_9999],
[2016] [ID_2016],
[2017] [ID_2017]
FROM dbo.ExampleTable
PIVOT (MAX([ID]) FOR [Year] IN ([2016],[2017],[9999])) [x]
ORDER BY ID_9999
答案 0 :(得分:3)
您可以通过多种方式执行此操作。条件聚合似乎很简单:
Rt-Click on the textbox containing the "138" and select "Textbox Properties".
Click "Action" on the left.
Select the "Go to report" option on the right.
Click on the "Browse" button and browse to the report you want to run.
Click on the "Add" button to add the parameter you want to send to the report. When you do so, the "Name" column will contain a list of the parameters the report has in it. Select the appropriate one that you want to pass the "138" into. The under the "Value" column, select the field containing the "138".
Click "OK".