?
Solved

T-SQL Using a CTE as a View and "Adding" a Field from Another CTE

Posted on 2014-01-23
4
Medium Priority
?
1,128 Views
Last Modified: 2014-01-24
Hello:

Attached are four Word documents.  The docs entitled "ITEMAGEPOS" and "ITEMAGENEG" represent CTEs.  The other two docs are screenshots showing samples of results from running either CTE.

I'd like to place "ITEMAGEPOS" in a view.  And, in that view, I'd like to add the [Quantity Remaining] field from "ITEMAGENEG" to the Quantity Remaining field of "ITEMAGEPOS" for every match of [Item Number], [Location Code], and [Date Range] in "ITEMAGEPOS".  

How do I accomplish both of these tasks?

I'm probably going to have to create a view for "ITEMAGENEG", ad well.  But, the only data that I want from "ITEMAGENEG" to be "within" "ITEMAGEPOS" is [Quantity Remaining].

Thanks!  Much appreciated!

TBSupport
ITEMAGEPOS.docx
Results-of-ITEMAGEPOS.docx
ITEMAGENEG.docx
Results-of-ITEMAGENEG.docx
0
Comment
Question by:TBSupport
[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
  • 3
4 Comments
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 39804171
I will take a look, but as an introductory note you can nest CTEs; therefore, one solution could be the following.

WITH cte1 AS ( ... ), cte2 AS ( ... )
SELECT ... FROM cte1 JOIN cte2 ON ...

I will open the Word documents, though, and see if my understanding of what you need changes.
0
 
LVL 60

Accepted Solution

by:
Kevin Cross earned 2000 total points
ID: 39804404
Okay, reviewing your code, the nested CTE option still is a viable solution; however, I notice that the only difference between the two queries is the filter on IV30300.DOCTYPE and IV30300.TRXQTY.  Therefore, you may be able to accomplish this with one query, leveraging conditional aggregates.

In other words, you would exclude WHERE conditions for IV30300.DOCTYPE, or make them inclusive of both queries (e.g., IV30300.DOCTYPE IN (1, 4, 6, 7) and IV30300.TRXQTY <> 0).  Subsequently, you could have multiple columns that use SUM(...) OVER(...).

Instead of: SUM(IV30300.TRXQTY) OVER (...)
Use:
SUM(
  CASE 
    WHEN IV30300.DOCTYPE IN (1, 6) AND IV30300.TRXQTY < 0 
      THEN IV30300.TRXQTY
    ELSE 0
  END
) OVER (...)

Open in new window


In fact, you can use this technique to perform the different aging calculations in one query also, avoiding the UNION (repetition of JOINs).  You would just have one SUM for each date range with a different WHEN condition on each that includes the document type, transaction quantity, and document date filters.

If you wish to stay with the multiple CTEs, the approach is as I stated above.

WITH CTE_NEG 
AS (
    /* query for CTE_NEG here. */
)
, CTE_POS 
AS (
    /* query for CTE_POS here. */
)
SELECT /* columns you want. */
FROM CTE_POS p
LEFT JOIN CTE_NEG n ON /* join fields */ AND n.RowNumber = p.RowNumber
;

Open in new window


I hope that helps!

P.S. For completeness, note the nested CTE approach also allows you do keep your current final SELECTs in their own CTE, then do whatever you want at the end.

WITH CTE_NEG 
AS (
    /* query for CTE_NEG here. */
)
, CTE_POS 
AS (
    /* query for CTE_POS here. */
)
, CTE_POS_SEL
AS (
    SELECT [Date Range], [Item Number], [Item Description], [Doc]
         , CASE 
             WHEN RowNumber = 1 THEN CAST([SUMQTY] AS VARCHAR(20)) 
             ELSE '' END AS [Quantity Remaining]
         , [Location Code], [Document Type], [Document Date], [Qty], [U Of M]
         , [Unit Cost], [Extended Cost], [Use]
    FROM CTE_POS
)
, CTE_NEG_SEL
AS (
    /* SELECT query from CTE_NEG like one above for CTE_POS here. */
)
SELECT /* columns you want. */
FROM CTE_POS_SEL p
LEFT JOIN CTE_NEG_SEL n ON /* join fields */
;

Open in new window

0
 
LVL 1

Author Comment

by:TBSupport
ID: 39806693
Thank you!  This worked!

TBSupport
0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 39806890
You are most welcome!

Best regards and happy coding,

Kevin
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Hi, I have heard from my friends that it’s not possible to create Label Printing report using SSRS. I am amazed after hearing this words not possible in SSRS. I googled lot and found that it is possible to some of people know about the Report Bui…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

752 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