Distinct or group by date

Shambhu Rai 1,406 Reputation points
2022-03-07T04:45:07.09+00:00

Hi Expert,

how to distinct or group by below records

create table main5 (unit int, saledate date ,data int)

insert main5

values(1,'20210407',10),
(1,'20210407',20),
(1,'20210407',30)

Expected output

180505-image.png

i tried

select distinct sum(unit),saledate,data from main5 group by (unit),saledate,data
but not getting proper data

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,768 questions
SQL Server Integration Services
SQL Server Integration Services
A Microsoft platform for building enterprise-level data integration and data transformations solutions.
2,458 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,555 questions
{count} votes

4 answers

Sort by: Most helpful
  1. LiHong-MSFT 10,046 Reputation points
    2022-03-07T06:10:11.787+00:00

    Hi @Shambhu Rai
    You can just use simple GROUP BY with aggregate function(SUM) like this:

    SELECT unit,saledate,SUM(data) AS SUM_data  
    FROM #main5  
    GROUP BY unit,saledate  
    

    Or use window function OVER( PARTITION BY ...) like this:

    SELECT DISTINCT unit,saledate,SUM(data)OVER(PARTITION BY unit,saledate) AS SUM_data  
    FROM #main5  
    

    PARTITION BY does not affect the number of rows returned, but it changes how a window function's result is calculated.And that's why we need to add ‘DISTINCT’in this query.
    The OVER clause defines a window or user-specified set of rows within a query result set. A window function then computes a value for each row in the window.
    You can use the OVER clause with functions to compute aggregated values such as moving averages, cumulative aggregates, running totals, or a top N per group results.

    In this issue,I tend to recommend using 'GROUP BY'

    Best regards,
    LiHong

    1 person found this answer helpful.
    0 comments No comments

  2. Shambhu Rai 1,406 Reputation points
    2022-03-07T05:32:07.263+00:00

    i got this

    SELECT distinct saledate, --sum(data) as data,
    sum(data) OVER(PARTITION BY unit ) AS ROWNO ,unit
    FROM main5 group by saledate,unit,data

    0 comments No comments

  3. Sherlan Emmanuel Burgonia 86 Reputation points
    2022-10-04T07:30:26.21+00:00

    Hi @LiHong-MSFT I'm following this thread and can I ask what if I want to group that by YYYY-MM only and not YYYY-MM-DD?

    0 comments No comments

  4. Tom Cooper 8,466 Reputation points
    2022-10-04T13:17:23.173+00:00

    @Sherlan Emmanuel Burgonia - one way to sum by year and month would be

    SELECT unit,Year(saledate) As Year,Month(saledate) AS Month, SUM(data) AS SUM_data  
    FROM #main5  
    GROUP BY unit,Year(saledate),Month(saledate)  
    

    Tom

    P.S. When you have a question related to some topic, it is best to create a new thread, avoid asking a new question in a current thread. That can make things confusing. If you think it would be helpful, you can include a link to the old thread in your new question.

    0 comments No comments