Solved

create query in sql server

Posted on 2016-08-10
5
15 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 45

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 32

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
MS SQL 2014 get SPIDs of users 6 26
SQl help with selection 14 44
C# Application Local DB Connection String 23 59
Test a query 23 15
Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.

757 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

19 Experts available now in Live!

Get 1:1 Help Now