get 1 row from multiple rows MYSQL - mysql

I am trying to achieve a SQL statement where I can get the number of installments from multiple same installments and also get 1 duedate from all duedates and sum the rest rows up.
Here is my Table in a picture.
What I want to achieve is this.
it will return me rows like
instalmentnbr | duedate | capitalpayment | interest_payment
1 2017-04-13 sum(capitalpayment) sum(interest_payment )
2 2017-05-13 sum(capitalpayment) sum(interest_payment )
3 2017-06-12 sum(capitalpayment) sum(capitalpayment)
So basically getting the 3 installments with their duedate and suming up the rest.
Here is my code.
select a.instalmentnbr, a.duedate, sum(a.capital_payment), sum(a.interest_payment), sum(a.overdue_payment)
from helltable a
where a.request_orig_id = 46 order by a.instalmentnbr;
I was checking out this example but I really dint get how to it works.
How to return only 1 row if multiple duplicate rows and still return rows that are not duplicates?

Sounds like you need to use GROUP BY:
select a.instalmentnbr, a.duedate, sum(a.capital_payment), sum(a.interest_payment), sum(a.overdue_payment)
from helltable a
where a.request_orig_id = 46
group by a.instalmentnbr, a.duedate
order by a.instalmentnbr;

Related

SQL select single row with two matching values

I'm probably having a bad day, but this is somehow escaping me:
I want to return the second row in this table only.
userId val1 val2
1 11 12
2 13 14
3 13 15
4 16 17
Using SELECT * FROM table WHERE val1=13 AND val2=14 obviously returns 2 rows, the second and third. Whats the correct way to select ONLY the second row? Where val1 is 13 and val2 is 14?
EDIT: I'm an idiot.
Just use SELECT * FROM table WHERE val1=13 AND val2=14like you already mentioned in your question, because in fact, it actually returns only row number 2.
If it has been a very bad day & there is a typo in your question & val2 in third row also equals 14 - the only way your query would return two rows, then this would do what you want
SELECT *
FROM table
WHERE val1=13 AND val2=14
ORDER BY userId
LIMIT 1;
If you get 2 rows, then there must be 2 rows match the condition.
Maybe you could try:
select count(*)
from table
where val1=13 AND val2=14;
to show the size of result set.

Why is this only returning 1 row?

I am trying to query 2 tables with a join. I expect to get 2 rows but only get 1:
SELECT tmp.pk, tmp.domain, count(crawl.pk)
FROM (
SELECT * FROM domains
WHERE domain IN('www.google.com', 'www.yahoo.com')
AND pk < 10000
) tmp
JOIN crawl ON crawl.domain=tmp.pk
AND crawl.date_crawled <= 3
HAVING COUNT(crawl.pk) < 1000
Result:
+-------+--------------------+-----------------+
| pk | domain | count(crawl.pk) |
+-------+--------------------+-----------------+
| 14929 | www.yahoo.com | 88 |
+-------+--------------------+-----------------+
1 row in set (0.03 sec)
If I remove 'www.yahoo.com' from the IN statement then I get 'www.google.com' in the result (therefore, I know that both www.google.com and www.yahoo.com pass my criteria).
Why is it returning only 1 row, when it should be returning 2?
Don't know why you're using a sub-query. Try this one...
SELECT d.pk, d.domain, count(c.pk)
FROM domains d
INNER JOIN crawl c ON d.pk = c.domain
WHERE d.pk < 10000
AND d.domain in ('www.google.com', 'www.yahoo.com')
AND c.date_crawled <= 3
GROUP BY d.pk, d.domain
HAVING COUNT(c.pk) < 1000
If you're still having issues, I'd try removing the HAVING clause as well as the d.pk < 10000
This doesn't make sense because tmp.pk is suppose to be less than 10000 yet your example recordset shows tmp.pk being greater than 10000.
And, your join looks incorrect. Seems you are trying to join a string-based field with a numerical one. I am referring specifically to: crawl.domain=tmp.pk
I would suggest you try JOIN by tmp.domain.
Alternatively, I would recommend removing the subquery and restructuring your query to represent more what #Phil suggested in his answer.

Unable to apply WHERE/AND on MySQL table with 2 columns on MAMP

I thought I had a very simple query to perform, but I can't seem to make it work.
I have this table with 2 columns:
version_id trim_id
1 15
1 25
1 28
1 30
1 35
2 12
2 25
2 33
2 48
3 11
3 25
3 30
3 32
I am trying to get any version-id's that have say a sub-set of trim_id's. Let's say all version_id's that have trim_id's 25 and 30. My obvious attempt was :
SELECT * FROM table WHERE trim_id=25 AND trim_id=30
I was expecting to have version_id 1 and 3 as a result, but instead I get nothing.
I am working with the latest version of MAMP, which has some odd behavior, like in this case it just tells me its 'LOADING' and never gives me an error message or something. But that's normally the case when there is no data to return.
This is InnoDB, if that helps.
Thanks for your input.
Your query does not work because you are using AND and the trim_id cannot have two different values at the same time, so you need to apply Relational Division to get the result.
You will need to use something similar to the following:
SELECT version_id
FROM yourtable
WHERE trim_id in (25, 30)
group by version_id
having count(distinct trim_id) = 2
See SQL Fiddle with Demo.
This will return the version_id values that have both 25 and 30. Then if you wanted to include additional columns in the final result, you can expand the query to:
select t1.version_id, t1.trim_id
from yourtable t1
where exists (SELECT t2.version_id
FROM yourtable t2
WHERE t2.trim_id in (25, 30)
and t1.version_id = t2.version_id
group by t2.version_id
having count(distinct t2.trim_id) = 2);
See SQL Fiddle with Demo
SELECT *
FROM table
WHERE trim_id IN(25,30)

Simple MySQL Query - Change table format around

I'm fairly sure this is a fairly easy answer but the answer is completely slipping my mind.
I have a database table that is currently formatted like:
event_id | elem_id | value
1 1 Value 1
1 2 Value 2
2 1 Value 3
2 2 Value 4
Both event_id and elem_id are undetermined numbers and have infinite possibilities.
How would I query it for example based on event_id 1 to get the data to be formatted as such:
event_id | 1 | 2
1 Value 1 Value 2
Knowing that elem_id is a number >= n so potentially there could be 50 elem_id yet I still need the data in that format.
Like I said I can't for the life of me figure out the query to assemble it that way. Any help would be GREATLY appreciated.
Try following:
SELECT
`event_id`,
(SELECT t2.`value` FROM table t2 WHERE t2.`event_id` = t1.`event_id` AND t2.`elem_id` = 1),
(SELECT t3.`value` FROM table t3 WHERE t3.`event_id` = t1.`event_id` AND t3.`elem_id` = 2)
FROM `table` t1 GROUP BY `event_id`;
Also you can use different way, and get elem_ids and values in comma-separated format in two cells
SELECT `event_id`, GROUP_CONCAT(`elem_id`), GROUP_CONCAT(`value`) FROM `table` GROUP BY `event_id`;
and you can change separator with following syntax: GROUP_CONCAT(field SEPARATOR '::')

Using 'AND' in SQL WHERE clause for the same filed

Here is the scenario. I have 1 table that has all the contact details. Another table that has all the list of Categories. And a 3rd table which is an associate table, that has the ID of the first table and the ID of the second table.
This is how my associate table looks like
contactdid -2 | categoryid -1
contactdid -2 | categoryid -2
contactdid -2 | categoryid -3
contactdid -3 | categoryid -1
contactdid -3 | categoryid -3
This is my SQL code below(Generated using SQLyog and i included the where clause).
SELECT
press_contacts.email
FROM
contacts_category
INNER JOIN press_category
ON (contacts_category.categoryid = press_category.id)
INNER JOIN press_contacts
ON (contacts_category.contactdid = press_contacts.id)
WHERE contacts_category.categoryid = 1 AND contacts_category.categoryid = 2 ;
I get the output when I do not have AND contacts_category.categoryid = 2inserted in the code.
Any idea how to solve this.I clearly have data.
Thanks in advance for the help.
contacts_category.categoryid can not be 1 and 2 at the same time, perhabs you mean OR instead of AND?
Use OR or IN() instead of AND.
A field can't have two values at the same time
If you only want to see those email addresses with contacts in both categories, try:
SELECT press_contacts.email
FROM contacts_category
INNER JOIN press_contacts
ON (contacts_category.contactdid = press_contacts.id)
WHERE contacts_category.categoryid in (1, 2)
GROUP BY press_contacts.email
HAVING COUNT(DISTINCT contacts_category.categoryid)=2