我有2个表,希望从条件中得到2个表的行和。 这是我的表格:
我想要来自2个表的行总和,例如:
if sk not null sum sk but if it is null sk = ss
例如,sh2的总和为:19 + 5 + 11 + 5 = 40
我真的需要那个,请帮忙!
答案 0 :(得分:0)
下一个sql将返回表中的总和,按名称分组:
select name, sum(total) total from
(select name, sum(coalesce(sk,ss)) total
from table1
group by name
union all
select name, sum(coalesce(sk,ss)) total
from table2
group by name
) t
group by name
子查询中的“group by”子句非常重要,因为表是否返回了很多行,它们将减少查询中“group by”的行数,从而优化时间并减少使用内存。
答案 1 :(得分:0)
如果sk不为空,请使用% --- Executes on button press in load.
function load_Callback(hObject, eventdata, handles)
% hObject handle to load (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% [flnm, flpth,cCheck]=uigetfile({'*.txt'},...
% 'File Select',...
% 'Multiset', 'on');
[flnm, flpth,cCheck]=uigetfile({'*.txt'},...
'File Select',...
'Multiselect', 'on');
%fullpathname=strcat(pathname, filename);
%text=audioread(fullpathname);
%set(handles.path_song, 'String',fullpathname);%show full path name in the listbox
assignin('base','flnm',flnm);
assignin('base','flpth',flpth);
assignin('base','cCheck',cCheck);
%If the user did not press the cancell button
%If the user did not press the cancell button
% if(cCheck==0)
if(cCheck)
%Reset selection to first entry
set(handles.playlist,'Value',1);
%Show selected filenames
set(handles.playlist,'String',flnm);
end
%LIST------------------------Display Files---------------------------------
function playlist_Callback(hObject, eventdata, handles)
% hObject handle to playlist (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns playlist contents as cell array
% contents{get(hObject,'Value')} returns selected item from playlist
listStr=get(hObject,'String');
listVal=get(hObject,'Value');
%Display Selected files
if(iscell(listStr))
% % % fileName=playlistStr(listVal);
fileName=char(listStr(listVal));
fprintf('Selected file:%s\n',fileName);
else
fileName=listStr;
fprintf('Selected file:%s\n',fileName);
end
选择sk。否则(如果sk为null),将使用ss。
coalesce
两个表格,UNION ALL
结果:
GROUP BY