Если вы ограничиваетесь только этими значениями, то создание простого представления и привязка к это может быть самым простым способом. Например:
CREATE VIEW view_for_gridview_binding AS
SELECT DISTINCT
eight.[date] as '8'
, two.[date] as '2'
, six.[date] as '6'
, four.[date] as '4'
FROM table_for_gridview_question
LEFT JOIN table_for_gridview_question AS eight ON (eight.[type] = 8)
LEFT JOIN table_for_gridview_question AS two ON (two.[type] = 2)
LEFT JOIN table_for_gridview_question AS six ON (six.[type] = 6)
LEFT JOIN table_for_gridview_question AS four ON (four.[type] = 4)
GO
Вот тест:
CREATE TABLE table_for_gridview_question (
Id int NOT NULL,
[type] int NOT NULL,
[date] date NOT NULL
)
GO
INSERT INTO table_for_gridview_question
SELECT 2, 8, '1997-09-22'
UNION
SELECT 1, 6, '1998-09-22'
UNION
SELECT 3, 4, '1999-09-22'
GO
CREATE VIEW view_for_gridview_binding AS
SELECT DISTINCT
eight.[date] as '8'
, two.[date] as '2'
, six.[date] as '6'
, four.[date] as '4'
FROM table_for_gridview_question
LEFT JOIN table_for_gridview_question AS eight ON (eight.[type] = 8)
LEFT JOIN table_for_gridview_question AS two ON (two.[type] = 2)
LEFT JOIN table_for_gridview_question AS six ON (six.[type] = 6)
LEFT JOIN table_for_gridview_question AS four ON (four.[type] = 4)
GO
SELECT * FROM view_for_gridview_binding
DROP VIEW view_for_gridview_binding
DROP TABLE table_for_gridview_question
Результаты SELECT
:
8 2 6 4
---------- ---------- ---------- ----------
1997-09-22 NULL 1998-09-22 1999-09-22
Если, однако, ваши type
могут быть чем угодно, вы можете подумать о чтении на PIVOT
и UNPIVOT
, как описано в части Dynamic Pivot Version части ответа здесь .