Master-detail Filtering in SQL - sql

I have an interesting query that I would like to get done:
1. I have an Article table [ArticleId, ArticleName]
2. An Article has an ArticleCheckin (1:1) [ArticleId, CheckinName]
3. An Article can have multiple ArticleResources [ArticleResourceId, ArticleId, ResourceName]
I would like to write a query that gets me a list of Articles with Checkin (if exists) and a list of the Article Resources for each Article in the same query.
Currently, I obtain the Article and am querying the Resources again for each article and that doesn't seem like the right way to do this.
Any help would be great!

AFAIK a query will always give you a flattened out result. This means that in order to get a list of Articles and lists of corresponding ArticleResources, you'll need to loop through each Article like you are doing now.
But you are not saying anything about which technology you are using, so it is hard to suggest how you can improve on your query/code.

This query will give you all articles with or without checkins but only if there are article resources (LEFT JOIN otherwise).
SELECT A.*, AC.*, AR.*
FROM Article A
LEFT JOIN ArticelCheckin AC ON AC.ArticleId = A.ArticleId
INNER JOIN ArticleResources AR ON AR.ArticleId = A.ArticleId

Related

Many to many relation, multi where clause on the same column and hibernate

Sorry for the bad question title, couldnt think of anything better.
Anyway, my tables are Tags - Poststags - Posts. Poststags is a junction table for many to many relation. I need to select all posts with given tags, I dont know how many tags the user will choose to search for. One way I found to do this is in the code below, however i would need to loop all the tags given by the user and construct the query string from there since the number of tags is unknown. Seems like a pretty bad solution to me.
Another solution would be to store all tags in one column in the Posts table as a pure string, but I dont want to do that because of other application requirements.
I have a working sql query, since I was trying pure sql before trying to implement it in hibernate, but I dont like doing a select of all posts containing each tag and then joining each query, is there a way to specify the same column multiple times in the WHERE clause? Something along the lines WHERE pt.tag_id = x AND pt.tag_id = y? (I know this won't work). IN operator won't work either since it will give me Posts that contain any of the supplied tags and not just the posts containing ALL of the supplied tags.
Also how would I implement such a query in HQL(if subqueries like this are even supported?). Or can I somehow manage this via criteria? Or do I have to resort to using createSQLQuery method of a hibernate session?
SELECT * FROM
( SELECT * FROM posts p
inner join poststags pt on pt.post_id = p.id
WHERE pt.tag_id = 1 ) AS A
INNER JOIN
( SELECT * FROM posts p
inner join poststags pt on pt.post_id = p.id
WHERE pt.tag_id = 2 ) AS B ON A.id = B.id
And yes, I know this query is not returning the Post entity itself, but I can handle that later.
Don't use hibernate or ORM for this kind of complex select, it may work, but in a bad way.
Your use case should be solved by full text search, which means each Post will need have its own tags.
I don't see much value to make Tag an entity. It's just a string.
Full text search could be heavy for database , A better way is using elasticsearch to help. Spring has integration with spring-data-elasticsearch and it's not difficult to use. Elasyicsearch is very powerful for free text search.
Here is a solution that 'should' work using Criteria queries in Hibernate.
Assuming that you have an entity for Post and an entity for PostTag and PostTag has reference to Post (which I think it should given the example query that you provided), I believe that something like this should do what you want:
static DetachedCriteria getPostTagCriteria(String tagString)
{
DetachedCriteria criteria = DetachedCriteria.forClass(PostTag.class, "uniqueName_" + postTagId);
criteria.createAlias("tag", "tag");
criteria.add(Restrictions.eq("tag.tagString", tagString));
criteria.setProjection(Projections.property("postId"));
return criteria;
}
static List<Post> getPosts(List<String> tagStrings)
{
Criteria criteria = getCurrentSession().createCriteria(Post.class, "post");
for(String tagString : tagStrings)
{
criteria.add(Property.forName("post.id").in(getPostTagCriteria(tagString)));
}
List<Post> ret = criteria.list();
return ret;
}
This assumes that you have reasonable entities to represent Post, PostTag and Tag and that they all reference each other in obvious parent/child sort of ways that I have completely made up here.
But, the general idea of creating multiple detached criteria objects based on your input should solve your problem. This solution also comes with the same caveats regarding SQL complexity mentioned above. You will be creating a sub-query for each tag passed in. So, depending on your indexes and table sizes, you may need to consider a different approach.

Write an SQL write on a List property

I have the following tables Situation:
A Blog contains many Posts. A Post has an Author.
I want to write an SQL query that: returns all the unique blogs that have at least 3 posts by the author "Janet";
I am no sure how to do this as I usually work with EntityFramework. Any help would be great.
**EDIT
I tried this:
SELECT auth.name
FROM Blog blo
LEFT JOIN Auth auth
WHERE blo.authname = "janet"
Select distinct Blogs from tblblogs,tblposts,tblauthor where tblblogs.id=tblposts.blogid and tblposts.id=tblauthor.postid and tblautor.name='Janet'
group by blogs
having count(tblposts.id)>=3
This is a rough query because you didn't mention the table structure. If you want more elaboration, kindly mention table structure

How to create a faceted search with SQL Server

I have an application which I will be accessing SQL server to return data which has been filtered by selections from the application as any common faceted search. I did see some out the box solutions, but these are expensive and I prefer building out something custom, but just don't know where to start.
The database structure is like this:
The data from the PRODUCT table would be searched by tags from the TAG table. Values which would be found in the TAG table would be something like this:
ID NAME
----------------------
1 Blue
2 Green
3 Small
4 Large
5 Red
They would be related to products through the ProductTag table.
I would need to return two groups of data from this setup:
The Products that are only related to the Tags selected, whether single or multiple
The Remaining tags that are also available to select for the products which have already been refined by single or multiple selected tags.
I would like this to be all with-in SQL server if possible, 2 seperate as stored procedures.
Most websites have this feature built into it these days, ie: http://www.gnc.com/family/index.jsp?categoryId=2108294&cp=3593186.3593187 (They've called it 'Narrow By')
I have been searching for a while how to do this, and I'm taking a wild guess that if a stored procedure has to be created in this nature, that there would need to be 1 param that accepts CSV values, like this:
[dbo].[GetFacetedProducts] #Tags_Selected = '1,3,5'
[dbo].[GetFacetedTags] #Tags_Selected = '1,3,5'
So with this architecture, does anyone know what types of queries need to be written for these stored procedures, or is the architecture flawed in any way? Has anyone created a faceted search before that was like this? If so, what types of queries would be needed to make something like this? I guess I'm just having trouble wrap my head around it, and there isn't much out there that shows someone how to make something like this.
A RDBMS for being used for faceted searching is the wrong tool for the job at hand. Faceted searching is a multidimensional search, which is difficult to express in the set-based SQL language. Using a data-cube or the like might give you some of the desired functionality, but would be quite a bit of work to build.
When we were faced with similar requirements we ultimately decided to utilize the Apache Solr search engine, which supports faceting as well as many other search-oriented functions and features.
It is possible to do faceted search in SQL Server. However don't try to use your live product data tables. Instead create a de-normalised "fact" table which holds every product (rows) and every tag (columns) so that the intersection is your product-tag values. You can re-populate this periodically from your main product table.
It is then straightforward and relatively efficient to get the facet counts for the matching records for each tag the user checks.
The approach I have described will be perfectly good for small cases, e.g. 1,000 product rows and 50-100 tags (attributes). Also there is an interesting opportunity with the forthcoming SQL Server 2014, which can place tables in memory - that should allow much larger fact tables.
I have also used Solr, and as STW points out this is the "correct" tool for facet searches. It is orders of magnitude faster than a SQL Server solution.
However there are some major disadvantages to using Solr. The main issue is that you have to setup not only another platform (Solr) but also all the paraphernalia that goes with it - Java and some kind of Java servlet (of which there are several). And whilst Solr runs on Windows quite nicely, you will still soon find yourself immersed in a world of command lines and editing of configuration files and environment variables that will remind you of all that was great about the 1980s ... or possibly not. And when that is all working you then need to export your product data to it, using various methods - there is a SQL Server connector which works fairly well but many prefer to post data to it as XML. And then you have to create a webservice-type process on your application to send it the user's query and parse the resulting list of matches and counts back into your application (again, XML is probably the best method).
So if your dataset is relatively small, I would stick with SQL Server. You can still get a sub-second response, and SQL 2014 will hopefully allow much bigger datasets. If your dataset is big then Solr will give remarkably fast results (it really is very fast) but be prepared to make a major investment in learning and supporting a whole new platform.
There's other places where you can get examples of turning a CSV parameter into a table variable. Assuming you have done that part your query boils down to the following:
GetFacetedProducts:
Find Product records where all tags passed in are assigned to each product.
If you wrote it by hand you could end up with:
SELECT P.*
FROM Product P
INNER JOIN ProductTag PT1 ON PT1.ProductID = P.ID AND PT1.TagID = 1
INNER JOIN ProductTag PT2 ON PT1.ProductID = P.ID AND PT1.TagID = 3
INNER JOIN ProductTag PT3 ON PT1.ProductID = P.ID AND PT1.TagID = 5
While this does select only the products that have those tags, it is not going to work with a dynamic list. In the past some people have built up the SQL and executed it dynamically, don't do that.
Instead, lets assume that the same tag can't be applied to a product twice, so we could change our question to:
Find me products where the number of tags matching (dynamic list) is equal to the number of tags in (dynamic list)
DECLARE #selectedTags TABLE (ID int)
DECLARE #tagCount int
INSERT INTO #selectedTags VALUES (1)
INSERT INTO #selectedTags VALUES (3)
INSERT INTO #selectedTags VALUES (5)
SELECT #tagCount = COUNT(*) FROM #selectedTags
SELECT
P.ID
FROM Product P
JOIN ProductTag PT
ON PT.ProductID = P.ID
JOIN #selectedTags T
ON T.ID = PT.TagID
GROUP BY
P.ID,
P.Name
HAVING COUNT(PT.TagID) = #tagCount
This returns just the ID of products that match all your tags, you could then join this back to the products table if you want more than just an ID, otherwise you're done.
As for your second query, once you have the product IDs that match, you want a list of all tags for those product IDs that aren't in your list:
SELECT DISTINCT
PT2.TagID
FROM aProductTag PT2
WHERE PT2.ProductID IN (
SELECT
P.ID
FROM aProduct P
JOIN aProductTag PT
ON PT.ProductID = P.ID
JOIN #selectedTags T
ON T.ID = PT.TagID
GROUP BY
P.ID,
P.Name
HAVING COUNT(PT.TagID) = #tagCount
)
AND PT2.TagID NOT IN (SELECT ID FROM #selectedTags)

Rails double match from has_and_belongs_to_many

Say that I have a has_and_belongs_to_many relationship where I have posts and categories. It is simple to find all the posts in a category, or all the categories that a particular post is a member of. However, what if I want to find a list of posts that belong to multiple categories? For example, a list of posts that are on the topic of security in Rails, I might want the posts that belong to the categories "Security" and "Rails".
Is it possible to do this with the finder methods build into ActiveRecord, or will I need to use SQL? Can someone please explain how?
You can use includes or joins, like:
#result = Post.includes(:categories).where("categories.name = 'Security' OR categories.name = 'Rails'")
or
#result = Post.joins(:categories).where("categories.name = 'Security' OR categories.name = 'Rails'")
I also suggest to check this railscast to understand the difference between joins and includes, so you can decide what is better in your case.
i don't know anything about rails, but i'm attempting a similar thing with some sql. this may or may not work for either of us....
i have a table of articles, and a look-up table of applied categories. to get an article that has the 'security' category and the 'rails' category, i'm joining the article table to the category table, of course, but also re-joining it a second time. each join of the category table uses a hint in the table alias name (ie language or topic)
pseudo code:
SELECT article.*,
category_language.category_id,
category_topic.category_id
FROM category category_language
INNER JOIN article ON category_language.articleID = article.articleID
INNER JOIN category category_topic ON article.articleID = category_topic.articleID
WHERE category_language.category_id in (420) /* rails */
and category_topic.category_id in (421) /* security */
this isn't completely ironed out, and i hope that if i am showing my ignorance here, someone will speak up.

MySQL: Limit output according to associated ID

So here's my situation. I have a books table and authors table. An author can have many books... In my authors page view, the user (logged in) can click an author in a tabled row and be directed to a page displaying the author's books (collected like this URI format: viewauthorbooks.php?author_id=23), very straight forward... However, in my query, I need to display the books for the author only, and not all books stored in the books table (as i currently have!) As I am a complete novice, I used the most simple query of:
SELECT * FROM books
This returns the books for me, but returns every single value (book) in the database, and not ones associated with the selected author. And when I click a different author the same books are displayed for them...I think everyone gets what I'm trying to achieve, I just don't know how to perform the query. I'm guessing that I need to start using more advanced query clauses like INNER JOIN etc. Anyone care to help me out :)
Enters the WHERE clause:
The WHERE clause is used to extract only those records that fulfill a specific criteria. In your case, all you need to do is:
SELECT * FROM tasks_tb WHERE author_id = '23';
You will obviously need to change the '23' with the value passed in the URL querystring, so that each page lists the books of each relevant author.
Since it is never too early to start reading about best practices, note that for public websites it is really dangerous to include any un-sanitized input into an SQL query. You may want to read further on this topic from the following Stack Overflow posts:
XKCD sql injection - please explain (with pictures!)
What is SQL injection?
Is SQL injection a risk today?
SQL Injection Topics on Stack Overflow
The basic syntax is
SELECT * FROM table WHERE column=value;
But you really need to study more SQL, I'd suggest going through sqlzoo tutorial. http://sqlzoo.net/