简体   繁体   中英

How to get data from mssql with similar description?

So I have table like:

id | description          | code     | unit_value   | short
1  | awesome product DG   | CODEB14  | null         | BT
2  | awesome product      | CODE14   | 5005         | NOBT
3  | product less awe BGO | CODEB15  | null         | BT
4  | product less awe     | CODE15   | 5006         | NOBT

And I need display 'unit_value ' for items with DG, BGO but need to base on items without DG, BGO. So item 'awesome product DG' have the same 'unit_value' as 'awesome product' item. But I can not assign value for items where 'short = BT'.

So what I have so far are two queries which some how I want to merge:

select value_i_need from my_table where short= 'BT'

select value_i_need from my_table where short!= 'BT' and description like '%awesome product%'

And I have no idea how to merge those two queries? Some suggestion would be very helpful.

You need to join two copies of the table together

CREATE TABLE #mytable
(
    id INT,
    description VARCHAR(50),
    code VARCHAR(10),
    unitvalue INT NULL,
    short VARCHAR(10)
)

INSERT INTO #mytable
(
    id,
    description,
    code,
    unitvalue,
    short
)
VALUES
(1, 'awesome product DG'   , 'CODEB14'  , null ,'BT'),
(2, 'awesome product'      , 'CODE14'   , 5005 ,'NOBT'),
(3, 'product less awe BGO' , 'CODEB15'  , null ,'BT'),
(4, 'product less awe'     , 'CODE15'   , 5006 ,'NOBT');

SELECT a.description, a.code, b.description, b.code, b.short, b.unitvalue, a.description, a.short
FROM #myTable a
LEFT OUTER JOIN #myTable b ON a.description LIKE b.description + '%'
    AND b.short != 'BT'
WHERE a.short = 'BT'

However, this is making a lot of assumptions ie that there is only one such item for each row, that you don't have products with similar names where the "like" would confuse the two. Also joining on a "like" is going to be slow if there is any kind of volume. So although this works on this trivial example data, I'm not sure I recommend you actually use it.

It feels to me like this data should not all be in the same table. You should have one table with the BT entries, and another with the NOBT entries and a foreign key to the BT table. Maybe? Its not totally clear what the data represents, but might point you in the right direction.

Do you just want or ?

select value_i_need
from my_table
where short = 'BT' or
      (short <> 'BT' and description like '%here is the name%')

You could use code like below. You need to use table aliases (T1 and T2 below) to help match the columns. This is a correlated sub-query assuming there is exactly one match. I'll point out that LIKE will cause problems with multiple rows returned if you have more than one product that matches.

select (
         select unit_value
           from my_table T2
          WHERE T2.description like '%awesome product%'
            AND T2.short = 'NOBT'                
       )
  from my_table T1
 where T1.short= 'BT' AND T1.description LIKE '%awesome product%'

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