我需要进行查询,其中只有每个拥有汽车的用户的最后一行获得车牌号码。
<DataGridTextColumn x:Name="Type" Binding="{Binding TypeOfData, Mode=OneTime}" SortMemberPath="TypeOfData" IsReadOnly="true" CanUserSort="true">
<DataGridTextColumn.Header>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Label Content="Type Of Data" />
<ComboBox x:Name="comboBoxType"
Grid.Column="1"
SelectionChanged="comboBoxType_SelectionChanged">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel x:Name="itemsComboBox">
<CheckBox Name="checkBoxType"
IsChecked="False"
Content="{Binding Key}"
Unchecked="FilterChange"
Checked="FilterChange"/>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<Button Content="Check"
Grid.Column="2" />
</Grid>
</DataGridTextColumn.Header>
</DataGridTextColumn>
应该成为:
ID UserId LicensePlate HasCar
1 1 ABC123 1
2 1 ABC123 1
3 2 NULL 0
4 3 UVW789 1
5 3 UVW789 1
所以我基本上需要找到所有拥有牌照的用户并更改除最后一张之外的所有用户并制作LicensePlate ID UserId LicensePlate HasCar
1 1 NULL 1
2 1 ABC123 1
3 2 NULL 0
4 3 NULL 1
5 3 UVW789 1
答案 0 :(得分:3)
假设ID列是一个标识列,因此它可以提供排序,这样的事情可以解决这个问题:
;WITH CTE AS
(
SELECT Id,
UserId,
LicensePlate,
ROW_NUMBER() OVER(PARTITION BY UserId ORDER BY Id DESC) rn
FROM Table
WHERE HasCar = 1
)
UPDATE CTE
SET LicensePlate = NULL
WHERE rn > 1
答案 1 :(得分:0)
你可以试试这个
figure
% plot
title('Test 1')
答案 2 :(得分:0)
您可以使用表格本身的连接来执行此操作:
UPDATE car c
INNER JOIN car c2 ON c.userId = c2.userId AND c.id < c2.id AND c.HasCar = 1 AND c2.HasCar = 1
SET c.LicensePlate = NULL
条件c.id < c2.id
将避免选择最后一行
答案 3 :(得分:0)
通过使用LAG功能,您也可以实现它。
;WITH License(ID,UserId,LicensePlate,HasCar)
as
(
SELECT 1,1,'ABC123',1 UNION ALL
SELECT 2,1,'ABC123',1 UNION ALL
SELECT 3,2,NULL ,0 UNION ALL
SELECT 4,3,'UVW789',1 UNION ALL
SELECT 5,3,'UVW789',1
)
SELECT ID,UserId,LAG(LicensePlate,1,NULL) OVER(PARTITION BY UserId ORDER BY LicensePlate),HasCar FROM License