I have to create a MS SQL Query on the table below.
Consider I have following single table:
I want to get the following result by grouping on column1:
How can I accomplish this?
Use COUNT with GROUP BY.
Query
SELECT column1, 'N/A' as column2, 'N/A' as column3,
COUNT(column1) AS column4_amount
FROM your_table_name
GROUP BY column1;
SELECT column1, 'N/A' as column2, 'N/A' as column3, COUNT(1) AS column4_amount
FROM table_name
GROUP BY column1
if you don't need column2 and column3 then try below:
SELECT column1, COUNT(1) AS column4_amount
FROM table_name
GROUP BY column1
Update:
As you want results which has more than one occurrence, try this:
SELECT column1, 'N/A' as column2, 'N/A' as column3, COUNT(1) AS column4_amount
FROM table_name
GROUP BY column1
HAVING COUNT(1) > 1
You should use GROUP BY with column1 as GROUP BY helps you group all the items with the same name.
To count total items under a group of items, from the table with the same name, use the COUNT aggregate function. Now you need to check that each item exists more than once. Use the HAVING clause which takes all the groups that have more than two items.
SELECT column1, 'N/A' as column2, 'N/A' as column3, COUNT(1) AS column4_amount
FROM your_table_name
GROUP BY column1
HAVING COUNT(1) > 1
Related
I am trying to get a new column with a concatenation of all distinct row values. This aggregation would be based on other columns.
I have tried the following but I get the same values repeated in the new column (A1, A1, A4). I need the concatenation to be distinct.
SELECT
STRING_AGG(COLUMN1, ', ') AS COLUMN1_ALIAS
,COLUMN2
,COLUMN3
,COLUMN4
FROM TABLE
GROUP BY COLUMN2 ,COLUMN3 ,COLUMN4
It looks like you want windowing rather than aggregation. Unfortunately, string_agg does not support over() in SQL Server ; neither does it support distinct in its aggregated form.
We could work around it with subqueries ; it is probably more efficient to deduplicate and pre-compute the aggregates first, then join with the original table:
select t.*, x.column1_alias
from mytable t
inner join (
select column2, column3, column4, string_agg(column1, ', ') as column1_alias
from (select distinct column1, column2, column3, column4 from mytable) t
group by column2, column3, column4
) x on x.column2 = t.column2 and x.column3 = t.column3 and x.column4 = t.column4
Side note : in a database that supports both over() and distinct on string aggregation, the query would phrase as:
select t.*,
string_agg(distinct column4, ', ')
over(partition by column2, column3, column4) as column1_alias
from mytable t
I want to sum City4, and the two misspellings together as one row. Any input on how to do this?
SELECT column1,
column2,
count(column3),
Sum(Column4)
FROM TABLE
AND column1 IN ('state1',
'State2',
'State3')
AND column2 IN ('City1',
'City2',
'City3',
'City4',
'City4 misspelled1',
'City4 misspelled 2')
GROUP BY column1,
column2
ORDER BY column1;
How can I aggregate and arrive to these results?
SELECT *
FROM TABLE
GROUP BY Column1
Assuming the columns actually contain the literal string values 'TRUE' and 'FALSE', we could use:
SELECT
Column1,
MAX(Column2) AS Column2,
MAX(Column3) AS Column3
FROM yourTable
GROUP BY
Column1;
When I try to select couple of columns with count, I get the following error:
Selected non-aggregate values must be part of the associated group
My query is something like this.
SELECT COUNT(1), COLUMN1, COLUMN2
FROM TABLE-NAME
If you're after a count for each combination of COLUMN1 and COLUMN2:
SELECT COUNT(1), COLUMN1, COLUMN2 FROM TABLE_NAME GROUP BY COLUMN1, COLUMN2
If you're after a count of all records in the table:
SELECT COUNT(1) OVER (), COLUMN1, COLUMN2 FROM TABLE_NAME
I have the following SQL
select count(*) col, column1, column2, column3 from TempTable
group by column1, column2, column3
order by 1 desc
so the column generated by the count will return a number and there are 17 rows that do not have the number 1 (duplicate rows as columns 1, 2 and 3 are primary keys) and i want to delete any that have the count greater than 1?
You can use the having-clause:
select count(*) col, column1, column2, column3
from TempTable group by column1, column2, column3
having count(*) > 1
order by 1 desc
To delete:
delete tt
from TempTable tt
inner join (select count(*) col, column1, column2, column3
from TempTable group by column1, column2, column3
having count(*) > 1) tmp
on tmp.column1 = tt.column1
and tmp.column2 = tt.column2
and tmp.column3 = tt.column3
First you insert the data in temporary table:
select count(*) col, column1, column2, column3
into #temp
from TempTable group by column1, column2, column3 order by 1 desc
Then, you delete the data, and insert it from the #temp table:
delete from TempTable
go
insert into TempTable select column1, column2, column3 from #temp
go