Avatar of PeterBaileyUk
PeterBaileyUk
 asked on

Integrate two queries

This sql server query gets the words that I want to pass to the lower query am struggling to do that

select  TW.ClientCode,tw.word - sequence column here from next bit of sql


from Tblwords TW
where clientcode='54553002'
group by TW.ClientCode, TW.ClientCode,tw.word

Open in new window


declare
	@string varchar(1000)='OCEAN'  this would become tw.word



select 
	(
		select 
			cast(ascii(substring(@string,N,1)) as varchar(5))+','
		from 
			[dictionary].[dbo].[fnTally]() nr
		where
			nr.N<=len(@string) 
		FOR XML PATH('')
	) as ascii_seq

Open in new window


example output from first query
Microsoft SQL Server

Avatar of undefined
Last Comment
Pawan Kumar

8/22/2022 - Mon
Jim Horn

This question does not contain enough details and simple English to be actionable.  Please revisit the question and ask again..
Sharath S

try this.
declare @string varchar(1000)='OCEAN'  this would become tw.word

select @string = tw.word 
from Tblwords TW
where clientcode='54553002'
group by TW.ClientCode,tw.word

select 
    (
        select 
            cast(ascii(substring(@string,N,1)) as varchar(5))+','
        from 
            [dictionary].[dbo].[fnTally]() nr
        where
            nr.N<=len(@string) 
        FOR XML PATH('')
    ) as ascii_seq

Open in new window

Pawan Kumar

Try this

select tw.word, scr
from Tblwords TW
CROSS APPLY
(
SELECT 
scr FROM
    (
        select 
            cast(ascii(substring(tw.word,N,1)) as varchar(5))+',' scr
        from 
            [dictionary].[dbo].[fnTally]() nr
        where
            nr.N<=len(tw.word) 
        FOR XML PATH('')
    )ascii_seq
)t
where clientcode='54553002'
group by TW.ClientCode,scr,tw.word

Open in new window

I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
PeterBaileyUk

ASKER
This id ID: 41799397 an excellent solution gives me the ascii summed sequence per group of words.

I need to do the same now but at letter level.

my first query gives
<TW ClientCode="54553002" word="OCEAN" />
<TW ClientCode="54553002" word="RACE" />
<TW ClientCode="54553002" word="T6" />
<TW ClientCode="54553002" word="XC90" />

i was hoping I could extract the sequence of the ascii letters in a new column:
OCEAN sequence = 79, 67, 69, 65, 78
RACE =82, 65, 67, 69

ID: 41800856 had error

Msg 8155, Level 16, State 2, Line 17
No column name was specified for column 1 of 'ascii_seq'.
Msg 207, Level 16, State 1, Line 7
Invalid column name 'scr'.
Sharath S

Which post are you referring?
Pawan Kumar

Try this

select tw.word, scr
from Tblwords TW
CROSS APPLY
(

        select 
            cast(ascii(substring(tw.word,N,1)) as varchar(5))+',' scr
        from 
            [dictionary].[dbo].[fnTally]() nr
        where
            nr.N<=len(tw.word) 
        FOR XML PATH('')
    
)y
where clientcode='54553002'
group by TW.ClientCode,scr,tw.word

Open in new window

⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
PeterBaileyUk

ASKER
its saying:
Msg 8155, Level 16, State 2, Line 16
No column name was specified for column 1 of 'y'.
Msg 207, Level 16, State 1, Line 17
Invalid column name 'scr'.
Msg 207, Level 16, State 1, Line 2
Invalid column name 'scr'.
Pawan Kumar

Try this

select TW.ClientCode,tw.word,
			  STUFF 
                ((
						select 
							cast(ascii(substring(tw.word,N,1)) as varchar(5))+',' 
						from 
							[dictionary].[dbo].[fnTally]() nr
						where
							nr.N<=len(tw.word) 
						FOR XML PATH('')
					) ,1,2,'') 
                AS cusr
from Tblwords TW
where clientcode='54553002'
group by TW.ClientCode,tw.word

Open in new window

PeterBaileyUk

ASKER
Its working structurally but dishing out the wrong answer
it gave out
it should be
ROWs
1
2 race=82,65, 67,69
3 t6 =84,54
4 XC90= 88, 67, 57, 48

Its missed the first line
Your help has saved me hundreds of hours of internet surfing.
fblack61
PeterBaileyUk

ASKER
if i do this it works, that proves out the function itself, apart from the , at the end
 
use Dictionary

select 
							cast(ascii(substring('OCEAN',N,1)) as varchar(5))+',' 
						from 
							[dictionary].[dbo].[fnTally]() nr
						where
							nr.N<=len('OCEAN') 
						FOR XML PATH('')

Open in new window

ASKER CERTIFIED SOLUTION
Pawan Kumar

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
PeterBaileyUk

ASKER
That got it can the end comma the redundant one be removed ? i will post another question for that if you want
Pawan Kumar

Okies fine. So Are you done with this question. If yes could you please one answer as accepted solution. and raise another question. ?

Great! Thanks !
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.