I need some urgent help!

  • I am working with SQL Server and needed some help with a query

     

    When you run this "SELECT 'YES' it would print Yes in column 1 Row 1, I want to print a dynamic list, so it doesn't look at a existing table. So if criteria 1 is met it would print YES/NO/Maybe and if it meets criteria 2 then its 100/50/25, as I said it wouldn't be a existing table. it would print a new table. This would have to be done without using any create functions. Just with SELECT.

     

  • Ok... so what is "Criteria 1" and "Criteria 2" to return 1 value or another?

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • So yeah, criteria meaning a condition. So if condition one is met then it would be list 1, else list 2.

  • SELECT  ListValue,...

    FROM <table>

    WHERE ListNumber = @ListNumber ;

    So the table would be something like

    CREATE TABLE ListValue (ListNumber INT, ListValue VARCHAR(10));

    GO

    INSERT INTO ListValue VALUES(1, 'A'),(1,'B'), (2,'C'),(2,'D');

  • Is there any way to do it without inserting data? with case statements? I pretty much need it to print like the above table but on the fly.

     

    Something like this

     

    SELECT "Yes" & "No"

    Table 1

    YES

    NO

  • In SELECT statements the FROM clause and JOINs are evaluated first.  Maybe you could use the VALUES table value constructor to store (rows of) criteria, order number, and value.  Then use a WHERE clause to filter the results

    /* criteria = YES/NO (comment/uncomment) */  
    declare @criteria varchar(10)='Yes';
    --declare @criteria varchar(10)='No';

    select v.col_value
    from (values ('No', 1, '100'),
    ('No', 2, '50'),
    ('No', 3, '25'),
    ('Yes', 1, 'Yes'),
    ('Yes', 2, 'No'),
    ('Yes', 3, 'Maybe'))
    v(criteria, order_num, col_value)
    where v.criteria=@criteria
    order by v.order_num;

     

    Aus dem Paradies, das Cantor uns geschaffen, soll uns niemand vertreiben können

  • So I saw a while back that its possible with a case statement, because once this sql code is created it goes into a another software that parses basic level of sql. So I'm trying to figure it out and find that article but can't find it for the death of me.

  • mpate100 wrote:

    So I saw a while back that its possible with a case statement, because once this sql code is created it goes into a another software that parses basic level of sql. So I'm trying to figure it out and find that article but can't find it for the death of me.

    Your descriptions haven't helped me help you.  I understand what criteria does... I'm just not bagging what you're raking in your problem description.  For example, the words "Yes", "No', and "Maybe" aren't "criteria" to me.  They the results of applying criteria.

    So, with that, can you provide a specific example instead of the extremely generic 100,000 foot level descriptions that you've been using?

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Considering there's something urgent going on, why not tell us exactly what it is that you're trying to do and then we might be able to better help. All these vague hints at what's going on mean we're guessing.

    "The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
    - Theodore Roosevelt

    Author of:
    SQL Server Execution Plans
    SQL Server Query Performance Tuning

  • Grant Fritchey wrote:

    Considering there's something urgent going on, why not tell us exactly what it is that you're trying to do and then we might be able to better help. All these vague hints at what's going on mean we're guessing.

    I think the homework submission date has now past.

  • Is there a way to create this output without using insert into and declaring anything.

    Lets forget all the conditions. Lets just say that I need to print this without using tables and declaring values.

    When you run this SQL "SELECT "YES", it would print this. I need it display the image above using just the select and case statement if possible.

     

  • Yes... Steve Collins posted an example above.  You could also use multiple SELECTs with a UNION ALL.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • mpate100 wrote:

    Is there a way to create this output without using insert into and declaring anything.

    Lets forget all the conditions. Lets just say that I need to print this without using tables and declaring values.

    When you run this SQL "SELECT "YES", it would print this. I need it display the image above using just the select and case statement if possible.

     

    To make this as simple as possible:

    Of course, you need to define <some condition>.  There is also Steve's example...

    Jeffrey Williams
    “We are all faced with a series of great opportunities brilliantly disguised as impossible situations.”

    ― Charles R. Swindoll

    How to post questions to get better answers faster
    Managing Transaction Logs

  • This was removed by the editor as SPAM

Viewing 14 posts - 1 through 13 (of 13 total)

You must be logged in to reply to this topic. Login to reply