Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Cursor or Temp Files?


Cursor or Temp Files?

Author
Message
Kurt W. Zimmerman
Kurt W. Zimmerman
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1080 Visits: 1396
Well with a little tinkering here is what I came up with:


;with C_Orders_CTE (conum, itemnum, coitemstat) as (
select conum
, itemnum
, coitemstat
from @Orders
where coitemstat = 'C'
), O_Orders_CTE (conum, itemnum, coitemstat) as (
select conum
, itemnum
, coitemstat
from @Orders
where coitemstat = 'O'
)
select distinct ord.conum
from @Orders ORD inner join
C_Orders_CTE C_CTE on ord.conum = C_CTE.conum
and ord.itemnum = C_CTE.itemnum left join
O_Orders_CTE O_CTE on C_CTE.conum = O_CTE.conum

where O_CTE.conum is null




Here is the result I get:

conum
AW00000253
SF00000389
SF00000390

Kurt W. Zimmerman
SR DBA
Lefrak Organization
New York, NY

http://www.linkedin.com/in/kurtwzimmerman
Gillian_Pappas2002
Gillian_Pappas2002
SSC-Enthusiastic
SSC-Enthusiastic (104 reputation)SSC-Enthusiastic (104 reputation)SSC-Enthusiastic (104 reputation)SSC-Enthusiastic (104 reputation)SSC-Enthusiastic (104 reputation)SSC-Enthusiastic (104 reputation)SSC-Enthusiastic (104 reputation)SSC-Enthusiastic (104 reputation)

Group: General Forum Members
Points: 104 Visits: 122
Hi Sean.

Here is my table.

DECLARE @TempCOGGP TABLE
(conum varchar(10),
coline varchar(10),
itemnum varchar(35),
custnum varchar(10),
custseq int,
qtyorder decimal (10,8),
qtyready decimal (10,8),
qtyship decimal (10,8),
promise date,
ship date,
orderdate date,
coitemstat varchar(3),
allocate date,
pattern varchar(3),
room varchar(1),
Tag varchar(1)
)
Gillian_Pappas2002
Gillian_Pappas2002
SSC-Enthusiastic
SSC-Enthusiastic (104 reputation)SSC-Enthusiastic (104 reputation)SSC-Enthusiastic (104 reputation)SSC-Enthusiastic (104 reputation)SSC-Enthusiastic (104 reputation)SSC-Enthusiastic (104 reputation)SSC-Enthusiastic (104 reputation)SSC-Enthusiastic (104 reputation)

Group: General Forum Members
Points: 104 Visits: 122
Thank you very much Kurt. That is awesome and is exactly what they are looking for.

Pardon my ignorance but I have never seen ;with before, and are you using alias's? Not exactly sure how to plug that in correctly into my .sp.

Thanks again for your patience and extreme help.

Gillian
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16667 Visits: 17030
Gillian_Pappas2002 (12/12/2013)
Thank you Sean.

Sorry about the mess.


Hi. Thank you again for your response. That works very well to get rid of either status C or O, but what they want me to do is to look at all of the same customer order numbers and their status. If the same customer order number has ANY status of 'O', they do not want to see any of those customer order records. They only want to see like customer orders that All have a 'C' status.

Below is the before status before applying your solution. They do not want to see ANY Orders 905055 because it has one with a status of 'O'. but they would want to see order AW00000253 because it doesn't have any status's of 'O". I hope this makes sense and I appreciate all of your help.

Order# Line# STATUS
905055 1 C
905055 2 C
905055 3 C
905055 4 O
AW00000253 1 C
AW00000253 2 C


You really need to actually read the article that has been suggested. If you had read it you would see that just sticking some values in a post is nothing like consumable data. We want and need to see ddl (create table) and data (insert statements). What you should have posted would look like this.


create table #Something
(
OrderNum varchar(20),
LineNum int,
Status char(1)
)

insert #Something
select '905055', 1, 'C' union all
select '905055', 2, 'C' union all
select '905055', 3, 'C' union all
select '905055', 4, 'O' union all
select 'AW00000253', 1, 'C' union all
select 'AW00000253', 2, 'C';



Then we would be able to see and work with the same thing. This makes it really easy for us to help you.

Somebody like myself can then come along and post a query that meets your requirements. Something like this one...


select *
from #Something
where OrderNum in
(
select OrderNum
from #Something
group by OrderNum
having MAX(Status) = 'C'
)



_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16667 Visits: 17030
Gillian_Pappas2002 (12/12/2013)
Thank you very much Kurt. That is awesome and is exactly what they are looking for.

Pardon my ignorance but I have never seen ;with before, and are you using alias's? Not exactly sure how to plug that in correctly into my .sp.

Thanks again for your patience and extreme help.

Gillian


WITH is the first part of a common table expression (cte). In actuality the ;with is a little odd. Many people got in the habit of starting a line of code like that but the truth is that a cte requires the previous line of code to end with a semicolon. Remember that the semicolon is a line terminator, not a line beginninator (credit to Lynn Pettis for the phrasing). :-D

In essence it is like an inline view. Check them out on BOL here. http://msdn.microsoft.com/en-us/library/ms175972.aspx

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Gillian_Pappas2002
Gillian_Pappas2002
SSC-Enthusiastic
SSC-Enthusiastic (104 reputation)SSC-Enthusiastic (104 reputation)SSC-Enthusiastic (104 reputation)SSC-Enthusiastic (104 reputation)SSC-Enthusiastic (104 reputation)SSC-Enthusiastic (104 reputation)SSC-Enthusiastic (104 reputation)SSC-Enthusiastic (104 reputation)

Group: General Forum Members
Points: 104 Visits: 122
Thank you very much for you help and I understand. Sorry for my ignorance.



DECLARE @TempCOGGP TABLE
(conum varchar(10),
coline varchar(10),
itemnum varchar(35),
custnum varchar(10),
custseq int,
qtyorder decimal (10,8),
qtyready decimal (10,8),
qtyship decimal (10,8),
promise date,
ship date,
orderdate date,
coitemstat varchar(3),
allocate date,
pattern varchar(3),
room varchar(1),
Tag varchar(1)
)
/********** */




insert into @TempCOGGP
SELECT coitem.co_num,
coitem.co_line,
coitem.item,
coitem.cust_num,
coitem.cust_seq,
qty_ordered,
qty_ready,
qty_shipped,
promise_date,
coitem.ship_date,
co.order_date,
coitem.stat,
coitem.Uf_PJ_AllocatedDate as AvailableDate,
@Pattern as pattern,
@Room as room,
@Tag

FROM coitem
inner join co on co.co_num = coitem.co_num
--inner join customer on customer.cust_num = coitem.cust_num and customer.cust_seq = coitem.cust_seq
Where (coitem.promise_date >= @StartInvoiceDate and coitem.promise_date <=
@EndInvoiceDate) and (substring (coitem.item,1,3) = @Pattern and
SUBSTRING (coitem.item, 6,1) = @Room)
ORDER BY coitem.co_num, coitem.co_line

select * from @TempCOGGP
Gillian_Pappas2002
Gillian_Pappas2002
SSC-Enthusiastic
SSC-Enthusiastic (104 reputation)SSC-Enthusiastic (104 reputation)SSC-Enthusiastic (104 reputation)SSC-Enthusiastic (104 reputation)SSC-Enthusiastic (104 reputation)SSC-Enthusiastic (104 reputation)SSC-Enthusiastic (104 reputation)SSC-Enthusiastic (104 reputation)

Group: General Forum Members
Points: 104 Visits: 122
Thank you for your explanation and link. I will will be reading this tonight and hope I can get it to work sometime tomorrow.

I really appreciate you help and patience.

Sincerely.

Gillian
Gillian_Pappas2002
Gillian_Pappas2002
SSC-Enthusiastic
SSC-Enthusiastic (104 reputation)SSC-Enthusiastic (104 reputation)SSC-Enthusiastic (104 reputation)SSC-Enthusiastic (104 reputation)SSC-Enthusiastic (104 reputation)SSC-Enthusiastic (104 reputation)SSC-Enthusiastic (104 reputation)SSC-Enthusiastic (104 reputation)

Group: General Forum Members
Points: 104 Visits: 122
Hi Kurt.

I hope you had a nice weekend. I read up on the cte and tried to apply your suggestions over the weekend. I keep getting compile errors on the ';with', and the ending, 'O_Orders_CTE (co_num,' Any suggestions?


/***************** START CTE *********/

;with C_ORDERS_CTE (
co_num,
co_line,
item,
cust_num,
cust_seq,
qty_ordered,
qty_ready,
qty_shipped,
promise_date,
ship_date,
order_date,
stat,
Uf_PJ_AllocatedDate, ---as AvailableDate,
@Pattern, ---as pattern,
@Room, ---as room,
@Tag)
AS
(
SELECT coitem.co_num,
coitem.co_line,
coitem.item,
coitem.cust_num,
coitem.cust_seq,
qty_ordered,
qty_ready,
qty_shipped,
promise_date,
coitem.ship_date,
co.order_date,
coitem.stat,
coitem.Uf_PJ_AllocatedDate as AvailableDate,
@Pattern as pattern,
@Room as room,
@Tag

FROM coitem
inner join co on co.co_num = coitem.co_num
--inner join customer on customer.cust_num = coitem.cust_num and customer.cust_seq = coitem.cust_seq
Where (coitem.promise_date >= @StartInvoiceDate and coitem.promise_date <=
@EndInvoiceDate) and (substring (coitem.item,1,3) = @Pattern and
SUBSTRING (coitem.item, 6,1) = @Room) and coitem.stat = 'C'
), O_Orders_CTE (co_num,
co_line,
coitem.item,
coitem.cust_num,
coitem.cust_seq,
qty_ordered,
qty_ready,
qty_shipped,
promise_date,
coitem.ship_date,
co.order_date,
coitem.stat,
coitem.Uf_PJ_AllocatedDate as AvailableDate,
@Pattern as pattern,
@Room as room,
@Tag
)
AS
(
SELECT coitem.co_num,
coitem.co_line,
coitem.item,
coitem.cust_num,
coitem.cust_seq,
qty_ordered,
qty_ready,
qty_shipped,
promise_date,
coitem.ship_date,
co.order_date,
coitem.stat,
coitem.Uf_PJ_AllocatedDate as AvailableDate,
@Pattern as pattern,
@Room as room,
@Tag
FROM coitem
inner join co on co.co_num = coitem.co_num
--inner join customer on customer.cust_num = coitem.cust_num and customer.cust_seq = coitem.cust_seq
Where (coitem.promise_date >= @StartInvoiceDate and coitem.promise_date <=
@EndInvoiceDate) and (substring (coitem.item,1,3) = @Pattern and
SUBSTRING (coitem.item, 6,1) = @Room) and coitem.stat = 'O'
)
Kurt W. Zimmerman
Kurt W. Zimmerman
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1080 Visits: 1396
On your CTE declaration you have @Pattern, @Room, & @Tag. I'd change them to Pattern, Room, & Tag.


;with C_ORDERS_CTE (
co_num,
co_line,
item,
cust_num,
cust_seq,
qty_ordered,
qty_ready,
qty_shipped,
promise_date,
ship_date,
order_date,
stat,
Uf_PJ_AllocatedDate, ---as AvailableDate,
@Pattern, ---as pattern,
@Room, ---as room,
@Tag)



change to

;with C_ORDERS_CTE (
co_num,
co_line,
item,
cust_num,
cust_seq,
qty_ordered,
qty_ready,
qty_shipped,
promise_date,
ship_date,
order_date,
stat,
Uf_PJ_AllocatedDate, ---as AvailableDate,
Pattern, ---as pattern,
Room, ---as room,
Tag)



Kurt W. Zimmerman
SR DBA
Lefrak Organization
New York, NY

http://www.linkedin.com/in/kurtwzimmerman
Gillian_Pappas2002
Gillian_Pappas2002
SSC-Enthusiastic
SSC-Enthusiastic (104 reputation)SSC-Enthusiastic (104 reputation)SSC-Enthusiastic (104 reputation)SSC-Enthusiastic (104 reputation)SSC-Enthusiastic (104 reputation)SSC-Enthusiastic (104 reputation)SSC-Enthusiastic (104 reputation)SSC-Enthusiastic (104 reputation)

Group: General Forum Members
Points: 104 Visits: 122
Thank you Kurt. I appreciate you help very much.

Gillian
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search