sql select query

Spunny 326 Reputation points
2024-02-06T15:03:38.7633333+00:00
Hi,

We get data from vendor and import into table like this

TxID

BCode

Ecode

SType

SCode

Desc

12345

SR

SR

MF

Abc

This is one type

12346

SD

SD

MF

Bcd

One more type

12347

CC

SD

OF

Efg

New type

12348

SR

SR

MF

Xyz

 

 

I get multiple rows of data like this. I need to send data to front end application as follows.

--Setup table variable to hold Datamart results

       DECLARE @Types TABLE

       (

             TxID smallint not null,

             BCode varchar(5),

             ECode varchar(5),

             SType varchar(5),         

             SCode Varchar(10),

             [Desc] varchar(50) null

       )

 

       INSERT INTO @Types (TXID, BCode, ECode, SType, SCode, [Desc])

       SELECT 12345, 'SR', 'SR', 'MF', 'Abc', 'This is one type'

       Union

       SELECT 12345, 'SD', 'SD', 'MF', 'Bcd', 'This is one more type'

       union

       SELECT 12345, 'CC', 'SD', 'OF', 'Efg', 'New Type'

       union

       SELECT 12345, 'SR', 'SR', 'MS', 'xyz', 'This is one different type'

 

 

SELECT

       CASE

                    WHEN BCode IN ('SR', 'SD') and SType = 'MF' THEN 'Fee'

                    WHEN BCode = 'CC' AND ECode = 'SD' AND SType = 'OF' THEN 'Fee'

                    ELSE BCode

    END AS Type

From @Types

This result data gets showed up on front end page. On that page, user has type dropdown with different types from that application which has ‘Fee’ as value.

When user selects ‘Fee’ from dropdown, grid should refresh ‘Fee’ related rows. But we do not have ‘Fee’ type in back end. Above table has those.

How do I make filter work when Fee is sent as type to back end stored procedure which gets data from above table.

I have to implements similar logic for other types.

 
SQL Server Transact-SQL
SQL Server Transact-SQL
SQL Server: A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.Transact-SQL: A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
62 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Erland Sommarskog 105K Reputation points MVP
    2024-02-08T22:13:51.51+00:00

    You need to repeat the logic:

    WHERE (BCode IN ('SR', 'SD') and SType = 'MF' AND @dropdown = 'Fee') OR
          (Other conditions AND @dropdown = 'somethingelse')
    

    A slicker solution is to create a table that is keyed by these BCode, SType and the other columns with Dropdown as the result value. But there may be some effort in maintaining this table. (I don't know anything about your business.)

    0 comments No comments