SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Count for each account no -parent and childsql


Count for each account no -parent and childsql

Author
Message
nutty
nutty
SSC Rookie
SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)

Group: General Forum Members
Points: 35 Visits: 46
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!!
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)

Group: General Forum Members
Points: 27252 Visits: 17557
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.

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)
nutty
nutty
SSC Rookie
SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)

Group: General Forum Members
Points: 35 Visits: 46
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
Alan.B
Alan.B
SSCertifiable
SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)

Group: General Forum Members
Points: 5454 Visits: 7736
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



-- Alan Burstein



Best practices for getting help on SQLServerCentral
Need to split a string? Try DelimitedSplit8K or DelimitedSplit8K_LEAD (SQL 2012+)
Need a pattern-based splitter? Try PatternSplitCM
Need to remove or replace those unwanted characters? Try PatExclude8K and PatReplace8K.

"I can't stress enough the importance of switching from a 'sequential files' mindset to 'set-based' thinking. After you make the switch, you can spend your time tuning and optimizing your queries instead of maintaining lengthy, poor-performing code. " -- Itzek Ben-Gan 2001
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)

Group: General Forum Members
Points: 27252 Visits: 17557
nutty (11/20/2013)
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


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.

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)
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)

Group: General Forum Members
Points: 27252 Visits: 17557
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. :-D

_______________________________________________________________

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.

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)
nutty
nutty
SSC Rookie
SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)

Group: General Forum Members
Points: 35 Visits: 46
Thanks for this.
I am working in pl/sql and it says 'LEFT' as an invalid identifier
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)

Group: General Forum Members
Points: 27252 Visits: 17557
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.

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)
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)

Group: General Forum Members
Points: 90840 Visits: 41150
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

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.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
nutty
nutty
SSC Rookie
SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)

Group: General Forum Members
Points: 35 Visits: 46
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
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