I have two (2) tables:
ps_wk_mp_seller_transaction_history
And ps_wk_mp_seller_order_status
I want to totalize seller_amount only if current_state = 5
For that I have written this:
select sum(seller_amount) as seller_amount
from ps_wk_mp_seller_transaction_history th inner join
ps_wk_mp_seller_order_status os
on os.id_order = th.id_transaction and
os.current_state = 5 and
th.id_customer_seller = 2;
For id_customer_seller=2 I get this 4984.020000 (4950+34.02) and it is exact
But I get 25.848000 instead of NULL for id_customer_seller=5
Can someone help me?
May be you can test yourself, this the code: https://github.com/kulturman/fakerepo
First of all your records have some logic issue, id_transaction have tow transactions with same id_transaction but different amount and different state ! so transaction with id 41 having state 5 and that why customer 5 will not be null because 41 is in state 5.
To fix this
transaction id must be unique id for every transaction in order to differentiate between transaction state and amount
the query should be like this
select sum(seller_amount) as seller_amount
from ps_wk_mp_seller_transaction_history th
left join ps_wk_mp_seller_order_status os
on os.id_order=th.id_transaction
where
th.id_customer_seller = 2
and os.current_state=5
working example here
Related
So I am working with event data.
I need to identify when "X happens", store that data in a column and then identify when "in-production" happens.
Now, I just want the first "in-production" that shows up after "X happens" I do not care about the previous ones.
Note: Between "X happens" and "in-production" happens multiple states can exist.
What have I tried: case whens, self joins, with tables... nothing to my avail.
Any help is much appreciated, thanks a lot friends!
TblEvents
=========
EventID OrderID EventDate Status
1 2 01/02/2011 in-production
2 2 02/02/2011 pending
3 2 03/02/2011 on-hold
4 2 03/02/2011 stuck
5 2 03/02/2011 *X happens*
6 2 04/02/2011 pending
7 2 05/02/2011 *in-production*
Output table:
date of event X | date of "in-production"
03/02/2011 05/02/2011
Try this:
SELECT
OuterTblEvents.EventDate AS 'date of event X',
(
SELECT EventDate
FROM TblEvents
WHERE
TblEvents.Status = 'in-production'
AND TblEvents.EventID > OuterTblEvents.EventID
ORDER BY TblEvents.EventID
LIMIT 1
) AS 'date of "in-production"'
FROM TblEvents OuterTblEvents
WHERE OuterTblEvents.Status = 'X happens'
Though note that if the table is large and/or you run this often - performance may suffer, because the inner query is invoked once per each row that is found in the outer query.
Also, a composite index on Status, EventID should make both the outer and inner queries more performant. The outer query would use only the Status part of the index, and the inner query would use both Status and EventID. Note that the order of the columns in this composite index matters.
i'm having some trouble with trying to extract some data from several MySQL tables in a join statement.
My tables and attributes are:
appointment_end_time (table)
appointment_end_time_id (int)(pk)(ai)
appointment_end_date (datetime)
appointment_start_time (table)
appointment_date_id (int)(pk)(ai)
appointment_start_date (datetime)
instructor(table)
instructor_id (int)(pk)(ai)
firstname varchar(45)
lastname varchar(45)
appointment_timetable
appointment_timetable_id int(11) AI PK
instructor_id int(11) FK
appointment_date_id int(11) FK
appointment_end_time_id int(11) FK
SELECT a.appointment_timetable_id, i.instructor_id, ad.appointment_start_date, aet.appointment_end_date
FROM db12405956.appointment_timetable a
JOIN instructor i on i.instructor_id = a.instructor_id
JOIN appointment_start_time ad on ad.appointment_date_id = a.appointment_date_id
JOIN appointment_end_time aet on aet.appointment_end_time_id = a.appointment_end_time_id
ORDER BY a.appointment_timetable_id;
However, this code brings back no rows selected when executed so i'm wondering what i'm doing wrong, any help will be much appreciated
Sample rows:
(appointment_end_time)
appointment_end_time_id appointment_end_date
1 2016-12-26 14:00:00
2 2016-12-24 13:00:00
3 2016-12-26 13:00:00
(appointment_start_time)
appointment_date_id appointment_start_date
1 2016-12-26 15:00:00
2 2016-12-24 16:00:00
3 2016-12-26 15:30:00
instructor_id firstname lastname
1 Sasha Thompson
2 Laura Robinson
3 John Walters
appointment_timetable
appointment_timetable_id instructor_id appointment_date_id appointment_end_time_
1 Blank Blank Blank
2 Blank Blank Blank
3 Blank Blank Blank
What you need is to learn how to diagnose the problem yourself. It is a common problem that a query doesn't return the expected results and you should understand how to break things down to find the issue.
Let's start with your query:
SELECT a.appointment_timetable_id, i.instructor_id, ad.appointment_start_date, aet.appointment_end_date
FROM db12405956.appointment_timetable a
JOIN instructor i on i.instructor_id = a.instructor_id
JOIN appointment_start_time ad on ad.appointment_date_id = a.appointment_date_id
JOIN appointment_end_time aet on aet.appointment_end_time_id = a.appointment_end_time_id
ORDER BY a.appointment_timetable_id;
What you do to break it down is start with the first table and then add the joins (and where conditions although you don't have any here), one at a time until the data problem appears. I find this easiest to do by using select * or select top 1 * (Or top 10 as I usually prefer to see more than one record) instead of the field list because then you don't have to look for the fields that are associated with joins you haven't added in yet.
So start with
SELECT top 10 *
FROM db12405956.appointment_timetable a
Then try
SELECT top 10 *
FROM db12405956.appointment_timetable a
JOIN instructor i on i.instructor_id = a.instructor_id
Then
SELECT top 10 *
FROM db12405956.appointment_timetable a
JOIN instructor i on i.instructor_id = a.instructor_id
JOIN appointment_start_time ad on ad.appointment_date_id = a.appointment_date_id
Finally
SELECT top 10 *
FROM db12405956.appointment_timetable a
JOIN instructor i on i.instructor_id = a.instructor_id
JOIN appointment_start_time ad on ad.appointment_date_id = a.appointment_date_id
JOIN appointment_end_time aet on aet.appointment_end_time_id = a.appointment_end_time_id
ORDER BY a.appointment_timetable_id;
At some point you will see where the records fell out and that is the location of the problem. Then you might need to look at the fields you are joining on and the data in them in your data sets to see why they are not returning any matches. For instance, if you are joining on dates, they may be stored as dates in one table and as varchar in another and date "01/01/2016' is not equal to 'Jan 1, 2016' or sometimes the column has some sort of prefix or suffix not in the other table. Something like PR2345 in one table and 2345 in the other. Sometimes the query is correct and no rows genuinely meet the conditions. This could be because the data is not fully populated yet (think writing a report for a system that is not live yet, no data on completed actions because none have completed yet.) or because the requirement was wrong in some of its assumptions or because there should be no matching records. It could even be a bug in the data entry.
Depending on the nature of the problem, you might need to return all the records or only use select top 1 (since all records are disappearing). Using SELECT * this way will help when you are returning too many or duplicate records as well as sometimes is is the fields not being returning that affect the results set. Note that I am not saying to use SELECT * in your final result set, it is only being used as a diagnostic tool here.
In your case, the problem looks as if it is in the first table. There are blanks for instructor ID and the other fields in your sample, so there is nothing to join on. (You only gave a sample so the rest of the table may not be like this.) If this is a case where the data is not there yet due to the feature that would add it not yet being live, then you can test your query only by adding test data to the table. Be sure to delete this data after you have finished unit testing. If the data should have been there, then you need to look at the insert from the application for a bug.
I've got two tables in my MySQL DB. One contains requiredSkill1, requiredSkillLevel1, requiredSkill2, requiredSkillLevel2, requiredSkill3 and requiredSkillLevel3.
The other table has X rows per user with the following collumns: skill and level.
itemid requiredSkill1 requiredSkillLevel1 requiredSkill2 requiredSkillLevel2 requiredSkill3 requiredSkillLevel3
2410 3319 4 20211 1 NULL NULL
The other table:
userid skill level
21058 3412 4
21058 3435 2
21058 3312 4
Keep in mind, these are just examples.
I want every itemid which has matching values in requiredSkill{1-3} and requiredSkillLevel{1-3}.
Is this even possible with a single query and is this still performant, since the user table contains up to 300 rows per user and the item table has a fixed value of 6000 rows. This will be used in a web application, so I can use Ajax to load ranges of items from the database to decrease loading time.
I don't have the data set up. A SQL Fiddle would be helpful, but I think you want to approach it like this:
SELECT itemid FROM items i
INNER JOIN users u1 ON u1.skill = i.requiredSkill1 AND u1.level >= i.requiredSkillLevel1
INNER JOIN users u2 ON u2.skill = i.requiredSkill2 AND u2.level >= i.requiredSkillLevel2 AND u1.userid = u2.userid
INNER JOIN users u3 ON u3.skill = i.requiredSkill3 AND u3.level >= i.requiredSkillLevel3 AND u3.userid = u1.userid
Someone will solve this for you if you post demo data.
When I make this sql statement I get 6 of the same record returned. So if I expect to get 2 records returned, I get six of each record back so that is 12 in total.
SELECT
ce2.*
FROM customerentry ce, customerentrytrace cet, customerentry ce2
WHERE ce.accountid = 1
AND ce.companyid = 1
AND ce.accountid=cet.accountid
AND ce.accountid=ce2.accountid
AND ce.companyid=cet.companyid
AND ce.companyid=ce2.companyid
AND cet.documentno = '2012Faktura1'
AND cet.documenttype = 1
AND ce2.documentno = cet.offsetdocumentno
AND ce2.documenttype = cet.offsetdocumenttype
ORDER BY created;
I know that I can solve it by adding distinct, but I would like to know why I get 6 of the same record returned. Anyone who can help me?
Since we have no idea about your table structure probably there are some columns that are related 1 to n items and you haven't handled them in the WHERE section of your query.
As an extra measure you can focus on your data needs and add a GROUP BY section before your ORDER section.
You are using an INNER JOIN, so for example there are two entries in table cet matching your where clause for combining table ce and cet, giving you 2 entries/entry of table ce.
Thinking this further you can see that if there are 3 entries in table ce2 matching the where clause for combining table cet and ce2 you get 3 entries/entry of table cet.
Which makes 6 entries per entry of table ce in total, giving you 12 entries in total even if you have only 2 entries in table ce.
So think again about what join could be the right for your desired solution.
Here a link for some more explanation: Short explanation of joins
Problem might be because you have not properly joined tables. Please read about JOIN
SELECT ce2.*
FROM customerentry ce INNER JOIN customerentrytrace cet ON ce.accountid=cet.accountid AND ce.companyid=cet.companyid,
INNER JOIN customerentry ce2 ON ce.accountid=ce2.accountid AND ce.companyid=ce2.companyid AND ce2.documentno = cet.offsetdocumentno AND ce2.documenttype = cet.offsetdocumenttype
WHERE ce.accountid = 1
AND ce.companyid = 1
AND cet.documentno = '2012Faktura1'
AND cet.documenttype = 1
ORDER BY created;
I have a query problem. I have a table of agents that do things. I keep track of the things they do in an events table. I want to keep my agents busy but not too busy, so I need a query that will return me a group of agents that have done no more that 10 events in the past 10 minutes and no more than 400 events in the past 24 hours. And from this pool of available agents I can choose one to give something to do
So my agent table looks something like:
Agent table
AgentID. AgentName
1 Bob
2 Sue
Event Table
Event ID. Agent ID. Event Timestamp
1 2 1319525462
2 1 1319525462
3 2 1319525462
Obviously these tables are just to give the form of the db. What I need generally and have not been able to figure out is how to select a group of agents from a join that returns a group of agents that have done no more than 10 events in the past 10 min and no more than 400 events in the past 24 hours. My actual tables are more complex, but I am just looking for a general principle on how to structure a query that would return the desired result. Thanks ahead of time for the help!
**UPDATE
building on Benoit's answere I came up with this:
SELECT DISTINCT username FROM campaign_agents
LEFT OUTER JOIN (SELECT count(event_index) myevents, field_event_agent_value FROM new_event WHERE field_event_time_value BETWEEN 1320206138 AND 1320292538 GROUP BY field_event_agent_value ) last_24_hours
ON last_24_hours.field_event_agent_value = campaign_agents.username
LEFT OUTER JOIN (SELECT count(event_index) myevents, field_event_agent_value FROM new_event WHERE field_event_time_value BETWEEN 1320291938 AND 1320292538 GROUP BY field_event_agent_value ) last_10_mins
ON last_10_mins.field_event_agent_value = campaign_agents.username
WHERE last_24_hours.myevents < 550 AND last_10_mins.myevents < 10
But it doesn't get the agents in the campaign_agents table who haven't done anything yet and are therefore not in the events table. Shouldn't a LEFT OUTER JOIN include everything in the first table, campaign_agents, even if there are no matches to the second table? Do I need to put and OR statement after the where to somehow get them included?
You can try:
SELECT agent.agentid
FROM agent
INNER JOIN (SELECT count(eventid) events, agentid
FROM event
WHERE timestamp BETWEEN /* (now - 24 hours) */ AND /* now */ -- adapt this
GROUP BY agentid
) last_24_hours
ON last_24_hours.agentid = agent.agentid
INNER JOIN (SELECT count(eventid) events, agentid
FROM event
WHERE timestamp BETWEEN /* (now - 10 minutes) */ AND /* now */ -- adapt this
GROUP BY agentid
) last_10_mins
ON last_10_mins.agentid = agent.agentid
WHERE last_24_hours.events < 400
AND last_10_mins.events < 10