我有一张如下表:
ID FID MDate Active
--------------------------
1 1 2009-05-25 1
1 2 2009-05-25 1
1 1 2010-02-04 0
1 3 2010-02-04 1
1 1 2009-04-01 0
1 1 2009-03-01 1
如何为每个日期获得有效FId
?
我在尝试如下:
SELECT DISTINCT
ID, MDate,
STUFF ((SELECT DISTINCT ',' + CAST(FID AS VARCHAR)
FROM
(SELECT ID, MDate, FID
FROM Table1
WHERE IsActive = 1) t
WHERE t.MDate = a.MDate
FOR XML PATH('')), 1, 1, '') colb
FROM
(SELECT ID, MDate, FID
FROM Table1
WHERE IsActive = 1) a
不知何故,它给出了部分结果。在上面查询FID 3的结果的最后一行被选中时,应该有两个FID 2, 3
,因为FID 2自2009-05-25
到目前为止是有效的。
我想要输出如下:
ID MDate FID
1 2009-03-01 1
1 2009-05-25 1,2
1 2010-02-04 2,3
如何在SQL中获取此内容?
答案 0 :(得分:2)
您需要正确指定字段名称,并且不需要该数量的子查询:
ID ModifyDate colb
----------- ---------- ----------
1 2009-03-01 1
1 2009-05-25 1,2
1 2010-02-04 1,3
(3 row(s) affected)
结果:
class Node{
protected int data;
protected Node link;
public Node(){
link=null;
data =0;
}
public Node(int d,Node n){
data=d;
link=n;
}
public void setlink(Node n){
link=n;
}
public void setData(int d){
data=d;
}
public Node getlink(){
return link;
}
public int getData(){
return data;
}
}
答案 1 :(得分:1)
我会先使用CTE过滤掉活动行
; WITH
CTE AS
(
SELECT *
FROM Table1
WHERE IsActive = 1
)
SELECT ID, ModifyDate,
colb = STUFF ( (
SELECT ',' + CAST(FID AS VARCHAR(10))
FROM CTE x
WHERE x.ModifyDate = c.ModifyDate
FOR XML PATH ('')
) , 1, 1, '')
FROM CTE c
GROUP BY ID, ModifyDate
答案 2 :(得分:0)
jquery_and_ui
示例输出如下:
{% javascripts
'@jquery_and_ui'
'@AppBundle/Resources/public/js/*' %}
<script src="{{ asset_url }}"></script>
{% endjavascripts %}
我认为这就是你想要的。