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


Help writing query logic


Help writing query logic

Author
Message
stevenplee
stevenplee
SSC Journeyman
SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)

Group: General Forum Members
Points: 78 Visits: 307
I need a little help figuring out this logic. I need to write a query that will set the Field InvoiceinPer value to Y if the order# is the same and the date is the same regardless to the transType. (see example1)

Example2 Will set the invoiceinper value to Y only if the transtype is anything other than Credit or rebill and the dates are different. Suggestion would be appreciated.
Ex1
Date Order# Transtype Amount InvoiceinPer
12/1/2010 12345 Inv 1000 Y
12/1/2010 12345 Credit -1000 Y
12/1/2010 12345 Rebill 2000 Y

Ex2
Date Order# Transtype Amount InvoiceinPer
12/1/2010 12345 Inv 1000 Y
12/3/2010 12345 Credit -1000 N
12/3/2010 12345 Rebill 2000 N
LutzM
LutzM
SSCertifiable
SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)

Group: General Forum Members
Points: 7021 Visits: 13559
Something like this?
DECLARE @tbl TABLE
(
DATE DATETIME, OrderNmbr INT, Transtype VARCHAR(10), Amount INT, InvoiceinPer CHAR(1)
)
--/*
INSERT INTO @tbl
SELECT '12/1/2010', 12345 ,'Inv', 1000 ,'N' UNION ALL
SELECT '12/3/2010', 12345 ,'Credit', -1000 ,'N' UNION ALL
SELECT '12/3/2010', 12345 ,'Rebill', 2000 ,'N'
--*/
/*
insert into @tbl
select '12/1/2010', 12345 ,'Inv', 1000 ,'N' union all
select '12/1/2010', 12345 ,'Credit', -1000 ,'N' union all
select '12/1/2010', 12345 ,'Rebill', 2000 ,'N'
*/
;WITH cte AS
(
SELECT TOP 1 DATE FROM @tbl t ORDER BY DATE
)
UPDATE @tbl
SET InvoiceinPer='Y'
FROM @tbl t
INNER JOIN cte ON t.date = cte.date

SELECT *
FROM @tbl





Lutz
A pessimist is an optimist with experience.

How to get fast answers to your question
How to post performance related questions
Links for Tally Table , Cross Tabs and Dynamic Cross Tabs , Delimited Split Function
stevenplee
stevenplee
SSC Journeyman
SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)

Group: General Forum Members
Points: 78 Visits: 307
Thanks, For your response. What does the ":with cte AS" statement do?

Thanks I'll give this a try
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45476 Visits: 39948
stevenplee (12/23/2010)
Thanks, For your response. What does the ":with cte AS" statement do?


A whole lot. Take a look at "WITH common_table_expression" in Books Online... there's just too much info to post here.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
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