?
Solved

create query in sql server

Posted on 2016-08-10
5
Medium Priority
?
27 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
[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 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 51

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 35

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
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
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Suggested Courses

762 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