0

so i have table name report the detail like this

No(autoincrement)       Code       Stats(bool)
--------------------------------------
       1                 F01        0
       2                 F02        0
       3                 F03        0
       4                 F03        1
       5                 F03        1
       6                 F04        1
       7                 F04        0

so the scenario is if code have value for stats is true then show them all and if the code didnt have value true then only show the false so the result i want is (note : one code always have 1 record for value false)

No(autoincrement)           Code       Stats
--------------------------------------------
           1                  F01        0
           2                  F02        0
           4                  F03        1
           5                  F03        1
           6                  F04        1

because f01 and f02 didnt have value stats true then show them and for case f03 and f04 because have value true for stat show them all true and ignore the false value

so how the query for i get the result

3
  • Which dbms you are using? Commented Mar 31, 2018 at 4:18
  • Im using mysql for my dbms Commented Mar 31, 2018 at 4:37
  • Check My Solution using MySql DenseRank @InheavenA Commented Mar 31, 2018 at 5:13

2 Answers 2

1

It seems you want DENSE_RANK() function

select * from
(
    select *,
            dense_rank() over (partition by Code order by Stats desc) Seq
    from table t
) t
where Seq = 1

If, you don't want to go with subquery form, you could explore the above things via TOP with ties which available in SQL Server

select top 1 with ties [No(autoincrement)], Code, [Stats(bool)]
from table t
order by dense_rank() over (partition by Code order by [Stats(bool)] desc)

Since MySQL doesn't have analytical function. So, you could explore these thing via UNION ALL

select * 
from table t
where exists (
    select 1 from table
    where autoincrement  = t.autoincrement  and Stats = 1
) UNION ALL 
select * 
from table t
where not exists (
    select 1 from table
    where code = t.code and Stats = 1) 
ORDER BY 1
Sign up to request clarification or add additional context in comments.

3 Comments

how to use dense_rank if im using mysql for query
What is 1 from select 1 is ?
@InheavenA.. select 1 means return 1 for every matched record that satisfied the filtered criteria.
0
SELECT No,Code,Stats FROM
(SELECT No,Code,Stats,
  Case when @Code = t.Code Then
            Case when @stats != t.stats  Then
                 @DenseRank := @DenseRank + 1
            ELSE
                 @DenseRank := 1
            END
   Else
      @DenseRank := 1 
   END as DenseRank,
  @Code :=t.code as VarCode,
  @stats :=t.stats as varstatus
  FROM Table1 t join
  (Select @DenseRank := 1,@Code := '',@stats := '' )r
Order BY CODE,Stats DESC)
AS T1
WHERE
DenseRank=1
Order BY CODE,Stats DESC;

Output

No  Code    Stats
1   F01     0
2   F02     0
4   F03     1
5   F03     1
6   F04     1

Demo

http://sqlfiddle.com/#!9/f868a/3

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.