How to do grouping on multiple rows in ssrs - ssrs-2008

I have Column 1, Column 2, Column 3, Column 4, Column 5, Column 6, in my Table. I have Grouped on Column 1 and Column 2, but when I Group on Column 6, it does the grouping but sits between Column 1 and Column 2. I want to Group on Column 6 and it should stay in the same place. (i.e Column 6). How can I achieve this?

Insert a table on your report.
In the first column, select your Column 6 (C6) field.
In the Grouping window, set your RowGroup (probably defaulted the name to Details) Group Properties and Group On your C6 field.
On the table, right-click on the cell with your C6 group and Add Group->Row Group->Parent Group. Repeat step 2 for you C2 field.
Repeat step 3 for your C1 field.
Right click on the column with your C2 field and Insert Column->Inside Group-Right.
Repeat step 5 two more times for your C3, C4, and C5 fields.
For more on Grouping in SSRS:
https://learn.microsoft.com/en-us/sql/reporting-services/lesson-6-adding-grouping-and-totals-reporting-services

Related

How to return both an aggregate function result and a different column value from a query?

Is it possible to build a select query that sums up value of a column and also get the fields from the column of the same table, and an example please . I am new to DB2 and still learning.
I tried using the below,
SELECT SUM(Column containing numbers)
,Column 2
FROM Table
but this gives me a SQL return code of -122
It's possible with the SUM olap function.
SELECT
SUM(C1) OVER () AS C1_SUM
, C2
FROM
(
SELECT 1, 'A' FROM SYSIBM.SYSDUMMY1
UNION ALL SELECT 2, 'B' FROM SYSIBM.SYSDUMMY1
) T (C1, C2)
C1_SUM
C2
3
B
3
A
fiddle

show filter values even if the data is not available in table

So there is the Table and their contents As shown below.
id
title
image
1
First
row
2
Second
row
3
third
row
So if we see in the table there are only 3 records and the id are 1,2,3
There is no entry for 4,5 in the table so no data.
Query:--
select "id", "title", "image"
FROM "table"
WHERE "id" IN (1, 2, 3, 4, 5)
So there are only 3 records in the table but in the where clause there are 5 id. So expecting output to be as shown below:--
id
title
image
1
First
row
2
Second
row
3
Third
row
4
5
So even if there is no data in table the id need to be shown empty if it is present in the IN operator select query.
You can't do this with the IN operator, but you could OUTER JOIN to a VALUES clause or unnest an array:
SELECT id, title, image
FROM table
RIGHT OUTER JOIN (SELECT unnest(ARRAY[1, 2, 3, 4, 5])) as u(id)
USING (id);
Here's a fiddle.

Case When Statement - Three columns

Advanced case when statement in T-SQL using three columns
Hope someone can help with the following :)
I have two tables in SQL.
Table 1 has 4 columns - person_id, A, B, and C.
Table 2 has 3 look-up columns - A, B, and C
I want to carry out the following:
Look down Table 1, column A, find value in Table 2, column A
If no value, go to Table 1, column B.
Look down Table 1, column B, find value in Table 2, column B
If no value, go to Table 1, column C
Look down Table 1, column C, find value in Table 2, column C
If no values in any column put 'Null'
So I know how to write a simple case when statement. However, I think that 'case when' only works on one column. I've started to write out the code but need help to get it right.
Select person_id,
case when 1.A is not null then 2.A
when 1.B is not null then 2.B
else 1.C
end as CODE
from table 1
left join table 2
Order by person_id
Would appreciate any help you can give, thank you.
Your sql seems mostly correct, except for the else 1.C.
Since a CASE WHEN will return 1 value, depending on the first positive WHEN criteria.
And a CASE returns NULL as default.
So ELSE null isn't really needed.
The table descriptions do indicate that Table2 only contains 1 row.
If so, then you can cross join them.
SELECT t1.person_id,
CASE
WHEN t1.A IS NOT NULL THEN t2.A
WHEN t1.B IS NOT NULL THEN t2.B
WHEN t1.C IS NOT NULL THEN t2.C
END AS [CODE]
FROM [Table 1] t1
CROSS JOIN (
SELECT TOP 1 A, B, C
FROM [Table 2]
ORDER BY A DESC, B DESC, C DESC
) t2

How to add a sum of a column where the id is matching to another column in SQL

My table has columns called id, totalsales, and p_sales. p_sales is an empty column that I added and I want to fill in this column with the sum of totalsales where the id is matching.
For example, if the first row has an id of 2, I want the p_sales to be filled with the sum of all totalsales with the id of 2. In the next row, if the id is 6 I want the p_sales to be filled with the sum of all totalsales with an id of 6 and so on so forth.
How do I achieve this using SQL?
Try this, only change table1 and table2 with the name of the tables you need
update dodge t set p_sales = (select sum(t2.totalsales)
from dodge t2 where t2.p_id = t.p_id) ;

adding to table a column with serial numbers that based on information in two columns

I have a 100 rows table ("GendersHeight") with two columns: the 1st column ("gender") is a string ("male" or "female"). the 2nd column ("height") is a variable number. I sorted the table based on these two columns accordingly:
select *
into MyNewTable
from GendersHeight
order by gender, height DESC
(there are 40 "male" rows, and 60 "female" rows in my table)
Now I'd like to add a new column (3rd column) to table "MyNewTable" that holds a serial number.
If I'll use the script:
ALTER TABLE MyNewTable
Add column MySorter serial NOT NULL;
I'll just get a serial column that ranges from 1 to 100.
BUT - what I want really to achieve here is creating a column that would range from 1 to 60 first, for all the "female" entries, and then would range from 1 to 40 for all the "male" entries.
Is there a way to create such a serial column that is taking in account the nature of the "gender" column?
You can use the rank() function:
CREATE TABLE "MyNewTable" AS
SELECT gender,
height,
rank() OVER(partition by gender order by height) as myrank
FROM "GendersHeight"