Can I write this query using the criteria API or am I stuck with HQL? - nhibernate

I have the following query which I would like to write using the criteria api of NH.
select status, count(1) from (select distinct Status, post_id from post_statistics) tbl group by status
each post_id can exist multiple times in post_statistics
e.g.
id post_id status
1 1 open
1 1 edit
1 1 open
1 2 open
so the query should return the following results:
status count
open 2
edit 1
thx in advance.

mappings, classes?
if i'm thinking right, with a proper mapping this would effectively be something like
(pseudo-HQL)
select stat.Name, count(stat.Posts) from status stat

Related

Check condition within table possibly self-join?

I have a users table with many columns however I am trying to use the below 2 columns to fetch the users do not have expired = 1
The query should return only user 2. Any help is much appreciated.
user_id expired
1 1
1 0
2 0
2 0
3 1
I know you have an answer, but if you are just looking for a list of single user_id's this might work a little more efficiently:
select user_id
from users
GROUP BY user_id
having MAX(expired) = 0
You can do this using not in like below :
select * from users where user_id
not in(select distinct user_id from users where expired = 1);
See SQL Here

Need sql query to pull back data that meets several groups of criteria from same table in one query

I need to write an sql query that will pull back the data that meets several groups of criteria from the same table. The easiest way to describe is to imagine using an SQL "in" clause but instead of the internals of that clause being "or"s joining the parameters you want it to match it is instead an "and".
I attempted to use count to verify the correct amount of data was pulled back for each "in" statement but the count can't always be trusted due to other entries being similar for each column.
A sample table might be this:
id count animal
--- ----- ------
1 5 puppy
1 6 cat
1 6 puppy
So, now I need a query that will pull back all entries with an id of 1 and a count of 5 and 6 and an animal of puppy and cat. I pretty much need to verify the entire path of the table entry to know I want to pull it back. Is there any built in function that can do this? Do I need to use a recursive CTE to dig deep after confirming that one set of criteria is met? Thanks for any help.
If I got it right
with cnt as(
select id
from tbl
where [count] in (5,6) and animal in ('puppy', 'cat')
group by id
having count(distinct[count])=2 and count(distinct animal)=2
)
select id, [count], animal
from tbl
where id in (select id from cnt);
It's kind of confusing what you're looking for exactly but can you not use or's and ands?
select id, count, animal
from table
where id = 1 and
(count = 5 or count = 6) and
(animal = puppy or anmial = cat)
I think you just want:
select t.*
from t
where id = 1 and
count in (5, 6) and
animal in ('puppy', 'cat');
EDIT:
If you want them all in the same row, just rearrange the conditions:
select t.*
from t
where id = 1 and
( (count = 5 and animal = 'puppy') or
(count = 6 and animal = 'cat')
);

How do I generate a table of IDs which have only one attribute each?

I have a table that looks like this
id attribute
1 a
1 a
2 b
2 a
And I want to collect all of the IDs which have ONLY attribute a. So in the example case:
id
1
My initial thought was to use a where, but that would return:
id
1
1
2
Because 2 also has an "a" attribute in one instance.
P.S. I realize the phrasing of the title is ambiguous; maybe there's a better term than attribute to use in this case?
ohh I just saw hive but this is pretty standard sql give it a try.
SELECT
ID
FROM
TABLENAME
GROUP BY
ID
HAVING
COUNT(DISTINCT attribute) = 1
Having is like a where statement after the GROUP BY aggregation has occurred.
HiveQL equivalent of SQL using group by ,having and distinct
select id from (select id,count(distinct attribute) cnt from table_actual group by id having cnt='1') tableouter;

Getting the newest revision from a table via SQL

I have a database that looks like this:
ID parent name description _record_status _log_user _log_timestamp _log_type
--------------------------------------------------------------------------------------------------------------------
1 1 This is a rwo! Test content active 1 2012-01-29 15:49:21 create
2 1 This is a row! Test Content active 1 2012-01-29 15:52:14 revision
3 3 Another record! More content active 1 2012-01-29 15:58:43 create
4 4 Deleted Record More content active 1 2012-01-29 15:58:43 create
5 4 Deleted Record More content deleted 1 2012-01-29 15:58:43 destroy
I want to be able to select the newest row for each record, where the record isn't deleted. So for example, the output I'd expect is:
ID name
------------------
2 This is a row!
3 Another Record!
Is there a way to do this via SQL that is efficient, and if not, what might I want to do in PHP to accomplish this?
Would having a separate version of each table for revisions be the way to go here?
You'd get the highest record per parent first, then excluded anything that has been deleted:
SELECT YourTable.ID, YourTable.name
FROM YourTable INNER JOIN (
SELECT parent, MAX(_log_timestamp) MaxLogTS
FROM YourTable
GROUP BY parent) T ON YourTable.parent = T.parent
AND YourTable._log_timestamp = T.MaxLogTS
WHERE YourTable._record_status != 'deleted'
This can be slightly optimized if you know that ID values are always in ascending time order. Then you could base the MAX record comparison on ID rather than a date and time value.
Assuming you are using MySQL, try this:
select * from
(select id, name, _record_status from
(select parent, id, name, _record_status
from your_table
order by parent, _log_timestamp desc) v
group by parent) q
where _record_status <> 'deleted'

SQL Select Entire Row by Distinct Columns

I need an sql statement which will allow me to select an entire row from oracle database, but by distinct columns. Here is a simplified database:
Project_Num Title Category
0 Project 1 Admin
0 Project 1 Development
1 Project 2 Admin
2 Project 3 Development
I need my statement to return the following result set:
0 Project 1 Admin
1 Project 2 Admin
2 Project 3 Development
So each project is returned based on whether its project_num and title are unique. If a project has 2+ entries where its category is different, I need to only select that project once (it doesn't matter which entry I select for that project).
Can anyone help me please?
SELECT Project_Num, Title, MIN(Category) AS Category
FROM MyTable
GROUP BY Project_Num, Title;
Do you even need to have the Category column in your result set?
If not then you could just use:
SELECT DISTINCT Project_Num, Title
FROM MyTable
SELECT *
FROM (SELECT DISTINCT YourDistinctField FROM YourTable) AS A
CROSS APPLY
( SELECT TOP 1 * FROM YourTable B
WHERE B.YourDistinctField = A.YourDistinctField ) AS NewTableName
I have been trying to figure this out for hours now, I tried lots of solutions that didn't work and finally got it by searching for "joining top 1" and adapting that solution I found to a distinct search.