November 20, 2013 at 1:49 pm
Hi Guys
I have a data where there are parent and child product.
Each parent has got a unique code(P1) and a link code (L1) on their account
and each child of the parent has got a seperate code (C12) but they share the same account no.
I want to count the parent and children
Here is a sample dataset
Line_no code Account No
123 C12 Ac111
1222 C12 Ac111
1243 C12 Ac111
433 P1 Ac111
433 L1 Ac111
543 C1 Ac222
544 C1 Ac222
4322 P1 Ac222
4322 L1 Ac222
Now if I do a group by, I di get unique parents but not unique children
Please help!!
November 20, 2013 at 2:05 pm
Hi and welcome to the forums. Since you are pretty new around here you have probably not seen the best practices article about how to post questions. You can find it at the first link in my signature.
Generally speaking it is best if you can post sample data in a consumable format. I did this for you.
create table #Something
(
Line_no int,
code varchar(5),
AccountNo char(5)
)
insert #Something
select 123, 'C12', 'Ac111' union all
select 1222, 'C12', 'Ac111' union all
select 1243, 'C12', 'Ac111' union all
select 433, 'P1', 'Ac111' union all
select 433, 'L1', 'Ac111' union all
select 543, 'C1', 'Ac222' union all
select 544, 'C1', 'Ac222' union all
select 4322, 'P1', 'Ac222' union all
select 4322, 'L1', 'Ac222'
select * from #Something
drop table #Something
So what would the desired output be based on this sample data? And what indicates a "Child"?
_______________________________________________________________
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 Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
November 20, 2013 at 2:18 pm
Hi
Apologies for not following the format. I will keep this in mind next time.
My desired output is
Parent_Count | Child_count
2 7
Thanks
Mita
November 20, 2013 at 2:36 pm
Operating under the assumption that items beginning with L are children:
DECLARE @lines TABLE
(line_no int,
code varchar(3) not null,
account_no varchar(10) not null)
INSERT @lines
SELECT Line_no = 123, code = 'C12', Account_no = 'Ac111' UNION ALL
SELECT 1222, 'C12','Ac111'UNION ALL
SELECT 1243, 'C12','Ac111'UNION ALL
SELECT 433, 'P1','Ac111'UNION ALL
SELECT 433, 'L1','Ac111'UNION ALL
SELECT 543, 'C1','Ac222'UNION ALL
SELECT 544, 'C1','Ac222'UNION ALL
SELECT 4322, 'P1','Ac222'UNION ALL
SELECT 4322, 'L1','Ac222';
SELECT * FROM @lines;
WITH line_count AS
(SELECT code, account_no, CASE WHEN LEFT(code,1) ='P' THEN 'parrent' ELSE 'child' END pc
FROM @lines)
SELECT pc, count(pc) pc_count
FROM line_count
GROUP BY pc
-- Itzik Ben-Gan 2001
November 20, 2013 at 2:42 pm
nutty (11/20/2013)
HiApologies for not following the format. I will keep this in mind next time.
My desired output is
Parent_Count | Child_count
2 7
Thanks
Mita
something like this work?
create table #Something
(
Line_no int,
code varchar(5),
AccountNo char(5)
)
insert #Something
select 123, 'C12', 'Ac111' union all
select 1222, 'C12', 'Ac111' union all
select 1243, 'C12', 'Ac111' union all
select 433, 'P1', 'Ac111' union all
select 433, 'L1', 'Ac111' union all
select 543, 'C1', 'Ac222' union all
select 544, 'C1', 'Ac222' union all
select 4322, 'P1', 'Ac222' union all
select 4322, 'L1', 'Ac222'
select SUM(case when left(code, 1) = 'P' then 1 end) as Parent, SUM(case when left(code, 1) in ('C', 'L') then 1 end) as Parent
from #Something
drop table #Something
_______________________________________________________________
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 Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
November 20, 2013 at 2:43 pm
Oops I didn't see Alan's fine post when I posted my last response. It seems very similar to mine. The main difference is the one I posted will return 2 columns and Alan's returns 2 rows. Pick which way you want the output since you have have two examples. 😀
_______________________________________________________________
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 Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
November 20, 2013 at 2:57 pm
Thanks for this.
I am working in pl/sql and it says 'LEFT' as an invalid identifier
November 20, 2013 at 3:12 pm
nutty (11/20/2013)
Thanks for this.I am working in pl/sql and it says 'LEFT' as an invalid identifier
I don't speak Oracle. This a sql server site so everything around here is sql server syntax. I am certain that oracle has something like left. substring maybe?
_______________________________________________________________
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 Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
November 20, 2013 at 4:01 pm
nutty (11/20/2013)
Thanks for this.I am working in pl/sql and it says 'LEFT' as an invalid identifier
Change the LEFT to a SUBSTRING starting at 1 with a length the same as what the original code has for LEFT.
--Jeff Moden
Change is inevitable... Change for the better is not.
November 20, 2013 at 4:01 pm
Hi Alan
In your query you have not taken into account the code for child
I have a big dataset where we have multiple combination of parent and child codes
thanks
November 20, 2013 at 4:32 pm
Hi Sean
The child account does not have a link code. It only has the child code.
so if I use this query for the whole dataset by just filtering on child code, it gives me the count of all the child codes instead of count of child codes belonging ot a particular account.
I need to write a query which somehow ties the child accounts to their parent account using the common account no.
Does that make sense?
November 20, 2013 at 9:03 pm
nutty (11/20/2013)
Hi GuysI have a data where there are parent and child product.
Each parent has got a unique code(P1) and a link code (L1) on their account
and each child of the parent has got a seperate code (C12) but they share the same account no.
I want to count the parent and children
Here is a sample dataset
Line_no code Account No
123 C12 Ac111
1222 C12 Ac111
1243 C12 Ac111
433 P1 Ac111
433 L1 Ac111
543 C1 Ac222
544 C1 Ac222
4322 P1 Ac222
4322 L1 Ac222
Now if I do a group by, I di get unique parents but not unique children
Please help!!
nutty (11/20/2013)
HiApologies for not following the format. I will keep this in mind next time.
My desired output is
Parent_Count | Child_count
2 7
Thanks
Mita
nutty (11/20/2013)
Hi SeanThe child account does not have a link code. It only has the child code.
so if I use this query for the whole dataset by just filtering on child code, it gives me the count of all the child codes instead of count of child codes belonging ot a particular account.
I need to write a query which somehow ties the child accounts to their parent account using the common account no.
Does that make sense?
Yes but if you follow the progression of your posts, then "no". If you look at your first post and count all of the rows, you end up with 9 rows. 2 Parents and 7 "other" rows which include both Links (Lx rows) and Children (Cx rows). That happens to match exactly what you posted in the second quote above and Sean's code produces that output.
So my question is, according to the data in your original post, what should the output actually look like because, right now, I'm totally confused by your posts. 😉
--Jeff Moden
Change is inevitable... Change for the better is not.
November 21, 2013 at 10:27 am
Yes it does match what I want as output.
However the trouble is, these child codes are common codes for various other parent codes.
For example two different parents can share the same child codes and I have to find out which child code belongs to which parent and the only way i can do is through the account no because account nos will be different for each set of parent and child.
Am I clear this time? sorry to confuse you
November 21, 2013 at 11:54 am
nutty (11/21/2013)
Yes it does match what I want as output.However the trouble is, these child codes are common codes for various other parent codes.
For example two different parents can share the same child codes and I have to find out which child code belongs to which parent and the only way i can do is through the account no because account nos will be different for each set of parent and child.
Am I clear this time? sorry to confuse you
So does the code I posted get you what you need or not? I am quite confused at this point.
_______________________________________________________________
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 Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
November 21, 2013 at 2:36 pm
nutty (11/21/2013)
Yes it does match what I want as output.However the trouble is, these child codes are common codes for various other parent codes.
For example two different parents can share the same child codes and I have to find out which child code belongs to which parent and the only way i can do is through the account no because account nos will be different for each set of parent and child.
Am I clear this time? sorry to confuse you
Ah. Understood. Add some data to the test data generation code on this thread and post it so folks can take a crack at it for you.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 15 posts - 1 through 15 (of 21 total)
You must be logged in to reply to this topic. Login to reply