Solved

Integrate two queries

Posted on 2016-09-15
13
75 Views
Last Modified: 2016-09-16
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
0
Comment
Question by:PeterBaileyUk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
  • 2
  • +1
13 Comments
 
LVL 66

Expert Comment

by:Jim Horn
ID: 41800397
This question does not contain enough details and simple English to be actionable.  Please revisit the question and ask again..
0
 
LVL 41

Expert Comment

by:Sharath
ID: 41800442
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

0
 
LVL 29

Expert Comment

by:Pawan Kumar
ID: 41800856
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

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:PeterBaileyUk
ID: 41800995
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'.
0
 
LVL 41

Expert Comment

by:Sharath
ID: 41801041
Which post are you referring?
0
 
LVL 29

Expert Comment

by:Pawan Kumar
ID: 41801045
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

0
 

Author Comment

by:PeterBaileyUk
ID: 41801074
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'.
0
 
LVL 29

Expert Comment

by:Pawan Kumar
ID: 41801076
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

0
 

Author Comment

by:PeterBaileyUk
ID: 41801085
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
0
 

Author Comment

by:PeterBaileyUk
ID: 41801089
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

0
 
LVL 29

Accepted Solution

by:
Pawan Kumar earned 500 total points
ID: 41801096
What about

select TW.ClientCode,tw.word,
			   
                ((
						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('')
					) ) 
                AS cusr
from Tblwords TW
where clientcode='54553002'
group by TW.ClientCode,tw.word

Open in new window

0
 

Author Comment

by:PeterBaileyUk
ID: 41801109
That got it can the end comma the redundant one be removed ? i will post another question for that if you want
0
 
LVL 29

Expert Comment

by:Pawan Kumar
ID: 41801113
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 !
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question