473,403 Members | 2,338 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Stumped by SQL challenge

Here is the table:

CREATE TABLE [child]
(
[pk_child_id] [int] NOT NULL ,
[fk_parent_id] [int] NOT NULL ,
[code] [char] (2)NOT NULL ,
[dt] [datetime] NOT NULL ,
[newcode] [int] NULL
)

There is a situation where there will be more than one record with the
same [fk_parent_id] value, but different values for the [code]field.
If one of those records has a [code]= 5, but the [dt] is AFTER a
similar record where [code]= 6 or [code]= 7 (but same [fk_parent_id]
value), I need to set [newcode] = 10. How can I pull this off? Again,
the group of records can have different [code] values, different [dt]
values, but a common [fk_parent_id].

Help!

Sep 19 '05 #1
4 1045
Thanks for posting the DDL. It does help if you include keys and constraints
with your CREATE TABLE statements and post some sample data abd required
results too. Here's my guess, hopefully it will give you a start even if it
isn't exactly what's required.

UPDATE child
SET newcode = 10
WHERE code = 5
AND EXISTS
(SELECT *
FROM child AS C
WHERE fk_parent_id = child.fk_parent_id
AND code > child.code
AND dt < child.dt) ;

--
David Portas
SQL Server MVP
--
Sep 19 '05 #2
Thank you for the information. I'm wondering how this would work as a
SELECT statement. Also, let's assume there is a foreign key constraint
on fk_parent_id. And the pk field is a primary key.

David Portas wrote:
Thanks for posting the DDL. It does help if you include keys and constraints
with your CREATE TABLE statements and post some sample data abd required
results too. Here's my guess, hopefully it will give you a start even if it
isn't exactly what's required.

UPDATE child
SET newcode = 10
WHERE code = 5
AND EXISTS
(SELECT *
FROM child AS C
WHERE fk_parent_id = child.fk_parent_id
AND code > child.code
AND dt < child.dt) ;

--
David Portas
SQL Server MVP
--


Sep 20 '05 #3
On 20 Sep 2005 09:10:49 -0700, im*******************@yahoo.com wrote:
Thank you for the information. I'm wondering how this would work as a
SELECT statement. Also, let's assume there is a foreign key constraint
on fk_parent_id. And the pk field is a primary key.


Hi imani,

A better way to get replies is to post your complete table structure,
including keys and relationships, as DDL. Also, supply sample data as
INSERT statements and expected output.

Here's a wild guess:

SELECT other columns,
CASE
WHEN code = 5
AND EXISTS
(SELECT *
FROM child AS C
WHERE fk_parent_id = child.fk_parent_id
AND code > child.code
AND dt < child.dt)
THEN 10
ELSE newcode
END AS newcode
FROM child

(untested)

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Sep 22 '05 #4
Please post completeDDL, so that people do not have to guess what the
keys, constraints, Declarative Referential Integrity, data types, etc.
in your schema are. Sample data is also a good idea, along with clear
specifications.

We never use prefixes like "pk-" or 'fk-" on data element names
(ISO-11179 rules). We also know that columns are not fields and rows
are not records. Names like "dt" and "code" beg that question "of
what??" Since you used a singular name for the table, does it have
only one row?

It looks like you are trying to build a self-referencing adjacency
list model for a hierarchy, but you never gave us constraints for that.
There are better ways to build a hierarchies. Can you explain your
problem?

Sep 25 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

42
by: Frank Buss | last post by:
I've setup a challenge, mainly for C++, Java and Lisp, but every other language is welcome: http://www.frank-buss.de/challenge/index.html There is nothing to win, but I hope there will be some...
8
by: Frank Buss | last post by:
A new challenge: http://www.frank-buss.de/marsrescue/index.html Have fun! Now you can win real prices. -- Frank Buß, fb@frank-buss.de http://www.frank-buss.de, http://www.it4-systems.de
0
by: Richard Jones | last post by:
The date for the second PyWeek challenge has been set: Sunday 26th March to Sunday 2nd April (00:00UTC to 00:00UTC). The PyWeek challenge invites entrants to write a game in one week from...
0
by: richard | last post by:
The date for the second PyWeek challenge has been set: Sunday 26th March to Sunday 2nd April (00:00UTC to 00:00UTC). The PyWeek challenge invites entrants to write a game in one week from...
11
by: CMM | last post by:
First let me say that maybe I'm having a "duh" moment and perhaps I'm missing something... but it seems to me that no one thing in the System.Collections namespace (even in .NET 2.0) even comes...
78
by: wkehowski | last post by:
The python code below generates a cartesian product subject to any logical combination of wildcard exclusions. For example, suppose I want to generate a cartesian product S^n, n>=3, of that...
3
by: Thierry | last post by:
For those interested in <b>programming riddles</b>, I would like to announce a new programming challenge I'm just launching at http://software.challenge.googlepages.com This challenge is in its...
0
by: Richard Jones | last post by:
The fifth PyWeek is only a month away. Come along and join the fun: write a video game in a week! There's some really interesting new libraries that have popped up recently. Have a gander on the...
12
by: Tom | last post by:
First, my thanks in advance to those who can help explain away this mystery line of code. Petzold is a great teacher and I appreciate his work; however, his explanation about an extra line of...
80
by: jacob navia | last post by:
Several people in this group argue that standard C is not portable since there are no compilers for it, etc. I propose this program in Standard C, that I have compiled in several OSes to test if...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.