简体   繁体   中英

SQL IF- ELSE if Statement - Or Case

I'm trying to do something with SQL and was wondering if it would be possible. I have a spreadsheet that looks like this

 Type   Qnty     description        MISCNUMB 
 A      1         One Is One           abc    
 A      1        One is bla bla        ASD
 A      2        asdasdsa              23213
 B      12       Two One               321
 B      1111                     
 C      122312   DRE                   321

In which I might have repeated values in some columns.

when I select type from the the table is there a way not to get all repeated values and only one per instance?

Can I use If- ELIF to compare columns in the table from each other and write a query?

One other thing is that, My data is so large that I can not go through data to have a case statement for each. I ultimately want to be able to look for a data like this.

IF Type =A > Show qnty with respect to it > ( once the user selects those values) >then show description with respect to selected qnty> show misc number

SELECT 
    Qnty 
FROM 
    TABLE_1 
WHERE 
    TYPE = 'A' 
    (I should technically get 1,1,2 right?)
    Pseudo code = IF Qnty= '1' FROM TABLE_1 
       then show only descriptions and MISC numb for those values, 
       else show rest) 

Second Attempt to understand it.

DECLARE @TABLE_1 TABLE (Type NVARCHAR, Qnty INT, Description NVARCHAR(100), MISCNUMB NVARCHAR(100))
DECLARE @UserChoosesOption INT

INSERT INTO @TABLE_1 
VALUES
     ('A',1,'random text', 'aaa')
    ,('A',1,'random text1', 'aaa1')
    ,('A',2,'random text2', 'aaa2')
    ,('B',12,'random text3', 'aaa3')
    ,('B',1111,'random text4', 'aaa4')
    ,('C',122312,'random text5', 'aaa5')

SELECT 
    Qnty
FROM @TABLE_1 WHERE Type = 'B'

SELECT @UserChoosesOption = 12

SELECT 
    Description, 
    MISCNUMB 
FROM @TABLE_1
WHERE Qnty = @UserChoosesOption

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