Counting frequency of customer [closed] - sql

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have this table on sql sever
cstomer |No_Nota
CUS000 | 98342
CUS000 | 98343
CUS000 | 98343
CUS001 | 98355
CUS001 | 98355
I would like to count the frequency of each customer. For similar number of no_nota the value is 1.
I'd like a result like this:
cstomer |Frequent
CUS000 | 2
CUS001 | 1

You want the distinct count of the column no_nota, so that's what you should select...
select customer, count(distinct no_nota) as frequent
from my_table
group by customer

You want to count the result of your select query:
SELECT COUNT(expression)
FROM tables
WHERE predicates;
Example:
SELECT COUNT(No_Nota) FROM your_table WHERE No_Nota > 0;

Sounds like you just want a group by statement to get a count of all individual customers. Something like:
SELECT cstomer, SUM(1) as Frequent FROM table GROUP BY cstomer, No_Nota

Related

Aggregation depends from value [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have a sample table
ID
Invoice_ID
docType
1
100
email
2
100
sms
3
200
email
4
200
email
5
300
sms
and I have to get results with only rows that have invoices with email and sms docType. For this example its only invoice_id 100
One approach is to query those type and count the distinct number of different types that appear:
SELECT invoice_id
FROM invoices
WHERE doctype IN ('sms', 'email')
GROUP BY invoice_id
HAVING COUNT(DISTINCT doctype) = 2
One method uses aggregation:
select invoice_id
from t
where docType in ('email', 'sms')
group by invoice_id
having count(distinct doctype) = 2;

How to get column values as a table column in sql [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
This is my table defined in the database:
pi_value | Status
------------+-------------------
500.000 | Bank Submitted
500.0000 | Bank Submitted
1000.0000 | Maturity Received
4000.0000 | Bank Submitted
50.0000 | Maturity Received
I want the output to look like this:
Maturity Received | Bank Submitted |
------------------+----------------+
1050.0000 | 5000.0000 |
------------------+----------------+
use conditional aggregation
select sum(case when status='Bank Submitted' then pi_value else 0 end),
sum(case when status='Maturity Received' then pi_value else 0 end) from table
As you say in a comment that the status is not before-known, just select the mere data and have your app or website do the layout:
select status, sum(pi_value) as total
from mytable
group by status
order by status;
Whatever programming language you are using, it will be easy to simply loop through the result set and fill some grid with it.
Alternative:
If you want to get the pivoted data right away, you'll have to use dynamic SQL instead. This means you select the data needed to build the query first, then you build the query from it and run it. Here is how to get the statuses:
select distinct status from mytable order by status;
Then loop through these and construct a query like shown in Zaynul Abadin Tuhin's answer. Then run that query.

query to check multiple revsions on a table [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Need a query to check ids with multiple revisionnums
id reviionnum
1 0
2 0
1 1
3 1
2 1
The query should result
id revisionnum
3 1
Please help
If you want ids with only one revision number, you can use aggregation:
select id, min(revissionnum)
from t
group by id
having count(*) = 1;
The min() is the value if there is only one row.

SQL select statement to Exclude a column from being sorted [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am looking for a way to exclude a single column from being sorted using SQL select statement.
Lets say a table has 10 columns and when an ORDER BY is done on a specific column using select statement, all the 10 columns are sorted. Is it possible to exclude any specific column (say column 6) from being sorted? If so how.
**
Before
ID name
-----------
1 papaya
2 apple
3 strawberry
4 banana
Required
ID name
-----------
1 apple
2 banana
3 papaya
4 strawberry
Appreciate your help
This will generate the ids and they will be always shown in order but that won't be the actual id, you might need that as well
SELECT #a:=#a+1 id,name from tableName order by name

Having 1 distinct column and a sum value from another column [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I would like to have a solution to this question:
I have a datatable here, as shown below
cid amount
1 5
1 10
2 2
3 5
3 7
3 11
Now I need to write a statement that returns a distinct cid, and the sum of the amount column for each cid. The table should look as below:
cid amount
1 15
2 2
3 23
What is the best way to do this? Thanks.
select cid, sum(amount) amount from table group by cid
Try this way:
select cid, sum(amount)
from tab
group by cid
select cid, sum(amount)
from table_name
group by cid;
select cid,sum(amount) from c1 group by cid