count by column name [closed] - sql

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
Trans|Store|Date
1 | 10 |9/01/13
2 | 10 |9/01/13
3 | 20 |9/01/13
4 | 10 |9/02/13
What I am trying to query is
(For Date = 9/01/13)
Store|#Trans|Date
10 | 2 |9/01/13
20 | 1 |9/01/13
How do I achieve this any ideas ?

You may try this:-
select store, count(*) as Transt from table
where Date = '9/01/13'
group by store

If that doesn't do it, this might.
Select
count(Store) as #Trans,
Store
from
tablename
where
Date = '9/01/13'
group by
Store
This smells like homework something fierce though.

Related

SQL query to return only 11 digit numeric values [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 2 years ago.
Improve this question
Actual data:
|31345678921|
|56789056789|
|56780345678|
|4567 |
|3456 |
|00678596 |
|03456788453|
|ASA 2344 |
|34565 |
|BBq23 |
|DNF LIMIT |
Required data:
|31345678921|
|56789056789|
|56780345678|
|03456788453|
One method is:
where actual not like '%[^0-9]%' and
len(actual) = 11
That is, the value has no non-digits and is 11 digits long.
Another method is:
where try_convert(numeric(11,0), actual) between 10000000000 and 99999999999
That is, when converted to a numeric, the range suggests that it is 11 digits.
And a third method is:
where actual like '[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
SQL Server supports character classes with like, so you can just like [0-9] 11 times.

Unit record of each type [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
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
Improve this question
On SQL Server 2008,I need to select the first distinct occurrence of each person in the following table:
ID WeeklyAvg MonthlyAvg
1 8 0
1 7 3
2 9 1
2 6 4
2 6 4
.......................
....................
The output should be:
1 8 0
2 9 1
How do I achieve this?
Even better if I can avoid putting all the 'distinct' columns in the group by clause,just because sql server restricts so.
Appreciate the help.
You can use ROW_NUMBER to get the "first" row:
SELECT ID, WeeklyAvg, MonthlyAvg
FROM
(
SELECT ID, WeeklyAvg, MonthlyAvg,
ROW_NUMBER() OVER (PARTITION BY ID ORDER BY ID) RowNum
FROM {table}
) A
WHERE RowNum = 1
Note that the "first" row will be arbitrary unless you specify a particular order.

Assign values to sorted result in SQL [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 9 years ago.
Improve this question
I have got table like below after sorting in SQL:
M_ID
-----
2013/01
2013/02
2013/03
2013/04
2013/05
2013/06
Now I want to assign each entries a particular value like below
M_ID Days
--------------
2013/01 20
2013/02 30
2013/03 40
2013/04 50
2013/05 60
2013/06 70
So, can you please let me know how to do that in SQL Query?
Do you mean something like this (presuming sql-server)?
SELECT M_ID,
Days = (ROW_NUMBER()OVER(ORDER BY M_ID) + 1) * 10
FROM dbo.TableName
Demo

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

Remove trailing zeros from a decimal column [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
How can I remove the trailing zero from the below table using SQL:
SKU SIZE
ABC 35.000000
QWE 36.000000
RTY 37.000000
VGY 38.000000
Expected results:
SKU SIZE
ABC 35
QWE 36
RTY 37
VGY 38
In sql server cast the column containing decimal values to Int
Select SKU,CAST (Size AS INT)
From tablename
For MySql use integer division function DIV
SELECT SKU,Size DIV 1
From Tablename
I suggest
SELECT sku, int(size)
FROM tablename
It's less typing. ;-)