按x排序然后按SQL Server中的y列排序

时间:2017-12-24 06:54:38

标签: sql sql-server tsql

考虑像

这样的表格
   debit    credit  code
-----------------------------
    0       10      5
    5       0       3
    0       11      2
    0       15      1
    7       0       6
    6       0       2
    5       0       1

我需要生成这样的结果集,首先是借记,然后按代码列排序:

debit   credit  code
----------------------------
5       0       1
6       0       2
5       0       3
7       0       6
0       15      1
0       11      2
0       10      5

4 个答案:

答案 0 :(得分:7)

你可以使用它。

debit       credit      code
----------- ----------- -----------
5           0           1
6           0           2
5           0           3
7           0           6
0           15          1
0           11          2
0           10          5

结果:

svn status

答案 1 :(得分:3)

请使用 order by cast(cast(code as varchar(50)) + cast(debit as varchar(2)+ cast(credit as varchar(2) as int) 中的下方,您将获得您要查找的输出

PendingIntent pendingIntent = PendingIntent.getService(this,0  /* Request code */
        , intent,PendingIntent.FLAG_UPDATE_CURRENT);

答案 2 :(得分:2)

package com.example.mujtaba.quizzer;

import android.app.ProgressDialog;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.StringRequest;
import com.example.mujtaba.quizzer.Activity.QuizMaking;
import com.example.mujtaba.quizzer.Activity.QuizTaking;

import org.w3c.dom.Text;

import java.util.HashMap;
import java.util.Map;

public class Login extends AppCompatActivity {
    private Button button;
    private TextView username;
    private TextView password;
    private Spinner role;
    private String url = "http://localhost:8080/users/signup";
    private RequestQueue queue;
    private ProgressDialog progress;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        username=(TextView) findViewById(R.id.username);
        password=(TextView) findViewById(R.id.password);
        button=(Button) findViewById(R.id.button);
        role = (Spinner) findViewById(R.id.role);

// Create an ArrayAdapter using the string array and a default spinner layout
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
                R.array.role_spinner, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
        role.setAdapter(adapter);
    }


    public void Quiz(View v) {   //select a new activity on the basis of role




        StringRequest MyStringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                //This code is executed if the server responds, whether or not the response contains data.
                //The String 'response' contains the server's response.
            }
        }, new Response.ErrorListener() { //Create an error listener to handle errors appropriately.
            @Override
            public void onErrorResponse(VolleyError error) {
                //This code is executed if there is an error.
            }
        }) {
            protected Map<String, String> getParams() {
                Map<String, String> MyData = new HashMap<String,String>();
                MyData.put("Username", username.getText().toString() ); //Add the data you'd like to send to the server.
                MyData.put("Password",password.getText().toString());
                MyData.put("Role",role.getSelectedItem().toString());
                MyData.put("Score","0");
                return MyData;
            }
        };

        queue.add(MyStringRequest);
    }
}

试试上面的代码

WOrking fiddle here

答案 3 :(得分:0)

使用此选项可以帮助您:

SELECT debit, code, credit  
FROM table a
WHERE debit > 0
or debit = 0
order by debit  code, credit desc

SELECT debit, code, credit  
FROM table a
WHERE debit > 0
or debit = 0
group by debit, code, credit
order by debit  code, credit desc