简体   繁体   中英

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

@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

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

You can query something like this, replace your_table by your actual set of data.

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). In your case this function could help because you have two differents cases to handle.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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