Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««123»»

Cursor or Temp Files? Expand / Collapse
Author
Message
Posted Thursday, December 12, 2013 2:37 PM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Friday, September 19, 2014 7:57 AM
Points: 989, Visits: 1,327
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
Post #1522488
Posted Thursday, December 12, 2013 2:40 PM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Tuesday, May 13, 2014 11:02 AM
Points: 56, Visits: 94
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)
)
Post #1522490
Posted Thursday, December 12, 2013 2:46 PM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Tuesday, May 13, 2014 11:02 AM
Points: 56, Visits: 94
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
Post #1522492
Posted Thursday, December 12, 2013 2:47 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 1:04 PM
Points: 13,086, Visits: 12,553
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)
Post #1522493
Posted Thursday, December 12, 2013 2:51 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 1:04 PM
Points: 13,086, Visits: 12,553
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).

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)
Post #1522495
Posted Thursday, December 12, 2013 2:52 PM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Tuesday, May 13, 2014 11:02 AM
Points: 56, Visits: 94
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
Post #1522496
Posted Thursday, December 12, 2013 3:01 PM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Tuesday, May 13, 2014 11:02 AM
Points: 56, Visits: 94
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
Post #1522499
Posted Monday, December 16, 2013 11:14 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Tuesday, May 13, 2014 11:02 AM
Points: 56, Visits: 94
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'
)
Post #1523370
Posted Monday, December 16, 2013 12:04 PM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Friday, September 19, 2014 7:57 AM
Points: 989, Visits: 1,327
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
Post #1523390
Posted Monday, December 16, 2013 12:07 PM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Tuesday, May 13, 2014 11:02 AM
Points: 56, Visits: 94
Thank you Kurt. I appreciate you help very much.

Gillian
Post #1523391
« Prev Topic | Next Topic »

Add to briefcase ««123»»

Permissions Expand / Collapse