Solved

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

Posted on 2014-01-23
4
1,094 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
  • 3
4 Comments
 
LVL 59

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 59

Accepted Solution

by:
Kevin Cross earned 500 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 59

Expert Comment

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

Best regards and happy coding,

Kevin
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Introduction In the following article I’ll be discussing and demonstrating several different ways of how images can be put on a report. I’m using SQL Server Reporting Services 2008 R2 CTP, more precisely version 10.50.1352.12, but the methods ex…
A recent questions about how to add SSRS named instances, couldn't find any that talks about SQL server 2008, anyway I decided to help by creating some screen shots. The installation is straightforward, you just pop the SQL server 2008 installati…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

809 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