?
Solved

Collection inside collection in sql server view

Posted on 2014-10-22
9
Medium Priority
?
186 Views
Last Modified: 2016-02-18
Hi.
I have two tables. relation 1:N. when I create a view, I got one record for each item in the N table. How could I get a single record with a collection of the N table inside the first record? Is it possible?
From this view, we will create a stored procedure and mapped it with EF 6 to use into a c# web app.

thx in advance
example.png
0
Comment
Question by:Deraldo Silva
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
9 Comments
 
LVL 51

Expert Comment

by:Vitor Montalvão
ID: 40396532
A GROUP BY clause will do the trick. You just need to decide if you want the MIN or MAX account:
SELECT users.Name, MAX(accounts.Account)
FROM accounts
	INNER JOIN users ON (accounts.userid = users.userid)
GROUP BY users.Name

Open in new window

0
 

Author Comment

by:Deraldo Silva
ID: 40396538
Hi Vitor, thx for your attention.
with this query I got the number of accounts that a user owns. I need this (in c# terms)

public class User
{
  public string Name { get; set; }
  public ICollection<string> accounts { get; set; }
}
or,
[ {"nome1", [{"1"}, {"2"},{"3"},{"4"}]}, {... }]

regards
0
 
LVL 51

Expert Comment

by:Vitor Montalvão
ID: 40396585
Deraldo, I'm afraid I can't help you with C#. I work only with SQL Server. Sorry.
0
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 

Author Comment

by:Deraldo Silva
ID: 40396600
Vitor, I do not have any question about c#, it's an example. because of this, I put two different examples, in  c# and in json.
ok. let me explain better.
My question is about sql server.
as I show in the picture, when i did my view, the sql is

SELECT        dbo.USERS.Name, dbo.ACCOUNTS.Account
FROM            dbo.ACCOUNTS INNER JOIN
                         dbo.USERS ON dbo.ACCOUNTS.UserID = dbo.USERS.UserID

but, with this query I got this result
Nome1 1
Nome1 2
Nome1 3
Nome1 4

If I have several fields in the Users table, these fields will be repeated, for all accounts, right? like this
Nome1 f1 f2 f3 f4 f5 f6 1
Nome1 f1 f2 f3 f4 f5 f6 2 and so on.

what I want: a single record where the accounts is another record set, inside the first one.

I don't know if it is possible.
maybe a select inside a select.
0
 
LVL 51

Expert Comment

by:Vitor Montalvão
ID: 40396609
Deraldo, can you post an example of the output you want?
Maybe it's better for me to understand with an example.
Thanks.
0
 

Author Comment

by:Deraldo Silva
ID: 40396696
thats the problem. I don't know how to show you the example as a sql result. I think that there is no such thing in sql. a recordset inside an record. a list as field element. wow! maybe converting one recordset into a xml value before output. So the user record, with all its fields and one field will be an xml with several accounts inside it.
is it possible with one view? or one stored procedure?

sorry for the inconvenience, this is like an handshake between two different expertises.
0
 
LVL 51

Accepted Solution

by:
Vitor Montalvão earned 2000 total points
ID: 40396702
SQL Server it's good to return data but for formatting I would prefer a program language or a reporting tool.
What I understood is that you want something like:
Nome1 f1 f2 f3 f4 f5 f6 1
                                         2
                                         3
                                         4
Nome2 f1 f3 f5 f7 f8 f9 1
                                         2
Nome3 f0 f2 f4 f6 f8 f9 1
                                         ...
Right?

If so, the only think SQL Server can do is to return it like:
Nome1 f1 f2 f3 f4 f5 f6 1
Nome1 f1 f2 f3 f4 f5 f6 2
Nome1 f1 f2 f3 f4 f5 f6 3
Nome1 f1 f2 f3 f4 f5 f6 4
Nome2 f1 f3 f5 f7 f8 f9 1
Nome2 f1 f3 f5 f7 f8 f9 2
Nome3 f0 f2 f4 f6 f8 f9 1

And then you format it as you need (suppress repeating values?).
0
 

Author Closing Comment

by:Deraldo Silva
ID: 40396727
yeah. that's what I realize.

anyway, thx for your attention.
0
 
LVL 30

Expert Comment

by:Olaf Doschke
ID: 40396788
Linq can do what you need, more precise the linq extension methods, not a linq query.
            using (var entities = new YourDatabaseEntities())
            {
                Users = new Collection<User>(
                    entities.users.Select(usr => new User
                    {
                        Name =  usr.UserName,
                        Accounts = usr.accounts.Select(acc => new Account
                        {
                            AccountNo = acc.AccountNumber
                        })
                    }));
            }

Open in new window

The User class then will need to have a string Name property and a IEnumerable<Account> Accounts property.
 
Bye, Olaf.
0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question