Database table : drm_drug
table fields: medicine, the Treasury , the company , the number of
Note:
1. presence of a drug with a maximum of two companies , but there are multiple warehouses
2. the same warehouse, the same drugs if there are two companies , then there are two cases : ① the two companies is a positive number and the number of companies and ② two negative
3. If it is the case ①, then there are two cases : ③ two the number of companies are positive ④ a company is positive, the other negative
Requirements: aggregate statistics for each company by company number ( query result fields: company , amount )
⑴. If the case ④, only to take a positive number of the company, and which the amount of data for both the number of companies and ( after adding positive and negative number )
⑵. If the case ②, then the two data do not take
⑶. If conditions ③, then the two data are taken
------ Solution ------------------------------------ --------
select c. Corporation , c. Quantity
from (
(Select pharmaceuticals , sum ( amount ) as the number of
from drm_drug
group by drugs
having sum ( amount )> 0) a
inner join
(Select pharmaceutical company
from drm_drug
where number > 0) b
on A. Drug = B. Drug
) c
------ For reference only ------------------------------ ---------
said plainly, is the table associated with the issue . .
a table setting vice key associated primary key of another table .
landlord is not write or database sql statement will not build ?
sql statement saying the truth I did not need a written
------ For reference only ---------------------- -----------------
select company , quantity
from
(Select pharmaceuticals , sum ( amount ) as the number of
from drm_drug
group by drugs ) a
inner join
(Select pharmaceutical company
from drm_drug
where number > 0) b
on A. Drug = B. Drug
------ For reference only ---------------------------------- -----
⑴. If the case ④, only to take a positive number of the company, and which the amount of data for both the number of companies and ( after adding the number of positive and negative )
⑵. If the case ②, then the two data do not take
⑶. If conditions ③, then the two data are taken
were written three sql then union.
------ For reference only -------------------------------------- -
is the sql statement to write a statement that it can be posted ?
------ For reference only -------------------------------------- -
Thank you very much , just verify indeed the case ;
没有评论:
发表评论