Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 123»»»

Count for each account no -parent and childsql Expand / Collapse
Author
Message
Posted Wednesday, November 20, 2013 1:49 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Tuesday, February 25, 2014 1:15 PM
Points: 13, 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!!
Post #1516191
Posted Wednesday, November 20, 2013 2:05 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 6:31 PM
Points: 13,207, Visits: 12,688
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 Moden's 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)
Post #1516200
Posted Wednesday, November 20, 2013 2:18 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Tuesday, February 25, 2014 1:15 PM
Points: 13, 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
Post #1516207
Posted Wednesday, November 20, 2013 2:36 PM


SSChasing Mays

SSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing Mays

Group: General Forum Members
Last Login: Today @ 6:57 PM
Points: 632, Visits: 2,950
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



Read this article for best practices on asking questions.
Need to split a string? Try this (Jeff Moden)
Need a pattern-based string spitter? Try this (Dwain Camps)

"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

My blog
Post #1516221
Posted Wednesday, November 20, 2013 2:42 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 6:31 PM
Points: 13,207, Visits: 12,688
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 Moden's 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)
Post #1516222
Posted Wednesday, November 20, 2013 2:43 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 6:31 PM
Points: 13,207, Visits: 12,688
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 Moden's 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)
Post #1516224
Posted Wednesday, November 20, 2013 2:57 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Tuesday, February 25, 2014 1:15 PM
Points: 13, Visits: 46
Thanks for this.
I am working in pl/sql and it says 'LEFT' as an invalid identifier
Post #1516231
Posted Wednesday, November 20, 2013 3:12 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 6:31 PM
Points: 13,207, Visits: 12,688
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 Moden's 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)
Post #1516234
Posted Wednesday, November 20, 2013 4:01 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 10:48 PM
Points: 35,547, Visits: 32,134
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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1516245
Posted Wednesday, November 20, 2013 4:01 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Tuesday, February 25, 2014 1:15 PM
Points: 13, 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
Post #1516246
« Prev Topic | Next Topic »

Add to briefcase 123»»»

Permissions Expand / Collapse