How to count "Out of" for each row ?

  • Good Morning All,

    I was looking for advice/help on how to count the number of values that exist in a row based on the values from an array of numbers. Basically the the array of numbers I want to look for are in row 1 of table [test 1] and I want to search for them and count the "out of" in table [test 2]. Excuse me for not using the easiest way to convey my question below. I guess in short I have 10 numbers and like to find how many of those numbers exist in each row. short example:

    Table Name: test1

    Columns: m1 (int), m2 (int), m3 (int) >>> etc

    Array/Row1: 1 2 3 4 5 6 7 8 9 10

    ------

    Table Name: test2

    Columns: n1 (int), n2 (int), n3 (int), n4 (int), n5 (int)

    Row 1: 3, 8, 18, 77, 12

    Row 2: 1, 4, 5, 7,18, 21

    Row 3: 2, 4, 6, 8, 10

    Answer: 2 out of 5

    Answer: 4 out of 5

    Answer: 5 out of 5

  • If the number of fields is known, then try this:

    with hlp as (select cast(f1 as varchar(2))+','+cast(f2 as varchar(2))+','+cast(f3 as varchar(2))+','+cast(f4 as varchar(2))+

    ','+cast(f5 as varchar(2))+','+cast(f6 as varchar(2))+','+cast(f7 as varchar(2))+','+cast(f8 as varchar(2))+

    ','+cast(f9 as varchar(2))+','+cast(f10 as varchar(2))+',' as string

    from test1)

    select CAST(LEN(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(string,CAST(f1 as varchar(5))+',','-'),CAST(f2 as varchar(5))+',','-'),CAST(f3 as varchar(5))+',','-'),CAST(f4 as varchar(5))+',','-'),CAST(f5 as varchar(5))+',','-'))-

    LEN(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(string,CAST(f1 as varchar(5))+',','-'),CAST(f2 as varchar(5))+',','-'),CAST(f3 as varchar(5))+',','-'),CAST(f4 as varchar(5))+',','-'),CAST(f5 as varchar(5))+',','-'),'-','')) as varchar(50))+' out of 5'

    from hlp, test2

  • Hi and welcome to the forums. In order to help we will need a few things:

    1. Sample DDL in the form of CREATE TABLE statements

    2. Sample data in the form of INSERT INTO statements

    3. Expected results based on the sample data

    Please take a few minutes and read the first article in my signature for best practices when posting questions.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

Viewing 3 posts - 1 through 2 (of 2 total)

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