A table
--------------------
id bId txnVolume
1 1(b table) 10.00
2 1 5.00
3 2 7.00
4 3 2.00
B table
--------------------
id cId
1 1(C table)
2 2
3 3
C table
--------------------
id cusId prodId
1 1 1
2 1 2
3 1 1
4 1 2
I want to get the sum of txnVolume on table A if C table is custId 1 and prodId 1? Thanks for the help guys! Cheers!
Select sum(coalesce(txnVolume,0))
FROM A INNER JOIN B on A.BID = B.Bid
INNER JOIN C on C.ID = B.CID
WHERE C.CustID = 1 and C.ProdID = 1
Try the following:
select sum(txnVolume) from A
join B on A.bID = B.id
join C on C.cID = C.id
where prodID = 1 and custID = 1
In sql you need to join the tables. When tables are joined they create one big table. You can then add where clauses to the columns.
SELECT sum(txnVolume)
FROM A
JOIN B on A.bId = B.id
JOIN C on B.cID = c.id
where c.custid = 1 and c.prodid = 1
SELECT SUM(a.txnVolume)
FROM c
LEFT JOIN b
ON c.id=b.cId
LEFT JOIN a
ON b.id=a.bId
WHERE c.cusId=1 AND c.prodId=1;
SELECT SUM(a.txnVolume) totals
FROM tableA a
INNER JOIN tablB b
ON a.bid = b.id
INNER JOIN tableC c
ON b.cid = c.ID
WHERE c.custID = 1 AND c.prodID = 1
Related
Table jenis_usaha
ID JENIS_USAHA
1 Laundry
2 Restauran
Table waralaba
ID ID_JENIS_USAHA
1 1
2 2
Table perusahaan
ID NAME ID_WARALABA
1 A 1
2 B 2
3 C 1
4 D 1
Table outlet
ID ID_PERUSAHAAN
1 1
2 1
3 2
4 2
5 1
6 1
7 1
8 1
9 1
The result that I want is like this
ID JENIS_USAHA TOTAL_PERUSAHAAN TOTAL_OUTLET
1 Laundry 3 7
2 Restauran 1 2
I have this mysql query
SELECT ju.ID,
ju.JENIS_USAHA,
COUNT(p.ID) AS TOTAL_PEMBERI,
(SELECT count(o.ID)
FROM outlet o
WHERE p.ID = o.ID_PERUSAHAAN
AND w.ID = p.ID_WARALABA
AND ju.ID = w.ID_JENIS_USAHA
) AS TOTAL_OUTLET
FROM jenis_usaha ju
LEFT JOIN waralaba w ON w.ID_JENIS_USAHA = ju.ID
LEFT JOIN perusahaan p ON p.ID_WARALABA = w.ID
GROUP BY ju.ID
I've got no error BUT a wrong result for TOTAL_OUTLET. Could you please show me the right query for this? thanks
you did the wrong join that's why you got wrong output
below query should work as your way
select j.id ,j.name,TOTAL_PERUSAHAAN,outlet as TOTAL_OUTLET
from jenis_usaha j inner join
(select p.id,count(o.ID_PERUSAHAAN) as outlet from outlet o
inner join perusahaan p
on o.ID_PERUSAHAAN=p.id
group by p.id
) t1
on t1.id=j.id
inner join
(
select w.id, count(p.ID_WARALABA) as TOTAL_PERUSAHAAN
from waralaba w inner join perusahaan p
on w.id=p.ID_WARALABA
group by w.id
) t2
on t2.id=j.id
id name TOTAL_PERUSAHAAN TOTAL_OUTLET
1 Laundry 3 7
2 Restauran 1 2
here is fiddle link where you can find details
http://sqlfiddle.com/#!9/400c971/6
I was taking very long to understand your query, so I decided to do my own.
This one bring the results you wanted anyway. Please check if this works for your case. =)
Btw, st stands for subtotal
SELECT
st.id,
st.name,
count(ps_id) AS ps_amount,
sum(ol_amount) AS ol_amount
FROM
(SELECT
ju.id,
ju.name,
ps.id AS ps_id,
count(ol.id) as ol_amount
FROM
jenis_usaha ju
INNER JOIN
waralaba wl
ON wl.id_jenis_usaha = ju.id
LEFT JOIN
perusahaan ps
ON ps.id_waralaba = wl.id
LEFT JOIN
outlet AS ol
ON ol.id_perusahaan = ps.id
GROUP BY
ju.id,
ju.name,
ps.id) st
GROUP BY
st.id,
st.name
I have 2 tables with below description.
table 1: customer, columns : customer_id, source
table 2: source, columns: source, rank
one customer would have many sources, each source has a particular rank in the rank table, i need to fetch the data in such a way that for each individual customer which ever has a lowest ranked source i need to fetch those records.
Here is an example:
customer table data is
1 abc
2 efg
3 abc
1 efg
1 hij
2 hij
source table data is
abc 2
hij 1
efg 3
the result set should be:
1 hij
2 hij
3 abc
You could use either of the two queries below to satisfy your requirement.
QUERY 1
SELECT c.customer_id,
c.source
FROM customer c
INNER JOIN source s
ON c.source = s.source
WHERE s.rank = (SELECT Min(s1.rank)
FROM source s1 inner join customer c1 on s1.source = c1.source
WHERE c1.customer_id = c.customer_id)
QUERY 2
SELECT x.customer_id ,
c1.source
FROM
(SELECT c.customer_id ,
MIN(s.rank) AS MinRank
FROM customer c
INNER JOIN SOURCE s ON c.source = s.source
GROUP BY c.customer_id) x
INNER JOIN customer c1 ON x.customer_id = c1.customer_id
INNER JOIN SOURCE s1 ON s1.source = c1.source
AND s1.rank = x.MinRank;
UPDATE 1
This update is in response to your comment for 3 tables rather than 2 tables. The query below extends Query 1 when your schema is spread across 3 tables.
SELECT c.customer_id,
s.source_name
FROM customer c
INNER JOIN source s
ON c.cust_id = s.cust_id
INNER JOIN rank r
ON s.source_name = r.source_name
WHERE r.rank = (SELECT Min(r1.rank)
FROM customer c1
INNER JOIN source s1
ON s1.cust_id = c1.cust_id
INNER JOIN rank r1
ON r1.source_name = s.source_name
WHERE c1.cust_id = c.cust_id);
For Oracle:
select d.customer_id, d.source
from (
select
c.customer_id,
s.source,
row_number() over (partition by c.customer_id order by s.rank asc) as rn
from customer c
join source s
on c.source = s.source
) d
where d.rn = 1
;
A much simpler way. Try this -
select c.cid,c.sourceid,min(s.rankid)
from customer c inner join sourc s
on (c.sourceid=s.sourceid)
group by c.cid order by c.cid asc
Here's an SQLFiddle
Select a.customer_id,b.source
from
(select c.customer_id,min(s.rank) as rank
from customer c
inner join source s
on c.source=s.source
group by c.customer_id) as a
inner join source b
on a.rank = b.rank
staff table:
id name
1 tony
2 sony
3 bony
4 rony
etc.
xyz table:
current staff, next staff, hod staff, vp staff.
1 2 3 4
2 3 1 4
4 3 2 1
etc.
when is run
`select * from xyz,`
I need a query which instead of giving me id it will actually give me names of that staff from staff table.
You need to join staff table more than once
SELECT b.name AS current_staff,
d.name AS next_staff,
c.name AS hod_staff,
e.name AS vp_staff
FROM xyz a
INNER JOIN staff b
ON a.current_staff = b.id
INNER JOIN staff c
ON a.hod_staff = c.id
INNER JOIN staff d
ON a.next_staff = d.id
INNER JOIN staff e
ON a.vp_staff = e.id
If I understand you right, I guess you just could use a multiple join here:
select a.name, b.name, c.name, d.name from xyz
inner join staff as a on a.id = xyz.currentstaff
inner join staff as b on b.id = xyz.nextstaff
inner join staff as c on c.id = xyz.hodstaff
inner join staff as d on d.id = xyz.vpstaff
I have database with following data structure with sample data. Each company have multiple members. The relationship is in the company_member table. Please note only required fields I have given below.
company
id title
1 company-1
2 company-2
company_member
companyid memberid
1 1
1 2
1 3
2 4
2 5
2 6
member
id firstname member_type_id
1 Name-1 2
2 Name-2 3
3 Name-3 3
4 Name-4 3
5 Name-5 2
6 Name-6 1
member_type
id user_level
1 0
2 1
3 2
I want list of unique companies with one member from each. But the member should be the lowest user_level within the company. i.e, following result should come;
result
companyid company_title memberid member_name user_level
1 company-1 1 Name-1 1
2 company-2 6 Name-6 0
I want to know how to get one member with lowest user level among the same company.
This is a bit complicated one, however this is one way of doing it using not exists, for bigger tables its wise to use not exits since using pivot tables it will not use index.
select
c.id,
c.title,
m.id as member_id,
m.firstname,
mt.user_level
from company_member_map cmp
join company c on c.id = cmp.companyid
join member m on m.id = cmp.memberid
join member_type mt on mt.id = m.member_type_id
where not exists
(
select 1 from company_member_map t1
join member t2 on t2.id = t1.memberid
join member_type t3 on t3.id = t2.member_type_id
where
t1.companyid = cmp.companyid
and t3.user_level < mt.user_level
)
DEMO
Finally I found solution:
select c.id companyid, c.title company_title, m.firstname member_name, mt.user_level
from company c
inner join company_member cm on cm.companyid = c.id
inner join member m on m.id = cm.memberid
inner join member_type mt on mt.id = m.member_type_id
inner join
(select c1.id companyid, mt1.user_level
from company c1
join company_member cm1 on cm1.companyid = c1.id
join member m1 on m1.id = cm1.memberid
join member_type mt1 on mt1.id = m1.member_type_id
group by c1.id,m1.id
order by user_level asc
) sq on c.id = sq.companyid and sq.user_level = mt.user_level
group by c.id;
Correct this, if anyone have better solution or simplified solution.
Check this SQL Fiddle
I have this database structure:
TBL_A | TBL_B | TBL_C | TBL_D | TBL_E
-------+---------+---------+---------+----------
id | id_tbla | id_tbla | id_tbla | id
name | id_user | id_user | id_user | name_tbla
... | is_bool | | weight | id_user
Here is what I'm trying to achieve :
SELECT
a.id,
a.name,
b.is_bool,
count(c.id_user) AS nb_views,
sum(d.weight) AS total_weight,
count(distinct e.id_user) AS distinct_users,
FROM TBL_A AS a
LEFT JOIN (TBL_B AS b) on (b.id_tbla = a.id)
LEFT JOIN (TBL_C AS c) on (c.id_tbla = a.id)
LEFT JOIN (TBL_D AS d) on (d.id_tbla = a.id)
LEFT JOIN (TBL_E AS e) on (e.name_tbla = a.name)
where a.id = 1 and e.id_user = 1
The query is performed but the results (nb_views, total_weight, distinct_users) are wrong. Any idea why?
You're trying to compute too many aggregates in one query.
Enita non sunt multiplicanda praeter necessitatem
(Latin, "entities are not to be multiplied beyond necessity")
Your tables B, C, D, and E are produced Cartesian Products against each other. Suppose the
given row in A matches:
3 rows in B
6 rows in C
4 rows in D
1 row in E
The total number of rows in the result is 3 * 6 * 4 * 1 = 72 rows. So your count(c.id_user) is 12 times what it should be, your sum(d.weight) is 18 times what it should be, etc.
The simplest remedy is to compute each of these aggregates in a separate query:
SELECT a.id, a.name, COALESCE(b.is_bool, FALSE) AS is_bool
FROM TBL_A AS a LEFT JOIN TBL_B AS b ON (b.id_tbla = a.id)
WHERE a.id = 1;
SELECT a.id, COUNT(c.id_user) AS nb_views
FROM TBL_A AS a LEFT JOIN TBL_C AS c ON (c.id_tbla = a.id)
WHERE a.id = 1;
SELECT a.id, SUM(d.weight) AS total_weight,
FROM TBL_A AS a LEFT JOIN TBL_D AS d ON (d.id_tbla = a.id)
WHERE a.id = 1;
SELECT a.id, COUNT(DISTINCT e.id_user) AS distinct_users,
FROM TBL_A AS a LEFT JOIN TBL_E AS e
ON (e.name_tbla = a.name AND e.id_user = 1)
WHERE a.id = 1;