?
Solved

create query in sql server

Posted on 2016-08-10
5
Medium Priority
?
32 Views
Last Modified: 2016-08-10
I am having trouble creating an sql server query that uses a sub query.
I have a sub query :

SELECT TblWordTags.ClientCode, TblWordTags.ClientCodeWordPosition
FROM TblWordTags
GROUP BY TblWordTags.ClientCode, TblWordTags.ClientCodeWordPosition;

Open in new window


The main query (ive restricted it with the where for the moment)
SELECT TblWords.ClientCodeWordPosition, TblWords.ClientCode, TblWords.Word, QrySubQuery.ClientCode, TblWords.StrFull
FROM TblWords LEFT JOIN QrySubQuery ON (TblWords.ClientCodeWordPosition = QrySubQuery.ClientCodeWordPosition) AND (TblWords.ClientCode = QrySubQuery.ClientCode)
WHERE (((TblWords.ClientCode)="A01ND"));

Open in new window


Not sure how to port this over to sql server with the sub query despite reading one of the experts posts on access to sql server
0
Comment
Question by:PeterBaileyUk
5 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 41749980
the code should work as is, except the double quotes, for string values it should be single quotes.
apart from that, what are the issues?
0
 
LVL 53

Accepted Solution

by:
Vitor Montalvão earned 2000 total points
ID: 41750006
Add the subselect code directly after the LEFT JOIN and give it an alias:
SELECT TblWords.ClientCodeWordPosition, TblWords.ClientCode, TblWords.Word, QrySubQuery.ClientCode, TblWords.StrFull
FROM TblWords 
	LEFT JOIN (SELECT ClientCode, ClientCodeWordPosition
		FROM TblWordTags
		GROUP BY ClientCode, ClientCodeWordPosition) QrySubQuery ON (TblWords.ClientCodeWordPosition = QrySubQuery.ClientCodeWordPosition AND TblWords.ClientCode = QrySubQuery.ClientCode)
WHERE TblWords.ClientCode ='A01ND'

Open in new window

Btw, why do you need the GROUP BY? Do you have duplicate values?
0
 

Author Comment

by:PeterBaileyUk
ID: 41750007
if i save the subquery then the outer query can use it or do i need to do something else?
0
 

Author Closing Comment

by:PeterBaileyUk
ID: 41750014
I wasnt sure sql server worked in this way, but it does so thank you
0
 
LVL 36

Expert Comment

by:ste5an
ID: 41750017
I would use a CTE:

WITH    QrySubQuery
          AS ( SELECT   WT.ClientCode ,
                        WT.ClientCodeWordPosition
               FROM     TblWordTags WT
               GROUP BY WT.ClientCode ,
                        WT.ClientCodeWordPosition
             )
    SELECT  W.ClientCodeWordPosition ,
            W.ClientCode ,
            W.Word ,
            Q.ClientCode ,
            W.StrFull
    FROM    TblWords W
            LEFT JOIN QrySubQuery Q ON ( W.ClientCodeWordPosition = Q.ClientCodeWordPosition
                                         AND W.ClientCode = Q.ClientCode
                                       )
    WHERE   W.ClientCode = 'A01ND';

Open in new window

0

Featured Post

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
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.
Suggested Courses

755 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