我正在使用以下数据库架构:
作者
Au_id
Au_lname
Au_fname
Phone
Address
City
State
Country
Postalcode
PUBLISHERS
Pub_id
Pub_name
City
State
TITLES
Title_id
Title, Type
Pub_id
Price
Advance
Total_sales
Notes
Pubdate
Contract
TITLEAUTHOR
Au_id
Title_id
店铺
Stor_id
Stor_name
Stor_address
City
State
Country
Postalcode
Payterms
销售
Stor_id
Ord_num
Sdate
SALESDETAIL
Stor_id
Ord_num
Title_id
Qty
Discount
我需要找到没有重复的Publisher Id
和Publisher Name
,他们已经在IS或IT上发布销量最高的书籍。
所以我写了这个查询:
SELECT DISTINCT PUBLISHERS.PUB_ID
,PUBLISHERS.PUB_NAME
FROM PUBLISHERS
INNER JOIN TITLES ON PUBLISHERS.PUB_ID = TITLES.PUB_ID
INNER JOIN SALESDETAIL ON TITLES.TITLE_ID = SALESDETAIL.TTITLE_ID
WHERE TITLES.TYPE = ‘IS’
OR TITLES.TYPE = ‘IT’
AND SALESDETAIL.QTY = MAX(SELECT COUNT(*) FROM SALESDETAIL GROUP BY TITLE_ID);
如果做得对,请帮助我吗?
答案 0 :(得分:0)
如果你使用SQL Server,听起来你可以使用SELECT
p.PUB_ID
,p.PUB_NAME
,max(sd.qty) OVER (PARTITION BY p.pub_id,p.pub_name ORDER BY p.pub_name)
AS highest_sales
FROM PUBLISHERS p
INNER JOIN TITLES t ON p.PUB_ID = t.PUB_ID
INNER JOIN SALESDETAIL sd ON t.TITLE_ID = sd.TITLE_ID
WHERE (t.TYPE = 'IS'
OR t.TYPE = 'IT');
函数的窗口版本:
//generate your user data
User[] userData = ...;
//add an array of Objects to a JList using the constructor
//(all classes extend java.lang.Object, including User)
JList foo = new JList(userData);
//overwrite the ListCellRenderer. This will take care of just displaying
//the name of the user
foo.addListSelectionListener(new ListSelectionListener(){
JLabel rv = new JLabel();
@Override
public Component getListCellRendererComponent(JList list,
Object value, int index, boolean isSelected,
boolean cellHasFocus){
String s = (value != null && value instanceof User)? ((User)value).name:"";
rv.setText(s);
if (isSelected) {
rv.setBackground(list.getSelectionBackground());
rv.setForeground(list.getSelectionForeground());
} else {
rv.setBackground(list.getBackground());
rv.setForeground(list.getForeground());
}
rv.setEnabled(list.isEnabled());
rv.setFont(list.getFont());
rv.setOpaque(true);
return rv;
}
});
//Now overwrite the ListSelectionListener which will take care of getting
//user object when the user clicks it
foo.addListSelectionListener(new ListSelectionListener(){
@Override
public void valueChanged(ListSelectionEvent lse) {
User selectedValue = (User)((JList)lse.getSource()).getSelectedValue();
//now you can do something with the User Object that was just selected
updateDatabase(selectedValue.id, selectedValue.name,
selectedValue.location);
});