Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Alright, after 3-4 days of trying to solve this I am giving up.
I started out with only one table but now I have changed it alot so I am able to have two tables, I thought it would make it alot easier for a rookie like me.
See my earlier post here
Instead of posting all the table details here I am providing a screenshot:
Tables and expected result
I have managed to come quite far with this but the problem is that I am not able to do the calculations excluding the batches that should not be included.
How could I solve this? I really appreciate the help I get from you guys.
Thanks in advance
First step: Create a filter for the details table
SELECT
MAX(id) AS id
FROM details
GROUP BY `concat`, `batch`
Next step: Use this to query the details table
SELECT * FROM details
WHERE id IN (
SELECT
MAX(id) AS id
FROM details
GROUP BY `concat`, `batch`
)
Next step: Use this derived table to join the master table for your final result
SELECT
`master`.id AS id,
`master`.plant AS plant,
`master`.`code` AS `code`,
COUNT(*) AS distinct_batches,
SUM(filtereddetails.volume) AS total_vol,
SUM(filtereddetails.`value`) AS total_val,
SUM(filtereddetails.volume*filtereddetails.risk) AS risk_vol,
SUM(filtereddetails.`value`*filtereddetails.risk) AS risk_val,
MAX(filtereddetails.end_date-filtereddetails.start_date) AS max_date_diff
FROM
`master`
INNER JOIN (
SELECT * FROM details
WHERE id IN (
SELECT
MAX(id) AS id
FROM details
GROUP BY `concat`, `batch`
)
) AS filtereddetails ON `master`.`concat`=filtereddetails.`concat`
GROUP BY
`master`.`concat`
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 months ago.
The community reviewed whether to reopen this question 4 months ago and left it closed:
Original close reason(s) were not resolved
Improve this question
I have a problem about writing a nested Select queries in sql.
I tried to devise a new table to show their counts of the tables but I couldn't complete the rest part. I show it as "...."
How can I do that?
Here is the code snippet shown below.
SELECT
(SELECT COUNT(DISTINCT u.id) AS totalUsers FROM Users u),
(SELECT COUNT(DISTINCT c.id) AS totalCategories FROM Categories c)
....
I want to get this result
New Table
totalUsers totalCategories ...
60 10 ...
First you need to create a table (but check first if it exists, I leave it up to you)
CREATE TABLE CountTable (
TotalUsers int,
TotalCategories int
);
Then insert the result of your query to the new table:
INSERT INTO CountTable (TotalUsers, TotalCategories)
SELECT
(SELECT COUNT(DISTINCT u.id) AS totalUsers FROM Users u),
(SELECT COUNT(DISTINCT c.id) AS totalCategories FROM Categories c)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I've got a database with two relevant tables (times and registrations).
I want to get the times from the times table, through an SQL query, that aren't in the registrations table (don't have a registration on them). Registration at 5PM, wouldn't show 5PM from times in the query.
My current query is:
SELECT time FROM `times` WHERE time IN (SELECT time FROM `registrations` WHERE ID IS NOT NULL)
(registrations DO have an ID)
This does the opposite of what I want it to do (shows all times, regardless of registration, or not).
How could I get the opposite effect?
You seem to want not exists:
select t.*
from times t
where not exists (select 1 from regisrations r where r.time = t.time)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have a table 'transaction' which contains columns transaction_id, sender_id,amount, date_time,payee_id .
I want to find transactions which are more than five made by sender_id in one day by single sender.
i tried
mysql> select * from transaction where sender_id count() =5 and datedif() =1;
I think this query will be helpful for you.
select sender_id, count(sender_id) as count
from `transaction`
group by sender_id, date(date_time)
having count>5
this will return the list of sender_ids who have more than 5 transactions
if you also need the list of that transactions than you need this
select t.*
from (
select sender_id, count(sender_id) as count
from `transaction`
group by sender_id, date(date_time)
having count>5
) as senders
inner join `transaction` as t on t.sender_id=senders.sender_id
PS. for good performance I recommend run second query on MySQL server with version 5.6.10 or higher
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have two tables
Table A : Content Question , its options and correct answer
Table B : This table is for time allow to answer the question in given time.
this table has Question_Id field which either have question id or zero. zero means if for Table A Question Id is not found in Table B then default Time will be Table B's Question_Id=0 > 5 Min
Now I want the data like Result table from query. By using Select Query with Join I am getting question details, which are matched with question Id (1,2,4) means for Question 3,5,6 row not getting that showing in result table.
Please suggest what sql query should write so that I can get result like Result Table's content.
I change your tables to small and simple tables and you can see the result in:
SQL Fiddle
or try this query:
SELECT t1.questionid,
t1.question,
t1.options,
t1.answer,
COALESCE(t2.timingstatement, '5 Min') TimingStatement
FROM tablea t1
LEFT OUTER JOIN tableb t2
ON t1.questionid = t2.questionid;
Try this
SELECT Q.QuestionID,Q.Question,Q.Options,QAnswer,
CASE WHEN Q.QuestionID NOT IN (SELECT QuestionID FROM Table2) THEN '5 Min'
ELSE T.TimingStatement
END [TimingStatement]
FROM Table1 Q
JOIN Table2 T ON Q.QuestionID = T.QuestionID
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have three tables say 'user', 'user_resources' and 'desktop_resources'.
'user' contains - emp_id,name and other attributes
'user_resources' - emp_id and desktop_id foreign key relation
'desktop_resources' - desktop_id and other attributes
Now i want a sql query from where i can get a table which shows me name from 'user' table and 'desktop_resources' attributes only where "emp_id=d_id"
how to go about it??
I don't see d_id column there, but if you think so, it would look like this:
SELECT name, desktop_resources.*
FROM desktop JOIN user_resources USING (desktop_id) JOIN user USING (emp_id)
This is a straightforward series of joins:
select u.name, dr.*
from user u
join user_resources ur on ur.emp_id = u.emp_id
join desktop_resources dr on dr.desktop_id = ur.desktop_id
where u.emp_id = $d_id
Finally found this query useful:
SELECT name, desktop.*
FROM desktop
NATURAL JOIN (
user
JOIN user_resource ON user.emp_id = user_resource.emp_id
)
I am sure there may be other ambiguous queries for this..if u have got a better query..please put it in comments...