将URL添加到聚合变量SQL

时间:2015-05-12 06:31:59

标签: sql postgresql url aggregate

我加入两个SQL表(applications,areas_application1)并按区域分组。 在第一个表中,我有列url_title和url_link。我想在eah区域显示url_title的列表,并为每个标题添加href链接。我尝试按空间聚合url_title,但我不知道如何为每个url_title聚合url_link。 这个版本不起作用。

public class JSONParser {

    static InputStream is = null;
    static JSONObject jObj = null;
    static String json = "";

    // constructor
    public JSONParser() {

    }

    // function get json from url
    // by making HTTP POST or GET mehtod
    public JSONObject makeHttpRequest(String url, String method,
            List<NameValuePair> params) {

        // Making HTTP request
        try {

            // check for request method
            if(method == "POST"){
                // request method is POST
                // defaultHttpClient
                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(url);
                httpPost.setEntity(new UrlEncodedFormEntity(params));

                HttpResponse httpResponse = httpClient.execute(httpPost);
                HttpEntity httpEntity = httpResponse.getEntity();
                is = httpEntity.getContent();

                System.out.println(is + "post");

            }else if(method == "GET"){
                System.out.println("1");
                // request method is GET
                DefaultHttpClient httpClient = new DefaultHttpClient();
                System.out.println("2");
                String paramString = URLEncodedUtils.format(params, "utf-8");
                System.out.println("3");
                url += "?" + paramString;
                System.out.println("4");
                HttpGet httpGet = new HttpGet(url);
                System.out.println("5");

                HttpResponse httpResponse = httpClient.execute(httpGet);
                System.out.println("6");
                HttpEntity httpEntity = httpResponse.getEntity();
                System.out.println("7");
                is = httpEntity.getContent();
                System.out.println(is + "get");
            }           


        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            json = sb.toString();

            Log.i("TagCovertS", "["+json+"]");


        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }

        // try parse the string to a JSON object
        try {
            jObj = new JSONObject(json);
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }

        // return JSON String
        return jObj;

    }

}

1 个答案:

答案 0 :(得分:0)

以下查询应该有所帮助:

SELECT 
  app.area,
  ar.cartodb_id,
  count(app.area),
  array_to_string(array_agg('<a href="'||app.url_link||'">'||app.url_title||'</a>'), 
' '),
  ar.the_geom_webmercator 
FROM applications as app 
JOIN areas_application1 as ar ON ar.name = app.area 
GROUP BY app.area, ar.the_geom_webmercator, ar.cartodb_id;