Hello I have two tables:
table1 - eod_stock
company_code | open | high | low | ltp | close | ycp | total_volume | total_value | datetime
table 2 - company
ID | code | name
here company code = code so to get all name and other info i used this code:
but first one gives me error and 2nd one returns only one row, but i need all 200 companies with their associated info.
select
company.code,
company.name,
eod_stock.open,
eod_stock.high,
max(eod_stock.datetime)
from
company
right join company on company.code= eod_stock.company_code;
and
select
eod_stock.company_code,
max(eod_stock.close),
eod_stock.total_volume,
eod_stock.total_trade,
eod_stock.high,
eod_stock.low,
eod_stock.ltp,
max(eod_stock.datetime),
company.name
from
eod_stock
inner join company on (company.code = eod_stock.company_code);
but first one gives me error and 2nd one returns only one row, but i need all 200 companies with their associated info.
The trick here is to start with a list of the max datetime for each company_code, which you can do with this basic query:
SELECT company_code, MAX(datetime) AS maxdatetime
FROM eod_stock
GROUP BY company_code
Join this to a query that gets company code, company name, and end-of-day values, and you should be all set:
SELECT
company.code,
company.name,
eod_stock.open,
eod_stock.high
FROM eod_stock
INNER JOIN company ON company.code = eod_stock.company_code
INNER JOIN (
SELECT company_code, MAX(datetime) AS maxdatetime
FROM eod_stock
GROUP BY company_code) maxdt
ON maxdt.company_code = eod_stock.company_code AND
maxdt.maxdatetime = eod_stock.datetime
Your first error i guess that your have to write :
Table2 right join Table1
instead of company right join company
the 2nd one to get all company your full join !!
select
eod_stock.company_code,
max(eod_stock.close),
eod_stock.total_volume,
eod_stock.total_trade,
eod_stock.high,
eod_stock.low,
eod_stock.ltp,
max(eod_stock.datetime),
company.name
from
eod_stock
inner join company on (company.code = eod_stock.company_code) group by eod_stock.company_code;
dagfr was correct all i needed to add group by in the query.
Related
I have the table news_feed in which all of my different types of activities data will be stored like admin activities, user activities, company activities etc. The table format looks like:
news_id | type | admin_id | user_id | company_id
1 | admin | 2 | 3 | 0
2 | user | 3 | 4 | 1
3 | company | 0 | 1 | 2
Suppose a user with an id 1 has liked the company which has id 2 then the record will be inserted like
4 user 0 1 2
And I'm listing them in my module and the listing is perfect. But suppose if the company id 2 doesn't exist or if it is inactive, then the news_feed block in listing getting empty. What I want to do is:
If the type is company then JOIN the company table while select listing with condition for status as active
If the type is user then JOIN the user table while select listing with condition for status as active
Well you can use UNION for this issue
SELECT t.column1, t.column2, t.column3
FROM my_table t INNER JOIN company_table c
ON t.company_id = c.id
WHERE c.active=1 AND t.type = "company"
UNION
SELECT column1, column2, column3
FROM my_table t INNER JOIN user_table u
ON t.user_id = u.id
WHERE c.active=1 AND t.type = "user"
Just to add, to increase the efiiciency use UNION ALL rather than UNION (or UNION DISTINCT) as UNION requires internal temporary table with index (to skip duplicate rows) while UNION ALL will create table without such index, but keep in mind it will skip the repeated data.
But more optimized way to do a Conditional Join in MySQL by using a INNER JOIN
SELECT t.column1, t.column2, t.column3
FROM my_table t
INNER JOIN company_table c
ON (t.company_id = c.id AND t.type = "company" AND c.active=1)
INNER JOIN user_table u
ON (t.user_id = u.id AND t.type = "user" AND u.active=1);
I have two tables, tbl_customers and tbl_transactions, where the fields of them are as follows:
**tbl_customer:**
c_id | c_name | age | Slab | gender | occupation | married | priority | ratings | contact | email | dob | anniversary
**tbl_transaction**
t_id | c_id | d_id | f_id | date_time | bill | member
The table tbl_transaction is a global table containing the transactions of all departments, differentiated by d_id.
Now I want to run a query that fetches c_id, total visits(means no of rows a c_id apppears in tbl_transaction), total revenue(means SUM(bill) of a particular c_id in) and the respective priority and ratings as it is in tbl_customer.
The query I was trying is :
Select c_id,
COUNT(cid) as tot_visit,
SUM(bill) as tot_revenue, priority, ratings
From tbl_customer c
inner join tbl_transaction t on c.c_id=t.c_id
Where $r_id='r1' AND $c_id='".$cid."'
group by c_id
So basicaly I want to display the total visits and revenue of each customer in customer table, but not sure what is the right way to do it.
Any help would be appreciated.
This is the error I am getting in SQL Engine
According to your error there are fields in both tables that have same name in your tables you need to give the table alias for the field, also when you are filtering on only one group then there is no need to use group by because you are using AND c.c_id='".$cid."' for a single group
Select c.c_id,
COUNT(c.cid) as tot_visit,
SUM(t.bill) as tot_revenue, c.priority, c.ratings
From tbl_customer c
inner join tbl_transaction t on c.c_id=t.c_id
Where $r_id='r1' AND c.c_id='".$cid."'
group by c.c_id
In general the query is fine. I would replace inner join with outer to process customers cho don't have transactions
Select c_id, COUNT(cid) as tot_visit, SUM(bill) as tot_revenue,
priority, ratings
From tbl_customer c LEFT JOIN tbl_transaction t on c.c_id=t.c_id
Where $r_id='r1' AND $c_id='".$cid."'
group by c_id
I have two tables of which one is updated daily. I would like to display "only" the latest record for each row.
This is the query I am using now that of course returns all the records.
SELECT *
FROM ss_pumps, secondary_systems WHERE ss_pumps.id=secondary_systems.segment_id
ORDER BY id ASC
Any help would be greatly appreciated!
You can find the latest record for every segment_id by ID using subquery. The result of the subquery is then join against the two tables: ss_pumps and secondary_systems.
SELECT a.*, c.*
FROM ss_pumps a
INNER JOIN
(
SELECT segment_id, MAX(datefield) max_val
FROM secondary_systems
GROUP BY segment_id
)b ON a.id = b.segment_id
INNER JOIN secondary_systems c
ON b.segment_id = c.segment_id AND
b.max_val = c.datefield
Actually, I'm not sure how your tables: ss_pumps and secondary_systems are related with each other.
I think you want it the other ways,
SELECT a.*, b.*
FROM secondary_systems a
INNER JOIN ss_pumps b
ON a.segment_ID = b.segment
INNER JOIN
(
SELECT segment, MAX(ID) max_val
FROM ss_pumps
GROUP BY segment
) c ON b.segment = c.segment AND
b.ID = c.max_val
Use this query:
SELECT * FROM ss_pumps, secondary_systems WHERE ss_pumps.id=secondary_systems.segment_id ORDER BY id DESC LIMIT 1
This is assuming that id is an auto increment column and will always be inserted in order.
Here's what I got:
SELECT *
FROM
ss_pumps ssp
LEFT JOIN ( SELECT * FROM secondary_systems ORDER BY id DESC ) ss ON ( ssp.id = ss.segment_id )
GROUP BY
ssp.id
ORDER BY
ssp.id ASC
CAVEAT: I'm assuming that the secondary_systems has its own id field that also autoincrements. That's the only way you can make sure you're getting "only" the latest record for each row.
Demo: http://sqlfiddle.com/#!2/f816b/2/0
In my demo ss_pumps held the parents while secondary_systems held the children. Each parent has 3 children. All but the last children are boys. The last child is always a girl. According to your problem, the resulting query should yield only girls.
| ID | PARENT | SEGMENT_ID | CHILD |
------------------------------------
| 1 | mother | 1 | betty |
| 2 | father | 2 | tina |
I have two table one table containing station id and station name, and another table containing id, name, startStationId, endStationId. I know second table's id by using that id i need to fetch all details of second table with station name for corresponding startStationId, endStationId.
ex: table1
---------------------------------
slNo staionId staionName
---------------------------------
1 0012 Bangalore ,
2 0014 Chennai ,
3 0015 Mumbai
---------------------------------
Table 2:
------------------------------------------
Id Name startStationId endStationId
-------------------------------------------
123 Dhinesh 0014 0015
-------------------------------------------
For example i know second table id(123).. so i want to fetch all results by using id, result would be.
Id =>123, Name => Dhinesh, StartStaion =>Chennai , Endstation=>Mumbai.
How can we write in one single query...?
Thanks in advance.
Try this.
SELECT t2.Id,t2.name,t1.StationName,t3.StationName
FROM table2 t2
INNER JOIN table1 t1 ON t2.startStationId = t1.stationId
INNER JOIN table1 t3 ON t2.endStationId = t3.stationId
SELECT t2.Id, t2.Name, tstart.stationName , tend.stationName
FROM table2 as t2
INNER JOIN table1 as tstart ON t2.startStationId = tstart.stationId
INNER JOIN table1 as tend ON t2.endStationId = tend.stationId
this should work
I have two tables in a database that looks like this
members table1
id | memberid | name | password
-------------------------------
Journal table2
id | memberid | title | desc
-----------------------------
i want to select all members from table one, joining with the result journal_count of each member in table 2. Am trying to count the number of times each memberid appears in table2
Am using mysql and php, can someone help me with this query
thanks
select m.memberid,m.name,count(j.memberid) as total
from members as m
left join journal as j
on m.memberid = j.memberid
group by m.memberid
P.S. If your table has a field named desc, beware that this is a reserved word and it would be better to rename it. Otherwise you'll always have to put it within backticks.
select m.memberid,m.name,count(j.memberid) as total
from members as m
left join journal as j
on m.memberid = j.memberid
where m.memberid = $memberid
group by m.memberid
select members.name, count(journal.memberid) as journal_count from members, journal where members.memberid = journal.memberid group by journal.memberid