Solved

create query in sql server

Posted on 2016-08-10
5
21 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 49

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Suggested Solutions

Title # Comments Views Activity
SQL Syntax 6 41
Data encryption options between SQL DBs 3 32
Need age at date of document 5 20
Please explain Equi-join 3 14
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
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 ?
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

733 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