473,425 Members | 1,610 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,425 software developers and data experts.

How to find those records have no parent and child in same table?

i have the following table:

CREATE TABLE dbo.Invoices ( [InvoiceCode] varchar(20), [ParentInvoiceCode] varchar(20),
[InvoiceDate] date )
INSERT INTO dbo.Invoices
VALUES
( 'INV-001', 'INV-001', N'2015-01-01 00:00:00.000' ),
( 'INV-002', 'INV-001', N'2015-01-01 00:00:00.000' ),
( 'INV-003', 'INV-001', N'2015-01-01 00:00:00.000' ),
( 'INV-004', 'INV-004', N'2015-01-01 00:00:00.000' ),
( 'INV-005', 'INV-005', N'2015-01-01 00:00:00.000' ),
( 'INV-006', 'INV-006', N'2015-01-01 00:00:00.000' ),
( 'INV-007', 'INV-007', N'2015-01-01 00:00:00.000' ),
( 'INV-008', 'INV-007', N'2015-01-01 00:00:00.000' ),
( 'INV-009', 'INV-007', N'2015-01-01 00:00:00.000' ),
( 'INV-010', 'INV-007', N'2015-01-01 00:00:00.000' )


as you can see that record number 4,5,6 have no parent and child.
what will be the query to show these records only.
Jun 6 '21 #1
2 4066
Banfa
9,065 Expert Mod 8TB
It is more like they are their own parent/child, the records you are looking for are the ones that only have 1 child, themselves.

Lets assume your table is called yTable, the first field is called id and the second field is called parent then

Expand|Select|Wrap|Line Numbers
  1. SELECT id, count(parent) AS children
  2. FROM yTable
  3. GROUP BY id
  4. WHERE children=1;
May do it, sorry my SQL is a bit rusty and I have no server to test it on.
Jun 7 '21 #2
SioSio
272 256MB
The order of execution is as follows.
FROM-> WHERE-> GROUP BY-> HAVING-> SELECT-> ORDER BY
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM `Invoices` GROUP BY `ParentInvoiceCode` HAVING (Count(`ParentInvoiceCode`))=1;
The "GROUP BY" clause is executed after "FROM".
Group by `ParentInvoiceCode` in "GROUP BY".
The result is extracted under the condition specified in "HAVING" (in this case, (COUNT(`ParentInvoiceCode`)) = 1).
Jun 8 '21 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: michael Tepperis | last post by:
the following SQL is not acceepted by DB2 V7: INSERT INTO BLT15 VALUES SELECT A.TNR , A.LNR , A.LINDEX , 'P'
1
by: Nicolae Fieraru | last post by:
Hi All, I have a table with the following information: ID, Name, Address, Field4 In this table it happens to have duplicate records with identical Name and Address but different content in...
2
by: mkidd | last post by:
Hi, I'm new at vb .net and I have an issue that must be a common one, but I haven't found an example of it yet. I want to create parent-child relationships for 3 tables...
2
by: Joost Kraaijeveld | last post by:
I have a table with column1, column2, column3 and column4. How do I get all records, sorted by column4 that have the same column1,column2 and column3? TIA Joost ...
1
by: AA Arens | last post by:
On the main form I choosed a position (like manager, secretary...) from a combo box. The field in the footer form of another form mentioned the ID of the position. How to have the position TEXT...
1
by: Rdailey | last post by:
Hello, I am trying to compare record values in one column to the next column within the same table. I have tried everything that I can think of and still have not achieved the desired result. ...
5
by: binky | last post by:
Question for all you SQL gurus out there. I have a (seemingly) complex query to write, and since I'm just learning SQL server, am not sure how to go about it. Tables: t_trans t_account All...
0
by: Deano | last post by:
I have a feature where the user is allowed to make a copy of their data. Some of these tables are related to a child table. From what I can see the easiest way to do this is to copy to a temp...
6
by: A1M6K | last post by:
Very new to VBA and struggling to link the main form and the subform to the same record in the same table. Currently they are each entering separate records in the same table. They are linked by...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.