Group By Help Needed

Hi @harishgg1,
Thank you for your reply. Can I get something like this?

17F62645-6BA0-4270-B17C-00041CCC92C0	96171063	1	190129	2020-04-01 15:00:14.000	1.04.2020 15:01	1573	Bim 840 Riot Points	1	32	32	1
539F7E12-8E9C-4F45-8AA0-027AF46E2C38	97173711	2	9019174	2020-04-01 17:52:34.000	2020-04-01 17:53:18.040	1582	Bim 10 TL Razer Gold Pin	1	10	10	1
Subtotal:									42	42	
031A482E-6C02-419D-B8A1-0F145DA2F079	98184487	3	9027765	2020-04-01 14:23:00.000	1.04.2020 14:24	1573	Bim 840 Riot Points	1	32	32	2
ED4AE4C3-5A80-49E3-AD81-1BC20C86C302	96184860	1	167834	1.04.2020 19:42	1.04.2020 19:42	1585	2100 ZA	1	5	5	1
Subtotal:									37	37	
Grand total:									79	79	

let me try !! :slight_smile:

Please @harishgg1, if it is NOT possible or it is better doing it via reports, please show me how to do it via SQL reports.

Cenk

sorry I dont have and idea about SQL Reports !!!

maybe if you could remote desktop me to your computer
I could take a look at it and try !!!

:slight_smile:

so is it possible with SQL query?

i can try !!!

to see if it is possible !!!

would you like me to ?? :slight_smile:

I appreciate slight_smile: please try to solve with query.

hi

i tried to do this !! hope this helps !! :slight_smile:

please click arrow to the left for DROP Create Sample Data

drop create sample data
drop table #Result
go 

create table #Result
(
Id varchar(50),
Val int
)
go 

insert into #Result  select '96171063',10
insert into #Result  select '97173711',20
insert into #Result  select '98184487',15
insert into #Result  select '96174860',5
insert into #Result  select '96173728',6
insert into #Result  select '98184487',7
insert into #Result  select '96173528',10
insert into #Result  select '98154487',11
insert into #Result  select '96173761',13
insert into #Result  select '97014860',20
insert into #Result  select '97064860',11
go 

SELECT 'data', * from #Result

Please click arrow to the left for SQL

SQL
;WITH [cte]
AS (
       SELECT
            SUBSTRING([#Result].[Id], 3, 2) AS [ok]
            , [#Result].[Val]
       FROM
            [#Result]
   )
      , [cte_1]
AS (
       SELECT   1 AS [rn], NULL AS [subtot], [cte].[ok], [cte].[Val] FROM   [cte]
   )
      , [cte_2]
AS (
       SELECT
            2 AS [rn]
            , ' sub total ' AS [subtot]
            , [cte].[ok]
            , SUM([cte].[Val]) AS [sumval]
       FROM
            [cte]
       GROUP BY
           [cte].[ok]
   )
      , [cte_3]
AS (
       SELECT
            3 AS [rn]
            , ' Grand Total ' AS [GrandTot]
            , NULL AS [ok]
            , SUM([cte].[Val]) AS [Grandtotal]
       FROM
            [cte]
   )
SELECT
    'SQL Output '
    , [a].[rn]
    , [a].[subtot]
    , [a].[ok]
    , [a].[Val]
FROM
(   SELECT
        [cte_1].[rn]
        , [cte_1].[subtot]
        , [cte_1].[ok]
        , [cte_1].[Val]
    FROM
        [cte_1]
    UNION ALL
    SELECT
        [cte_2].[rn]
        , [cte_2].[subtot]
        , [cte_2].[ok]
        , [cte_2].[sumval]
    FROM
        [cte_2]
    UNION ALL
    SELECT
        [cte_3].[rn]
        , [cte_3].[GrandTot]
        , [cte_3].[ok]
        , [cte_3].[Grandtotal]
    FROM
        [cte_3]) AS [a]
ORDER BY
    [a].[ok]
    , [a].[rn];

image

Can we also add the other fields @harishgg1? Like in my sample query.

I setup a structure for you to do this in SQL - you need to at least make an attempt at learning and writing the code yourself.

As for doing this in SSRS - do you have SSRS and can you publish a report to the SSRS report server? If you don't have that then it does no good to try to show you how...

How are you going to display this data to the user? A much simpler method would be to copy/paste the data into Excel and use Excel to add totals - but if you use the above you can get the expected results.

1 Like

hi cenk

when people are trying to see what you are doing !!
it has to be easy to undestand

how to put it in a way that others can understand easily
is a PHD subject by itself

It all invloves trying to make sense
what is cat
what does cat eats dog mean
How can this be translated mapped to SQL Language

Hope this helps!!!

making it neat and clean and easy to digest
for people to understand
will make a lot of people help you

1 Like

@Cenk As mentioned in other posts, your requirements change with every post, that is hard to work with.

1 Like

@yosiasz I don't agree. My requirements did NOT change. Anyways thank you for your help.

My query in post #1:

SELECT cf.referenceid, ISNULL(cc.shopno,gr.shopNo) AS shopno, ISNULL(cc.safeno,gr.safeNo) AS safeno, 
ISNULL(cc.cashierno, gr.cashierNo) AS cashierno, cf.purchaseStatusDate, cc.confirmcanceldatetime, cf.productcode, cf.productdescription, cf.quantity, cf.unitprice, cf.totalprice, 
ISNULL(cc.status, 0) As status FROM[gameconfirmresponses] cf LEFT JOIN(SELECT *, Row_number() 
OVER(partition BY referenceid ORDER BY confirmcanceldatetime) AS row_num 
FROM[confirmcancels]) AS cc ON cf.referenceid = cc.referenceid AND cc.row_num = 1 
JOIN[GameRequests] AS gr ON gr.referenceId = cf.referenceId WHERE cf.purchasestatusdate >= '20200401' AND cf.purchasestatusdate < '20200402'

Result I want:

17F62645-6BA0-4270-B17C-00041CCC92C0	96171063	1	190129	2020-04-01 15:00:14.000	1.04.2020 15:01	1573	Bim 840 Riot Points	1	32	32	1
539F7E12-8E9C-4F45-8AA0-027AF46E2C38	97173711	2	9019174	2020-04-01 17:52:34.000	2020-04-01 17:53:18.040	1582	Bim 10 TL Razer Gold Pin	1	10	10	1
Subtotal:									42	42	
031A482E-6C02-419D-B8A1-0F145DA2F079	98184487	3	9027765	2020-04-01 14:23:00.000	1.04.2020 14:24	1573	Bim 840 Riot Points	1	32	32	2
ED4AE4C3-5A80-49E3-AD81-1BC20C86C302	96184860	1	167834	1.04.2020 19:42	1.04.2020 19:42	1585	2100 ZA	1	5	5	1
Subtotal:									37	37	
Grand total:									79	79

hi cenk

Please make
result you want
in nice easy format to understand

and explain from where you are getting the sub total figures

how can you expect people to undertand from looking at it

-- Looks like you dont have enough computer skills to do it
-- NOR DO YOU UNDERSTAND

where is
17 32
17 10
from your result set

people here are very very much experienced and SENIORs
they can catch you very easily !!

Your SQL ..
; with cte as 
(
SELECT
    [cf].[referenceId]
    , ISNULL([cc].[shopNo], [gr].[shopNo]) AS [shopNo]
	,substring(ISNULL([cc].[shopNo], [gr].[shopNo]),3,2) as [3rd to 4th shopno]
    , ISNULL([cc].[safeNo], [gr].[safeNo]) AS [safeNo]
    , ISNULL([cc].[cashierNo], [gr].[cashierNo]) AS [cashierNo]
    , [cf].[purchaseStatusDate]
    , [cc].[confirmCancelDatetime]
    , [cf].[productCode]
    , [cf].[productDescription]
    , [cf].[quantity]
    , [cf].[unitPrice]
    , [cf].[totalPrice]
    , ISNULL([cc].[status], 0) AS [status]
FROM
    [#GameConfirmResponses] AS [cf]
    LEFT JOIN
    (   SELECT
            [#ConfirmCancels].[referenceId]
            , [#ConfirmCancels].[confirmCancelDatetime]
            , [#ConfirmCancels].[status]
            , [#ConfirmCancels].[shopNo]
            , [#ConfirmCancels].[safeNo]
            , [#ConfirmCancels].[cashierNo]
            , ROW_NUMBER() OVER (PARTITION BY
                                     [#ConfirmCancels].[referenceId]
                                 ORDER BY
                                     [#ConfirmCancels].[confirmCancelDatetime]) AS [row_num]
        FROM
            [#ConfirmCancels]) AS [cc]
        ON [cf].[referenceId] = [cc].[referenceId]
           AND  [cc].[row_num] = 1
    JOIN [#GameRequests] AS [gr]
        ON [gr].[referenceId] = [cf].[referenceId]
WHERE
    [cf].[purchaseStatusDate] >= '20200401'
    AND [cf].[purchaseStatusDate] < '20200402'
) 
select [3rd to 4th shopno],unitPrice,	totalPrice
from cte

image