简体   繁体   English

SQL区分具有条件的列

[英]SQL Distinct a column with conditions

I'm working on a query where I need to count distinct CarId row when the column LocationId is not null and get all CarId if its null or 0 but the query that I tried distincts all the CarId even if its null 我正在进行一个查询,我需要在列LocationId不为null时计算不同的CarId行,如果它的null0则获取所有CarId ,但是我尝试的查询将所有CarId即使它为null

@LocId int

    Select Count(distinct a.CarId) from VehicleDetails a 
        inner join VehicleDocuments b on a.DocId=b.DocId
        left join VehicleShipmentDetails dpg on dpg.VehicleShipmentId= b.VehicleShipmentId
        where b.LogicalDelete=0 and a.LogicalDelete=0 
        and (dpg.LocationId= @LocId or dpg.LocationId= 0 or dpg.LocationId is null)



|  ID  |    CarId       |    LocationId   |  DateCreated  |
|------+----------------+-----------------+---------------|
|   1  |       1        |        5        |   02/03/2019  |
|   2  |       2        |      null       |   01/14/2019  |
|   3  |       2        |        0        |   02/03/2019  |
|   4  |       2        |        5        |   12/30/2018  |
|   5  |       4        |        3        |   01/10/2019  |
|   6  |       3        |        5        |   02/14/2019  |
|   7  |       2        |        5        |   03/13/2019  |

Desired output: 期望的输出:

|  ID  |    CarId       |    LocationId   |  DateCreated  |
+------+----------------+-----------------+---------------+
|   1  |       1        |        5        |   02/03/2019  |
|   2  |       2        |      null       |   01/14/2019  |
|   3  |       2        |        0        |   02/03/2019  |
|   4  |       2        |        5        |   03/13/2019  |
|   5  |       4        |        3        |   01/10/2019  |
|   6  |       3        |        5        |   02/14/2019  |

Current Output 电流输出

|  ID  |    CarId       |    LocationId   |  DateCreated  |
+------+----------------+-----------------+---------------+
|   1  |       1        |        5        |   02/03/2019  |
|   2  |       2        |        5        |   01/14/2019  |
|   3  |       4        |        3        |   01/10/2019  |
|   4  |       3        |        5        |   02/14/2019  |

Im getting a count of 4 but i needed to have 6 as the Count 我得到4的计数,但我需要有6作为伯爵

EDIT: My goal is to remove the row to Distinct CarId if the value of the LocationId is Null or 0 but on my Current code, It distincts all CarId that is null , 0 and equals to @LocId 编辑:如果LocationId值为Null0我的目标是删除行到Distinct CarId ,但在我的当前代码中,它区分所有为null 0 ,等于@LocId

You can query something like this, replace your_table by your actual set of data. 您可以查询类似的内容,用您的实际数据集替换your_table

SELECT ID, CardId, LocationId, DateCreated
FROM your_table as T
WHERE NOT EXISTS (SELECT *
                  FROM your_table as T1
                  WHERE T.ID > T1.ID AND T.CarID = T1.CarID)

In SQL, you can use the statement CASE to manage conditions (just like the "if then else" in other programming languages). 在SQL中,您可以使用语句CASE来管理条件(就像其他编程语言中的“if then else”)。 In your case this function could help because you have two differents cases to handle. 在您的情况下,此功能可以提供帮助,因为您有两个不同的情况需要处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM