SSRS Remove Column from Report - sql

i am including column 13 as a dummy column here:
+----+---+---+---+----+---+---+---+---+---+----+----+----+----+
| | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 |
+----+---+---+---+----+---+---+---+---+---+----+----+----+----+
| 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 |
| 2 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 |
| 3 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 1 |
| 4 | 1 | 0 | 0 | 20 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 |
| 5 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 |
| 6 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 |
| 7 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 |
| 8 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 |
| 9 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 |
| 10 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 |
+----+---+---+---+----+---+---+---+---+---+----+----+----+----+
the reason i am including a dummy column is so that IF columns 1 through 12 are all zero, i would still like to include an entry for that row.
as you can see row 1 would not have been included.
this report is generated by SSRS.
i am wondering if there is a way to HIDE column 13?
is there some kind of conditional formatting i can do?
to clarify here's my query:
select tat.*, tat.tat as tat2 from tat
it is organized in the report this way:
this data set [TAT] contains dummy data specifically for column 13

Specific columns in a column group can be hidden based on values with the following steps.
Right-click header of the column group you want to hide, Column Group -> Group Properties
Click on the Visibility pane and select Show or hide based on an expression radio button. Use an expression to determine when column is hidden.
True hides the column, False displays it. You will need to update the field name in my example to match your month field name.

Don't include column 13 in your select? If you are doing a select *, change it to Select col1, col2, ...., col12

Related

Pandas: Dataframe pivot result doesn't drill down

i have the bellow dataframe
Item_code Type year-month Qty
0 TH-32H400M O Jan-22-Q 0.000000
1 TH-32H400M MPO Jan-22-Q 0.000000
2 TH-32H400M ADJ Jan-22-Q 0.000000
3 TH-32H400M BP_O Jan-22-Q 0.000000
4 TH-32H400M LY_O Jan-22-Q 0.000000
... ... ... ... ...
1795 TH-75JX660M P Jun-23-Q 0.000000
1796 TH-75JX660M S Jun-23-Q 11.538462
1797 TH-75JX660M BP_S Jun-23-Q 0.000000
1798 TH-75JX660M LY_S Jun-23-Q 0.000000
1799 TH-75JX660M I Jun-23-Q 0.769231
When i run the below code i get the desired result but with a few issues,
new_df = new_df.pivot(index=['Item_code','year-month'], columns='Type', values='Qty')
+--------------+------------+----------+------+------+---+-------------+------+-----+-----+-----+-----+
| Item_code | year-month | ADJ | BP_O | BP_S | I | LY_O | LY_S | MPO | O | P | S |
+--------------+------------+----------+------+------+---+-------------+------+-----+-----+-----+-----+
| TH-32GS655M | Apr-22-Q | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| | Apr-23-Q | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 350 | 350 | 350 |
| | Aug-22-Q | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| | Dec-22-Q | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 350 | 0 | 0 |
| | Feb-22-Q | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| | Feb-23-Q | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 350 | 350 | 350 |
| | Jan-22-Q | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| ------------ | | | | | | | | | | | | |
| TH-75HX750 | Jan-23-Q | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 350 | 350 | 350 |
| | Jul-22-Q | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| | Jun-22-Q | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| | Jun-23-Q | 0 | 0 | 0 | 13| 0 | 0 | 0 | 0 | 0 | 1.9 |
+--------------+------------+----------+------+------+---+-------------+------+-----+-----+-----+-----+
Why is "Item code" only not repeated on every row
How to get column name on the same row,
Basically "Type" should not be there and "Item_code" & "year-month" should be first row witht he rest of column names
Thank you for the help.
Maybe this solution will work.
new_df = new_df.pivot(index=['Item_code','year-month'], columns='Type', values='Qty')
new_df = new_df.reset_index().fillna(0)

How to set value based on value existence in SQL Server?

I have the following T-SQL code:
select
id,
(case
when n in(Bla1', 'Bla2') then 1
when n = 'Bla3' then 99
else 0
end) as c
from
hello
Running this code outputs this result:
| id | c |
+--------+----+
| 577140 | 0 |
| 577140 | 1 |
| 577140 | 0 |
| 577140 | 0 |
| 577140 | 99 |
| 577141 | 0 |
| 577141 | 0 |
| 577141 | 0 |
| 577142 | 0 |
| 577142 | 0 |
| 577142 | 1 |
How can I modify the code to get the following output?
| id | c |
+--------+----+
| 577140 | 99 |
| 577141 | 0 |
| 577142 | 1 |
Rule
For each id: If 99 exists, then c becomes 99. If not, either 1 or 0, depending if any 1 exists.
You can use aggregation:
select id,
max(case when n in ('Bla1', 'Bla2') then 1
when n = 'Bla3' then 99
else 0
end) as c
from hello
group by id;

SQL how to force to display row with 0 if no data available?

My table returns results as following (skips row if HourOfDay does not have data for particular ID)
ID HourOfDay Counts
--------------------------
1 5 5
1 13 10
1 23 3
..........................HourOfDay up till 23
2 9 1
and so on.
What I am trying to achieve is to force showing rows displaying 0 for HoursOfDay, which don't have data, like following:
ID HourOfDay Counts
--------------------------
1 0 0
1 1 0
1 2 0
1......................
1 5 5
1 6 0
1......................
1 23 3
2 0 0
2 1 0
etc.
I have researched around about it. It looks like I can achieve this result if I create an extra table and outer join it. So I have created table variable in SP (as a temp workaround)
DECLARE #Hours TABLE
(
[Hour] INT NULL
);
INSERT INTO #Hours VALUES (0),(1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12)
,(13),(14),(15),(16),(17),(18),(19),(20),(21),(22),(23);
However, no matter how I join it, it does not achieve desired result.
How do I proceed? Do I add extra columns to join on? Completely different approach? Any hint in the right direction is appreciated!
Using a derived table for the distinct Ids cross joined to #Hours, left joined to your table:
select
i.Id
, h.Hour
, coalesce(t.Counts,0) as Counts
from (select distinct Id from t) as i
cross join #Hours as h
left join t
on i.Id = t.Id
and h.Hour = t.HourOfDay
rextester demo: http://rextester.com/XFZYX88502
returns:
+----+------+--------+
| Id | Hour | Counts |
+----+------+--------+
| 1 | 0 | 0 |
| 1 | 1 | 0 |
| 1 | 2 | 0 |
| 1 | 3 | 0 |
| 1 | 4 | 0 |
| 1 | 5 | 5 |
| 1 | 6 | 0 |
| 1 | 7 | 0 |
| 1 | 8 | 0 |
| 1 | 9 | 0 |
| 1 | 10 | 0 |
| 1 | 11 | 0 |
| 1 | 12 | 0 |
| 1 | 13 | 10 |
| 1 | 14 | 0 |
| 1 | 15 | 0 |
| 1 | 16 | 0 |
| 1 | 17 | 0 |
| 1 | 18 | 0 |
| 1 | 19 | 0 |
| 1 | 20 | 0 |
| 1 | 21 | 0 |
| 1 | 22 | 0 |
| 1 | 23 | 3 |
| 2 | 0 | 0 |
| 2 | 1 | 0 |
| 2 | 2 | 0 |
| 2 | 3 | 0 |
| 2 | 4 | 0 |
| 2 | 5 | 0 |
| 2 | 6 | 0 |
| 2 | 7 | 0 |
| 2 | 8 | 0 |
| 2 | 9 | 1 |
| 2 | 10 | 0 |
| 2 | 11 | 0 |
| 2 | 12 | 0 |
| 2 | 13 | 0 |
| 2 | 14 | 0 |
| 2 | 15 | 0 |
| 2 | 16 | 0 |
| 2 | 17 | 0 |
| 2 | 18 | 0 |
| 2 | 19 | 0 |
| 2 | 20 | 0 |
| 2 | 21 | 0 |
| 2 | 22 | 0 |
| 2 | 23 | 0 |
+----+------+--------+

What is the equivalent of aggregate functions FIRST and LAST from MySQL in Firebird

Does anyone know what is the equivalent of the aggregate functions FIRST and LAST from MySQL to Firebird. I have this inventory master table that looks like this:
DATE |ITEM_CODE | BEG | + | - | - | - | + | + | + | + | - | - | END
2015-10-27 | 000000000MS016 |12.5 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 12.5
2015-10-27 | 000000000PN044 | 0 |10 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 10
2015-10-27 | 000000000VI064 | 440 | 5 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 445
2015-10-27 | 000000000VI029 | 274 | 0 | 5 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 269
2015-10-28 | 000000000MS016 |12.5 |20 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 32.5
2015-10-28 | 000000000PN044 | 10 |50 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 60
2015-10-28 | 000000000VI064 | 445 | 0 | 0 |10 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 435
2015-10-28 | 000000000VI029 | 269 | 0 | 0 | 0 |20 | 0 | 0 | 0 | 0 | 0 | 0 | 249
2015-10-29 | 000000000MS016 |32.5 | 0 |10 | 0 | 0 | 0 | 0 | 0 |30 | 0 | 5 | 47.5
2015-10-29 | 000000000PN044 | 60 | 5 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 65
2015-10-29 | 000000000VI064 | 435 | 0 | 0 | 0 | 0 |10 | 0 | 0 | 0 | 8 | 0 | 437
2015-10-29 | 000000000VI029 | 249 |35 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 4 | 280
2015-10-30 | 000000000MS016 |47.5 | 0 |15 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 32.5
2015-10-30 | 000000000PN044 | 65 | 5 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 5 | 0 | 65
2015-10-30 | 000000000VI064 | 437 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 437
2015-10-30 | 000000000VI029 | 280 | 0 | 5 | 0 | 5 | 0 | 0 | 6 | 0 | 3 | 0 | 273
and I have this SELECT clause:
SELECT
INV.ITEM_CODE,
FIRST(INV.BEG_QTY) AS BEG_QTY,
SUM(INV.REC_QTY) AS REC_QTY,
SUM(INV.RET_QTY) AS RET_QTY,
SUM(INV.SOLD_QTY) AS SOLD_QTY,
SUM(INV.BO_QTY) AS BO_QTY,
SUM(INV.ADJ_QTY) AS ADJ_QTY,
SUM(INV.COUNT_P) AS COUNT_P,
SUM(INV.COUNT_C) AS COUNT_C,
SUM(INV.TRANS_IN) AS TRANS_IN,
SUM(INV.TRANS_OUT) AS TRANS_OUT,
SUM(INV.DELIVERY) AS DELIVERY,
LAST(INV.END_QTY) AS END_QTY
FROM INV_MASTER INV
WHERE (INV.INV_DATE BETWEEN '2015-10-27' AND '2015-10-31')
GROUP BY INV.ITEM_CODE
ORDER BY INV.ITEM_CODE
and the result SHOULD look like this:
ITEM_CODE | BEG | + | - | - | - | + | + | + | + | - | - | END
000000000MS016 |12.5 |20 |25 | 0 | 0 | 0 | 0 | 0 |30 | 0 | 5 | 32.5
000000000PN044 | 0 |70 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 5 | 0 | 65
000000000VI064 | 440 | 5 | 0 |10 | 0 |10 | 0 | 0 | 0 | 8 | 0 | 437
000000000VI029 | 274 |35 |10 | 0 |25 | 0 | 0 | 6 | 0 | 3 | 4 | 273
but I'm having a problem with the FIRST and LAST aggregate functions, I'm using firebird v2.5. How can i do this?
You should be able to replace the use of LAST with
(SELECT END_QTY FROM INV_MASTER
WHERE ITEM_CODE = INV.ITEM_CODE
AND INV_DATE = MAX(INV.INV_DATE)) AS END_QTY
This selects the END_QTY of the current item, with the highest date for that item.

Postgres crosstab query

I have a table which has 7 different classes with an area value.
pid | class| area |
----+------+------+
2 | 1 | 10 |
2 | 2 | 10 |
2 | 6 | 20 |
4 | 1 | 30 |
4 | 2 | 40 |
4 | 3 | 50 |
4 | 4 | 60 |
4 | 5 | 70 |
9 | 6 | 80 |
11 | 1 | 90 |
11 | 4 | 10 |
11 | 7 | 20 |
However I want to present this data in a format that has each distinct pid as a column heading and then have each row correspond to a class area (i.e. first row is the area of class 1 for each pid).
2 | 4 | 9 | 11 |
---+-----+-----+----+
10 | 30 | 0 | 90 |
10 | 40 | 0 | 0 |
0 | 50 | 0 | 0 |
0 | 60 | 0 | 10 |
0 | 70 | 0 | 0 |
20 | 0 | 60 | 0 |
0 | 0 | 0 | 20 |
Is it possible to create an output like this in PostgreSQL?
Try this:
SELECT
SUM(CASE WHEN pid = 2 THEN area ELSE 0 END) As "2",
SUM(CASE WHEN pid = 4 THEN area ELSE 0 END) As "4",
SUM(CASE WHEN pid = 9 THEN area ELSE 0 END) As "9",
SUM(CASE WHEN pid = 11 THEN area ELSE 0 END) As "11"
FROM t
GROUP BY class
ORDER BY class