我正在删除一些COUNT为0或大于1的记录。查询工作正常,并在SQL结果中看起来很棒。但是我循环遍历结果并创建一个Header并根据Country对它们进行分组并列出该国家/地区的用户名称。
这是我的SQL:
SELECT * FROM (
SELECT u.ContactName
,cu.[User ID]
,c.Name
,c.ID
,cu.[Foreign Table]
,count(*) OVER (PARTITION BY c.ID) AS user_in_this_country
FROM dbo.Country AS c
INNER JOIN dbo.CountryUser AS cu ON c.ID = cu.[Foreign ID]
INNER JOIN dbo.UserColder AS u ON cu.[User ID] = u.ID
WHERE EXISTS (
SELECT *
FROM CountryUser AS cu2
WHERE cu2.[Foreign ID] = cu.[Foreign ID]
AND cu2.[User ID] <> cu.[User ID]
AND cu2.[Foreign Table] = 'Country')
)
t
WHERE user_in_this_country > 1 or user_in_this_country = 0
ORDER BY Name ASC
这将删除看起来像的数据:
Justin United States 2
Bob United States 2
然后循环并添加标题/组的方法是:
string lastValue = "";
protected string AddGroupingHeader(string Value)
{
//Get the data field value of interest for this row
string currentValue = Value;
//Specify name to display if dataFieldValue is a database NULL
if (currentValue.Length == 0)
{
currentValue = "";
}
//See if there's been a change in value
if (lastValue != currentValue)
{
//There's been a change! Record the change and emit the header
lastValue = currentValue;
return "<div style='float: left; font-size: 16px; margin: 10px 0px 0px 10px;'>" + currentValue + "</div>";
}
else
{
return "";
}
}
在aspx中,我只是传递了这样的值:
<%# AddGroupingHeader(Eval("Name").ToString())%>
现在,如果我有几条记录,它就可以运行并显示如下:
United States
- Justin
- Bob
China
- Frank
- Ted
但是这就是问题所在。如果我有:
United States Justin
United Bob
然后它没有放头。它这样做:
Justin
Bob
对不起,很长的帖子,但是有人看到任何会导致这种情况的事情,我已经被困了2天了。 :(
谢谢!
答案 0 :(得分:0)
删除float
样式。方法AddGroupingHeader
可以简化。您可以将其称为仅传递值,该值用作标题。
string _lastHeader;
protected string AddGroupingHeader(string header)
{
if (header != _lastHeader) {
_lastHeader = header;
return "<div style='font-size: 16px; margin: 10px 0px 0px 10px;'>" + header + "</div>";
}
return "";
}