我有以下查询返回12个月的数据。最初的查询是14天,我用datediff
方法改变了一天到一个月来获得14个月的数据,但我只获得了12个月的数据。任何人都可以检查,看看为什么?
Select 'playing' As activity
--,ad.xDate
,min(ad.xDate) As xDate
,Isnull(sum(t.TimePerDay),0) As TimePerDay
From AllDates As ad With (Nolock)
Left Join @test As t On ad.xDate = t.date
GROUP BY datepart(Month, ad.xDate)
--ORDER BY YEAR(datepart(Month, ad.xDate)) DESC, MONTH(datepart(Month, ad.xDate)) DESC, DAY(datepart(Month, ad.xDate))
ORDER BY MIN(ad.xDate)
option (maxrecursion 0)
END
答案 0 :(得分:1)
2件事:
@LastXMonths = -14
@MinDate
所以@MaxDate
仅在GROUP BY datepart(Month, ad.xDate)
之前13个月。
接下来,这句话:
+1
它会返回1到12之间的数字,所以你永远不会超过12个月。
删除第一个语句中的Select 'playing' As activity
,ad.xDate
,Isnull(sum(t.TimePerDay),0) As TimePerDay
From AllDates As ad With (Nolock)
Left Join @test As t On ad.xDate = t.date
GROUP BY ad.xDate
ORDER BY ad.xDate
option (maxrecursion 0)
,并将最终选择更改为:
public class First_Fragment extends Fragment {
View myView;
TextView tv;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
myView = inflater.inflate(R.layout.first_layout, container, false);
tv = (TextView) myView.findViewById(R.id.textView2);
return myView;
}
public boolean executeCommand(){
System.out.println("executeCommand");
Runtime runtime = Runtime.getRuntime();
try
{
Process mIpAddrProcess = runtime.exec("/system/bin/ping -c 1 8.8.8.8");
int mExitValue = mIpAddrProcess.waitFor();
System.out.println(" mExitValue " + mExitValue);
if(mExitValue==0){
return true;
}else{
return false;
}
tv.setText(mExitValue);
}
catch (InterruptedException ignore)
{
ignore.printStackTrace();
System.out.println(" Exception:"+ignore);
}
catch (IOException e)
{
e.printStackTrace();
System.out.println(" Exception:"+e);
}
return false;
}
}
答案 1 :(得分:1)
您需要按ad.xDate的月份和年份分组。一年只有十二个月你应该看到你的结果最早的两个月(14 - 2)月总数太大,因为它们实际上代表了两个日历月的组合。
它适用于您的原始版本,因为任何月份都有超过14天。如果您尝试将旧查询扩展超过31天(或28或29,30几个月),那么您会再次发现同样的问题。
...
SELECT
'playing' As activity,
min(ad.xDate) As xDate,
Isnull(sum(t.TimePerDay), 0) As TimePerDay
FROM AllDates As ad Left Outer Join @test As t On ad.xDate = t.date
GROUP BY Year(ad.xDate), Month(ad.xDate) /* <--- change here */
ORDER BY xDate