Question

i have table with 100 columns name. I want to get 1st record of each item with specific conditions

I have column name Model_Name( car model name about 7 to 8 different models)

Credit_flag(Y/N)

Private_Offer(Y/N)

Cash_back(some value or blank)

Bonus_cash(some value or blank)

Now If i want to get 1st record for each model

where Credit_flag is equal to yes

where private_offer is equal to yes

where cash_back is equal to some value not blank

where Bonus_cash is equal to some value not blank

Cars

No correct solution

OTHER TIPS

You cannot just get the first record, you have to specify how. For eg, if you try to fetch the first record, you may get a different result the next time you run the same query, the reason being sql determines the fastest method to get the rows and implements it, if data size increases, it may use another method. To get the same result everytime, you should do an order by so it knows it has to get the first result after sorting the data.

You can use statement similar to below, too lazy to write the entire thing. But you should get the idea.

select Top 1 column1, col2,col3 from table where col1 = 'Y' AND cashBack <> '' order by col1

The response by @user1063280 assumes that you want a single row from the table. Your question read to me more like you wanted to first relevant row for each model in the table. If that's what you want, the below query should achieve your goal:

DECLARE @Cars TABLE
(
    ID int IDENTITY,
    CreditFlag bit,
    PrivateOfferFlag bit,
    ModelName varchar(3),
    CashBack numeric(19,2),
    BonusCash numeric(19,2),
    TotalSavings AS CashBack + BonusCash
);
INSERT INTO @Cars (CreditFlag, PrivateOfferFlag, ModelName, CashBack, BonusCash) VALUES (0, 1, 'AAA', 750, 750);
INSERT INTO @Cars (CreditFlag, PrivateOfferFlag, ModelName, CashBack, BonusCash) VALUES (0, 1, 'BBB', 750, 750);
INSERT INTO @Cars (CreditFlag, PrivateOfferFlag, ModelName, CashBack, BonusCash) VALUES (0, 1, 'CCC', 2500, 750);
INSERT INTO @Cars (CreditFlag, PrivateOfferFlag, ModelName, CashBack, BonusCash) VALUES (1, 1, 'DDD', 750, 750);
INSERT INTO @Cars (CreditFlag, PrivateOfferFlag, ModelName, CashBack, BonusCash) VALUES (0, 0, 'AAA', 1500, 750);
INSERT INTO @Cars (CreditFlag, PrivateOfferFlag, ModelName, CashBack, BonusCash) VALUES (0, 0, 'BBB', 2000, 750);
INSERT INTO @Cars (CreditFlag, PrivateOfferFlag, ModelName, CashBack, BonusCash) VALUES (1, 0, 'CCC', 3000, 750);
INSERT INTO @Cars (CreditFlag, PrivateOfferFlag, ModelName, CashBack, BonusCash) VALUES (0, 0, 'DDD', 3000, 750);
INSERT INTO @Cars (CreditFlag, PrivateOfferFlag, ModelName, CashBack, BonusCash) VALUES (0, 1, 'AAA', 750, 750);

SELECT
    *
FROM        @Cars c1
WHERE       ID = 
            (
                SELECT
                    TOP 1 ID
                FROM        @Cars c2
                WHERE       c2.ModelName = c1.ModelName
                AND         c2.CreditFlag = 1
                AND         c2.PrivateOfferFlag = 1
                AND         c2.CashBack > 0
                AND         c2.BonusCash > 0
                ORDER BY    ID
            );

You are able to provide any filtration you'd like in the sub-query, and you can specify any ordering you'd like. I added an ID column and just picked the first such column. You could be more sophisticated and sort descending by TotalSavings, for example.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top