JimiJ13
asked on
How can I group record by 25 or any specified number per group?
Dear Experts,
I am trying to generate a multi-columns sub reports, but it is unachievable with my Reporting tool for now, and my option is to use tables with their respective data sources. With this, I need to break my record into 4 columns by a number of records depending on the total number of records.
Is it possible to use pivot for the this scenario? I have only 2 fields reports that ranges from 50-150 records per monthly reporting.
Your best solution will be highly appreciated.
Thanks.
I am trying to generate a multi-columns sub reports, but it is unachievable with my Reporting tool for now, and my option is to use tables with their respective data sources. With this, I need to break my record into 4 columns by a number of records depending on the total number of records.
Is it possible to use pivot for the this scenario? I have only 2 fields reports that ranges from 50-150 records per monthly reporting.
Your best solution will be highly appreciated.
Thanks.
ASKER
Looks great! I will try this and let you know.
Thanks.
Thanks.
ASKER
chaau,
Can I have 2 fields per column?
Thanks.
Can I have 2 fields per column?
Thanks.
What do you mean? do you mean you want total 8 columns with the pairs of columns?
ASKER
Hi chaau,
My main objective is the report output having 2 fields per column. It will look like this:
Colum 1 Column 2 Column 3 Column 4
OR Amt OR Amt OR Amt OR Amt
123 100 136 500 129 400 132 250
124 1000 127 800 130 450 133 450
125 500 128 400 131 5000 134 5000
I hope that makes sense.
Thanks.
My main objective is the report output having 2 fields per column. It will look like this:
Colum 1 Column 2 Column 3 Column 4
OR Amt OR Amt OR Amt OR Amt
123 100 136 500 129 400 132 250
124 1000 127 800 130 450 133 450
125 500 128 400 131 5000 134 5000
I hope that makes sense.
Thanks.
Yes, it is possible. Check this up:
Results:
;with tt as (select *, row_number() over(order by ord) as rn from t ),
cnt4 as (select count(*)/4 as c from t),
t1 as (select ord, amt, row_number() over(order by rn) as rn from tt cross join cnt4 where rn >=1 and rn <= cnt4.c),
t2 as (select ord, amt, row_number() over(order by rn) as rn from tt cross join cnt4 where rn >=cnt4.c*1+1 and rn <= cnt4.c*2),
t3 as (select ord, amt, row_number() over(order by rn) as rn from tt cross join cnt4 where rn >=cnt4.c*2+1 and rn <= cnt4.c*3),
t4 as (select ord, amt, row_number() over(order by rn) as rn from tt cross join cnt4 where rn >=cnt4.c*3+1)
select
t1.ord as ord1, t1.amt as amt1,
t2.ord as ord2, t2.amt as amt2,
t3.ord as ord3, t3.amt as amt3,
t4.ord as ord4, t4.amt as amt4
from
t1 right join t2 on t1.rn = t2.rn
right join t3 on t3.rn = t2.rn
right join t4 on t4.rn = t2.rn
SQL FiddleResults:
| ORD1 | AMT1 | ORD2 | AMT2 | ORD3 | AMT3 | ORD4 | AMT4 |
|--------|--------|--------|--------|--------|--------|------|------|
| 1 | 234 | 11 | 234 | 21 | 234 | 31 | 234 |
| 2 | 234 | 12 | 234 | 22 | 234 | 32 | 234 |
| 3 | 234 | 13 | 234 | 23 | 234 | 33 | 234 |
| 4 | 234 | 14 | 234 | 24 | 234 | 34 | 234 |
| 5 | 234 | 15 | 234 | 25 | 234 | 35 | 234 |
| 6 | 234 | 16 | 234 | 26 | 234 | 36 | 234 |
| 7 | 234 | 17 | 234 | 27 | 234 | 37 | 234 |
| 8 | 234 | 18 | 234 | 28 | 234 | 38 | 234 |
| 9 | 234 | 19 | 234 | 29 | 234 | 39 | 234 |
| 10 | 234 | 20 | 234 | 30 | 234 | 40 | 234 |
| (null) | (null) | (null) | (null) | (null) | (null) | 41 | 234 |
ASKER
chaau,
Seems to be looking great!
Now, this is the final challenge; I want to have parameters for a year and month, how can I do it?
Thanks.
Seems to be looking great!
Now, this is the final challenge; I want to have parameters for a year and month, how can I do it?
Thanks.
You should have provided the table structure you are working with. Would have been much easier. How is the date column called?
ASKER
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
And the Count line as well and I think I am good to go:
cnt4 as (select count(*)/4 as c from t where [year]=@year and [month]=@month ),
Thanks.
cnt4 as (select count(*)/4 as c from t where [year]=@year and [month]=@month ),
Thanks.
ASKER
Thanks chaau. It's a great solution and nice working with you.
....or you can make the cnt4 CTE get the count from the tt CTE. Some reporting tools have troubles reusing the same parameters twice.
ASKER
Please show me how " cnt4 CTE get the count from the tt CTE"?
Thanks.
Thanks.
cnt4 as (select count(*)/4 as c from tt ),
ASKER
Got it - cnt4 as (select count(*)/4 as c from tt) !
Thanks.
Thanks.
Open in new window
Result:Open in new window
Check the SQL Fiddle for example. Please provide your real table structure if you have troubles applying the pseudo code to your real situation