My Assistant
![]() ![]() |
|
|
Mar 5 2012, 06:30 PM
Post
#1
|
|
|
UtterAccess Ruler Posts: 4,237 From: Downey, CA |
I'm new to T-SQL, and having problems with the syntax of the query I want - this query shows all our customers who are receiving free service - I need to see the value of the free service - the service codes and the short description (ONLINE_DESC_ALA) are unique for those customers who get some free service. But the long description (STMT_DESCR_LINE1_ALA) is the same - Employees (EMP) get free service, so...
ServiceCode.......ONLINE_DESC_ALA.........STMT_DESCR_LINE1_ALA..........CHRG_ALA ......WE012...............Widget-EMP...........................Widget..............................$0.00 ......WG055...............Basic Widget.........................Widget..............................$5.95 I want the large code block below to be my 'main' query, but also show in my record set ALL records where there is a match on the STMT_DESCR_LINE1_ALA, and the corresponding charge (CHRG_ALA) is it possible to make the following query a SUB query, then query off of it, where the SubQuery.STMT_DESCR_LINE1_ALA = MainQuery.STMT_DESCR_LINE1_ALA ? CODE SELECT A.SUB_ACCT_NO_SBB ,a.RES_NAME_SBB ,a.VIP_FLG_SBB ,b.ADDR1_HSE ,b.RES_CITY_HSE ,B.RES_STATE_HSE ,B.POSTAL_CDE_HSE ,D.SERV_CDE_ALA ,D.ONLINE_DESC_ALA ,D.STMT_DESCR_LINE1_ALA ,D.CHRG_ALA Charge ,e.DESCR_CTD ,E.CDE_VALUE_CTD CodeValue_CTD from dbo.tbl_SBB_BASE_TW A ,dbo.tbl_HSE_BASE_TW B ,dbo.tbl_OCI_CUR_ITEM_TW C ,dbo.tbl_ALA_SERV_CODE_TW D ,dbo.tbl_CTD_TW E WHERE A.HSE_KEY_SBB=B.HSE_KEY_HSE AND A.SUB_ACCT_NO_SBB=C.SUB_ACCT_NO_OCI AND C.SYS_OCI = d.SYS_ALA AND C.PRIN_OCI = d.PRIN_ALA AND C.AGNT_OCI = d.AGNT_ALA and C.SERV_CDE_OCI = d.SERV_CDE_ALA AND A.VIP_FLG_SBB in ('R','U','E','W') And C.PRIN_OCI in (4100) and C.AGNT_OCI in (960,1030) and E.CDE_TBL_NO_CTD = '24' AND E.CDE_VALUE_CTD=A.VIP_FLG_SBB order by A.SUB_ACCT_NO_SBB |
|
|
|
Mar 8 2012, 07:35 PM
Post
#2
|
|
|
UtterAccess Guru Posts: 744 |
Ouch.
First of all, that's a really old syntax (like Oracle in 1999). Take out all the JOIN statements in the WHERE clause (tblA.Field1=tblB.FieldX) and put them in the JOIN clause where they belong. If you really need help, post the CREATE TABLE statement and enough INSERT statements to populate a table so the results can be checked. (If it's too hard to help you, nobody is going to do it, because it's too much... well, work!) |
|
|
|
Mar 8 2012, 07:38 PM
Post
#3
|
|
|
UtterAccess Ruler Posts: 4,237 From: Downey, CA |
Mad, thanks - perhaps you missed:
QUOTE I'm new to T-SQL haha - I don't even know how to create a table in T-SQL :-( |
|
|
|
Mar 9 2012, 10:04 AM
Post
#4
|
|
|
UtterAccess Member Posts: 22 |
Quick answer is yes. You can use a query as a sub query.
Like Mad, however, I would have to ask where the code above came from as I've not seen that style since my informix days, some 10 years ago and even then its was somewhat out of date! But by way of example, I think you need something in the following style: SELECT * FROM dbo.[Table] MQ LEFT OUTER JOIN ( SELECT [Subquery] FROM dbo.[Table] T INNER JOIN dbo.[OtherTables] OT ON T.[ID] = OT.[ID] WHERE [Things] = 'Wasnames' AND [Watchamacallits] = 'thingymagigs' ) SQ ON MQ.[ID] = SQ.[ID] This will give you everything from table "table", and the information from the subquery only where it matches |
|
|
|
Mar 12 2012, 10:26 AM
Post
#5
|
|
|
UtterAccess Ruler Posts: 4,237 From: Downey, CA |
Quick answer is yes. You can use a query as a sub query. Like Mad, however, I would have to ask where the code above came from as I've not seen that style since my informix days, some 10 years ago and even then its was somewhat out of date! But by way of example, I think you need something in the following style: SELECT * FROM dbo.[Table] MQ LEFT OUTER JOIN ( SELECT [Subquery] FROM dbo.[Table] T INNER JOIN dbo.[OtherTables] OT ON T.[ID] = OT.[ID] WHERE [Things] = 'Wasnames' AND [Watchamacallits] = 'thingymagigs' ) SQ ON MQ.[ID] = SQ.[ID] This will give you everything from table "table", and the information from the subquery only where it matches not sure about the [subquery] part of this |
|
|
|
Mar 13 2012, 05:17 AM
Post
#6
|
|
|
UtterAccess Member Posts: 22 |
Probably a poor choice of example word. Enclosing something in [] indicates to SQL it is a column name. Therefore [Subquery] is just a column name. That said, I then went on to use [ID] in the join statement, not [Subquery], so my bad, sorry. This should make more sense:
SELECT * FROM dbo.[Table] MQ LEFT OUTER JOIN ( SELECT DISTINCT [ID] ON MQ.[ID] = SQ.[ID] *The distict just covers off the chance of the subquery returning multiple rows. Remove if that is intended behaviour |
|
|
|
![]() ![]() |
|
Go to Top · Lo-Fi Version | Time is now: 20th May 2013 - 11:14 AM |