I have the following table structure and I want a turn the query into a NH criteria but I'm not sure how to incorporate the correct 'Projection', does anyone know how?
And the query I want to turn into a Criteria:
select ComponentId from Table_1
where [Name] = 'Contact' or [Name] = 'CurrencyPair'
group by ComponentId
having count(VersionId) = 2
I came across this question when trying to find a solution to my problem.
This post was useful.
In my case I was trying to return all records which have all the sub records the user specifies.
I ended up using a sub query which filters the sub records to the ones the user specified and returns the count, and checking that the count of the sub query equals the number of sub records the user specified.
Bit different from your scenario, but hope helps.
Related
I'm trying to find out which partners has not paid the monthly tuition in a particular month.
I have a table called Socios containing all partners names SocioNome and another table called RegistroPagamento contaning all payments done (This particular table is fulfilled by a form where the user input the Partner Name, Amount Paid and which particular month/year the payment is related to).
I have created a query where I used the SQL code below:
SELECT [SocioNome]
FROM [Socios] NOT IN
(SELECT [SocioNome] FROM [RegistroPagamento] WHERE [MesBoleto] = [Forms]![Selecionar_MCobranca]![TBoxMes] AND [AnoBoleto] = [Forms]![Selecionar_MCobranca]![TBoxAno]);
[Selecionar_MCobranca] is the form I have mentioned before and the [TBoxMes] & [TBoxAno] are the combo boxes from the form which the user can select the month and the year the payment refers to.
When I run the code, a error message pops up indicating that there is a FORM clause syntax issue, and I don't know exactly what is causing the problem.
NOT IN is a comparison operator in the WHERE clause. It does not belong in the FROM cluase. I strongly recommend using NOT EXISTS instead. The idea is:
SELECT s.SocioNome
FROM Socios as s
WHERE NOT EXISTS (SELECT 1
FROM RegistroPagamento as rp
WHERE rp.MesBoleto = [Forms]![Selecionar_MCobranca]![TBoxMes] AND
rp.AnoBoleto = [Forms]![Selecionar_MCobranca]![TBoxAno] AND
rp.SocioNome = s.SocioNome
);
NOT IN returns no rows if any row in the subquery is NULL. To protect against this, just use NOT EXISTS. It has the expected behavior in this case.
Good day all, hope you can help!
I have been toiling with this Query for a while now, and I am sure it will be relatively simple to fix!
I use the query to return all of the values from one table, along with a count of votes from one table, and a count of comments from another.
I have got it to work for loading an individual record
DCount('[query_id]','[comments]','[query_id]=" & Target & "')
However if I add something similar to the query that returns every query_id, the count shows the same for them all.
Is there a different function I can use than DCount to achieve this?
My previous issue was using count, and as the query had non unique data it was counting all votes from a person (i.e if I had made 6 votes, the count would show as 6 for any record my user id was attached to)
Happy to provide any further detail regarding the query.
You may be after something like:
DCount("*","[comments]","[person_id] = " & [Target] & "")
where [Target] is the field holding the PersonId of the other table.
I perhaps didn't ask the question in the best possible way, but I have managed to get the results I expected by using sub select statements in the SELECT clause
Just in case someone else doesn't know how to word or ask a question, Andre posted a very helpful comment to my original question above, advising the following link.
How to ask a good SQL question
SELECT issues.query_id, issues.query_raised_by, issues.query_raised_date, issues.query_summary, issues.query_status, issues.query_status_date, issues.query_detail, issues.query_response
(SELECT COUNT(*) FROM vote WHERE query_id = issues.query_id) AS voteCount,
(SELECT COUNT(*) FROM comments WHERE query_id = issues.query_id) AS commentCount
Hello I have a question regarding a query I am trying to create. I want to create a query that will List members'name for who ever checked out one oe more books and have returned ALL of them. I have a query (shown below that finds the entries if there Return Date is no NULL but I can't figure out how to now show the Names if they have a returned book but still have another book they have no returned (Return Date = NULL). Below is my script if anybody can give me some advice. Thank you.
SELECT MemName
FROM MEMBER, CHECK_OUT
WHERE MEMBER.ID = CHECK_OUT.MemID
AND CHECK_OUT.DateRet IS NOT NULL
If I understand what you are asking, you want a query that will return all members who currently have a book checked out, correct?
Something like this might be what you want:
SELECT
M.MemName
FROM
MEMBER AS M
INNER JOIN CHECK_OUT AS C ON (C.MemID = M.ID)
WHERE
C.DateRet IS NULL
This will list all member names that have something checked out, but will not remove duplicates. To list each name only one, use SELECT DISTINT. To get the names who do NOT have anything checked out anymore (but did at one point) use either your query or modify mine to say C.DateRet IS NOT NULL. I personally just like using INNER JOINS in this situation.
I want to create a query that will List members' names for who ever checked out one or more books and have returned ALL of them.
SELECT MEMBER.MemName
FROM MEMBER, CHECK_OUT
WHERE MEMBER.ID = CHECK_OUT.MemID
group by MEMBER.ID, MEMBER.MemName
having max(nvl2(CHECK_OUT.DateRet, 0, 1)) = 0
i've been searching for literally hours (1pm-11pm) for a solution to this SQL query I have to write. Basically, I have 2 tables and I have to select the ID from one table which has the maximum results in another. The second issue is there are 2 IDs. I can't quite explain what I mean because I'm that unsure but I can post my instructions and a link to the tables.
Any help will be greatly appreciated. I've also looked at a million other posts on SO and other places but even if it seems remotely relevant, I've no idea what changes to make to suit my needs.
i.e
SQL SELECT MAX COUNT
So my task is as follows:-
Display the name and the telephone number of private owners which have more properties than anybody else.
The top table in the image shows the "properties for rent" table and the lower shows the "private owners".
In reference to the question, I need to use the primary key of the private owners table to count the number of properties that each private owner has available to rent and then display the details of the private owner(s) who has the most properties available - which by studying the data, is 2 private owners (CO87 and CO93).
Again, I'd appreciate any help at all with this, I've been pulling my hair out for the best part of 12 hours :/
Thanks in advance guys,
Tim.
P.s - Just for the curious, this is one of an insane amount of SQL tasks for a university assignment =)
Edit:- The owner IDs are strings, not integers.
Your process should be:
Get a count of properties for each owner
Find the owner IDs with the max # of properties
Find the owners with those owner ID.
Seems like this should work:
SELECT * FROM Owners
WHERE OwnerID IN
(
SELECT OwnerID
FROM Properties
GROUP BY OwnerID
HAVING COUNT(*) =
(SELECT COUNT(*)
FROM Properties
GROUP BY OwnerID
ORDER BY COUNT(*) DESC
LIMIT 1)
)
Using CodeIgniter's Active Record class and MySQL, I have a table of posts with user_id and various other fields, and I want to count how many posts each user has made. I could get rows where user_id = $whatever and count the number of results, but I'd have to loop through every user_id and use that count_all_results() query over and over for each one.
There must be a better way! If every field just had a field with a 1 in it, I could select_sum up that field and get a count. But that seems dumb.
Many thanks in advance!
Using active record should be:
$this->db->select('field1, ... ,fieldn, count(1) as number_elements_of_row');
$this->db->group_by(array('field_group_1', ... ,'field_group_n'));
$result = $this->db->get('mytable');
so $result will have what you need!