Solved

create query in sql server

Posted on 2016-08-10
5
17 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 142

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 46

Accepted Solution

by:
Vitor Montalvão earned 500 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 33

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

I have a large data set and a SSIS package. How can I load this file in multi threading?
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how the fundamental information of how to create a table.

920 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now