Need help with TSQL proc - tsql

So I have a mapping table of tournaments to teams. All this table has is TeamId and TournamentId.
I have a proc, GetTournamentsByTeam that takes in #TeamId int
Basically I want to get all the tournament records from tblTournament using all the TournamentId's I get from the mapping table where the TeamId = #TeamId.
How would I do this?

What you want to do is an inner join between the tables.
SELECT tournament.*
FROM tblTournament tournament
INNER JOIN tblTournamentTeams tt ON tournament.TournamentId = tt.TournamentId
WHERE tt.TeamId = #TeamId

What you want to do is read any SQL book for beginners. Seriously.

Related

JPA query using named parameter from a oneToOne joined table

I am trying to create a query that references fields in the WHERE from both the current table and a table joined on a column. I have no problems creating a query from a single parameter in the Participation table.
#NamedQuery(name="Participation.byUserID", query="SELECT c FROM Participation c WHERE c.userID = :userID")
Here is the fun part... Within the Participation entity, I have a join:
#OneToOne(optional=false)
#JoinColumn(name = "EventID", insertable = false, updatable = false)
private Event event;
The Event entity has some fields I would like to use in my query. For example, eventDate or eventType.
I am wanting something like WHERE Participation.userID = 123 AND Event.eventType = "meeting" using JPA queries instead of SQL. How can I expand the simple named query above to include comparisons from the joined table?
So far I have not been able to get this working, so any help would be appreciated.
This is a really simple requirement, I suggest you go through some basic JPA tutorials.
SELECT c FROM Participation c WHERE c.userID = :userID and c.event.eventType = :eventType

Copy SQL result into another table

I need some help. I'm trying to copy a tsql query result into another table. I was able to do it with the below tsql but I need to put some sort of check method to not copy a record if it already exist in the "PageControls" table.
INSERT INTO PageControls (UserId, PageId)
SELECT t1.UserId, t2.PageId FROM
aspnet_users t1, Pages t2
How can I accomplish this?
Thank you.
It looks like you're trying to populate the pagecontrols table with a cartesian product of users and pages. Assuming that's your goal, then you can add not exists to your query to exclude those already in the pagecontrols table:
INSERT INTO PageControls (UserId, PageId)
SELECT t1.UserId, t2.PageId
FROM aspnet_users t1, Pages t2
WHERE NOT EXISTS (
SELECT *
FROM PageControls p
WHERE p.userid = t1.userid and p.pageid = t2.pageid
)
SQL Fiddle Demo

EF Subquery (where column in first query includes values in another list)

hoping an EF/linq guru out there can help me out with what should be a pretty simple query (as it would be in SQL). Here is the pseudocode of what I am trying to do:
QUERY A:
var query1 = FROM tableA in Models.TableAs
select tableA
QUERY B:
var query2 = FROM tableB in Models.TableBs
WHERE tableB.ColumnA IN (query1.results.columnB)
My actual queries are much more complicated and cannot be represented by a simple join. I need to actually run the first query to basically get a list of values to use to filter the values down in the second query. Anyone have any ideas?
Thanks!
Something like this maybe
var query1 = from tableA in Models.TableAs
select tableA
var query2 = from tableB in Models.TableBs
where query1.Any(ta=>tableB.ColumnA==ta.ColumnB);
You're looking at this from the wrong direction for Entity Framework.
In SQL, you'd write stuff like:
SELECT * FROM TABLEA
WHERE TABLEA.COLUMNA IN (TABLEB.COLUMNB)
using IN.
If we have models like this:
public class TableA
{
public TableBEntity TableB {get;set;}
}
public class TableB
{
public IEnumerable<TableAEntity> TableA {get;set;}
}
In EF, try this way:
from myvar in context.TableB select myvar.TableA
I believe either jure's query or this query will get you the same results, but I think they will generate different SQL and one will probably be faster than the other. You might try both to see which one gives better performance.
var query1 = from tableA in Models.TableAs
select tableA.columnB;
var query2 = from tableB in Models.TableBs
where query1.Contains(tableB.ColumnA)
select tableB;

JPA Query over a join table

I have 3 tables like:
A AB B
------------- ------------ ---------------
a1 a1,b1 b1
AB is a transition table between A and B
With this, my classes have no composition within these two classes to each other. But I want to know that , with a JPQL Query, if any records exist for my element from A table in AB table. Just number or a boolean value is what I need.
Because AB is a transition table, there is no model object for it and I want to know if I can do this with a #Query in my Repository object.
the AB table must be modeled in an entity to be queried in JPQL. So you must model this as
an own entity class or an association in your A and or your B entity.
I suggest to use Native query method intead of JPQL (JPA supports Native query too). Let us assume table A is Customer and table B is a Product and AB is a Sale. Here is the query for getting list of products which are ordered by a customer.
entityManager.createNativeQuery("SELECT PRODUCT_ID FROM
SALE WHERE CUSTOMER_ID = 'C_123'");
Actually, the answer to this situation is simpler than you might think. It's a simple matter of using the right tool for the right job. JPA was not designed for implementing complicated SQL queries, that's what SQL is for! So you need a way to get JPA to access a production-level SQL query;
em.createNativeQuery
So in your case what you want to do is access the AB table looking only for the id field. Once you have retrieved your query, take your id field and look up the Java object using the id field. It's a second search true, but trivial by SQL standards.
Let's assume you are looking for an A object based on the number of times a B object references it. Say you are wanting a semi-complicated (but typical) SQL query to group type A objects based on the number of B objects and in descending order. This would be a typical popularity query that you might want to implement as per project requirements.
Your native SQL query would be as such:
select a_id as id from AB group by a_id order by count(*) desc;
Now what you want to do is tell JPA to expect the id list to comeback in a form that that JPA can accept. You need to put together an extra JPA entity. One that will never be used in the normal fashion of JPA. But JPA needs a way to get the queried objects back to you. You would put together an entity for this search query as such;
#Entity
public class IdSearch {
#Id
#Column
Long id;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
Now you implement a little bit of code to bring the two technologies together;
#SuppressWarnings("unchecked")
public List<IdSearch> findMostPopularA() {
return em.createNativeQuery("select a_id as id from AB group by a_id
order by count(*) desc", IdSearch.class).getResultList();
}
There, that's all you have to do to get JPA to get your query completed successfully. To get at your A objects you would simply cross reference into your the A list using the traditional JPA approach, as such;
List<IdSearch> list = producer.getMostPopularA();
Iterator<IdSearch> it = list.iterator();
while ( it.hasNext() ) {
IdSearch a = it.next();
A object = em.find(A.class,a.getId());
// your in business!
Still, a little more refinement of the above can simplify things a bit further actually given the many many capabilities of the SQL design structure. A slightly more complicated SQL query will an even more direct JPA interface to your actual data;
#SuppressWarnings("unchecked")
public List<A> findMostPopularA() {
return em.createNativeQuery("select * from A, AB
where A.id = AB.a_id
group by a_id
order by count(*) desc", A.class).getResultList();
}
This removes the need for an interm IdSearch table!
List<A> list = producer.getMostPopularA();
Iterator<A> it = list.iterator();
while ( it.hasNext() ) {
A a = it.next();
// your in business!
What may not be clear tot the naked eye is the wonderfully simplified way JPA allows you to make use of complicated SQL structures inside the JPA interface. Imagine if you an SQL as follows;
SELECT array_agg(players), player_teams
FROM (
SELECT DISTINCT t1.t1player AS players, t1.player_teams
FROM (
SELECT
p.playerid AS t1id,
concat(p.playerid,':', p.playername, ' ') AS t1player,
array_agg(pl.teamid ORDER BY pl.teamid) AS player_teams
FROM player p
LEFT JOIN plays pl ON p.playerid = pl.playerid
GROUP BY p.playerid, p.playername
) t1
INNER JOIN (
SELECT
p.playerid AS t2id,
array_agg(pl.teamid ORDER BY pl.teamid) AS player_teams
FROM player p
LEFT JOIN plays pl ON p.playerid = pl.playerid
GROUP BY p.playerid, p.playername
) t2 ON t1.player_teams=t2.player_teams AND t1.t1id <> t2.t2id
) innerQuery
GROUP BY player_teams
The point is that with createNativeQuery interface, you can still retrieve precisely the data you are looking for and straight into the desired object for easy access by Java.
#SuppressWarnings("unchecked")
public List<A> findMostPopularA() {
return em.createNativeQuery("SELECT array_agg(players), player_teams
FROM (
SELECT DISTINCT t1.t1player AS players, t1.player_teams
FROM (
SELECT
p.playerid AS t1id,
concat(p.playerid,':', p.playername, ' ') AS t1player,
array_agg(pl.teamid ORDER BY pl.teamid) AS player_teams
FROM player p
LEFT JOIN plays pl ON p.playerid = pl.playerid
GROUP BY p.playerid, p.playername
) t1
INNER JOIN (
SELECT
p.playerid AS t2id,
array_agg(pl.teamid ORDER BY pl.teamid) AS player_teams
FROM player p
LEFT JOIN plays pl ON p.playerid = pl.playerid
GROUP BY p.playerid, p.playername
) t2 ON t1.player_teams=t2.player_teams AND t1.t1id <> t2.t2id
) innerQuery
GROUP BY player_teams
", A.class).getResultList();
}

Self m:n Relation

I have persons and a person can contact multiple other persons, so basically the "default" tables would be:
persons (id)
contacts (person1_id, person2_id)
With this schema, I'd have to issue queries like
SELECT *
FROM contacts c
WHERE ( person1_id = *id of person1* AND person2_id = *id of person2* )
OR
( person1_id = *id of person2* AND person2_id = *id of person1* )
to get the relation between two persons when I insert such a relation only once.
What is the common practice to deal with this situation?
Insert data once and do such an OR query
Insert the relation twice so that person1_id = id of person1 AND person2_id = id of person2 is enough
An entirely different approach?
Assuming:
The m:n table actually contains additional data, so if I create a relation for both ways, I'd have to duplicate the data
This is a core part of the application and most non-trivial queries involve at least a sub query that determines whether or not such a relation exists
If you write your insert logic such that person1_id < person2_id is true for all rows, then you can just write
SELECT *
FROM contacts c
WHERE person1_id = min(*id_of_person_1*, *id_of_person_2*)
AND person2_id = max(*id_of_person_1*, *id_of_person_2*)
Why don't you use Join between the tables?
something like this:
SELECT *
FROM contact c INNER JOIN person p ON p.id = c.person1_id
The the where and group bys you need to complete you're query =)
Take a look here how the results will be showed:
http://www.w3schools.com/Sql/sql_join_inner.asp
Regards,
Elkas
Try this one mate =)
SELECT c.person1_id as id_person_1, c.person2_id as id_person_2, p1.name as name_person_1, p2.name as name_person_2
FROM contact c
LEFT JOIN person p1 ON p1.id = c.person1_id
RIGHT JOIN person p2 ON p2.id = c.person2_id;
I don't know if it will work.. but give it try mate =)
"Insert the relation twice so that person1_id = id of person1 AND person2_id = id of person2 is enough"
That is how I'd do it, personally. It allows to deal with the situation where A has the contact details of B but not the other way around (e.g. a girl gives a guy her number at the bar saying "call me" as she walks out). It also makes the queries simpler.