Click here to Skip to main content
15,896,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
CustomerID   StartWeight   EndWeight    Charges    AddWeight    AddCharges
1              0.00          0.50        50          0            0
1              0.60          1.00        100         1            70
2              0.00          0.50        60          0            0
2              0.60          1.00        110         1            80
3              0.00          1.00        120         1            100



select * from table where CustomerID=1
this show the result of first two line.

What I have tried:

select * from table where CustomerAccountID=1 and EndWeight=(select MAX(EndWeight) from table)


This show null value while i want to show the second row
Posted
Updated 30-Jan-18 5:32am
Comments
David_Wimbley 30-Jan-18 11:28am    
EDIT: I missed your customer Id condition

1 solution

Not entirely sure what the problem is with your query/why you are getting incorrect data, i mocked up your data set and the query provides expected results.

SQL
DECLARE @CpMaxValueRow TABLE (
	CustomerId INT NULL,
	EndWeight FLOAT NULL
);

INSERT INTO @CpMaxValueRow
        ( CustomerId, EndWeight )
VALUES  ( 1, -- CustomerId - int
          0.5  -- EndWeight - float
          )
INSERT INTO @CpMaxValueRow
        ( CustomerId, EndWeight )
VALUES  ( 1, -- CustomerId - int
          1  -- EndWeight - float
          )
INSERT INTO @CpMaxValueRow
        ( CustomerId, EndWeight )
VALUES  ( 2, -- CustomerId - int
          .5  -- EndWeight - float
          )
INSERT INTO @CpMaxValueRow
        ( CustomerId, EndWeight )
VALUES  ( 2, -- CustomerId - int
          1  -- EndWeight - float
          )
INSERT INTO @CpMaxValueRow
        ( CustomerId, EndWeight )
VALUES  ( 2, -- CustomerId - int
          1  -- EndWeight - float
          )

		  SELECT * FROM @CpMaxValueRow AS A WHERE CustomerId = 1 AND EndWeight = (SELECT MAX(B.EndWeight) FROM @CpMaxValueRow AS B)
 
Share this answer
 
Comments
Member 10028394 30-Jan-18 12:53pm    
your query work fine but when i apply this query according to our table it not work
David_Wimbley 30-Jan-18 13:41pm    
Given i dont have access to your data or your schema, then you need to figure out what is different between this querys results and what you get against your normal DB.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900