SSRS comparison of two datasets for "Difference Table" - reporting-services

We are rolling out new Symantec clients that connect to a new server. I've built a report that shows the rollout status.
So far so good. Now I try to report the servers, that are not visible in the new environment.
Source 1: SEPM-Database on SQL Server A
Source 2: SCCM-Database on SQL Server B
Datasets that querying the things that I need are already there.
Server name values of the fields are identical.
My goal is a table that compares:
"SEPM-Database servernames" with "SCCM-Database servernames"
and writes only the server names into the table that ARE NOT in both.
Need this to check out which server hasn't joined the new environment.
Sorry, hope you understand what I mean. SSRS is very new to me. In the attached screenshot, you can see my first report that I have now:
First report created
Visual overview database "relations"

You could do it as a full join like this.
WITH
sepm_server_list
AS
(
SELECT tbl.* FROM (VALUES
( 'SEPM', 'Server1')
, ( 'SEPM', 'Server2')
, ( 'SEPM', 'Server3')
, ( 'SEPM', 'Server4')
) tbl ([DatabaseName], [ServerName])
)
,
sccm_server_list
AS
(
SELECT tbl.* FROM (VALUES
( 'SCCM', 'Server1')
, ( 'SCCM', 'Server2')
, ( 'SCCM', 'Server3')
, ( 'SCCM', 'Server5')
) tbl ([DatabaseName], [ServerName])
)
SELECT
sepm.*
, sccm.*
FROM
sepm_server_list AS sepm
FULL JOIN sccm_server_list AS sccm ON sepm.[ServerName] = sccm.[ServerName]
WHERE
1=1
AND
(
sepm.[ServerName] IS NULL
OR
sccm.[ServerName] IS NULL
)
Or you could write it as an except statement with a union, to get the values in the same columns.
WITH
sepm_server_list
AS
(
SELECT tbl.* FROM (VALUES
( 'SEPM', 'Server1')
, ( 'SEPM', 'Server2')
, ( 'SEPM', 'Server3')
, ( 'SEPM', 'Server4')
) tbl ([DatabaseName], [ServerName])
)
,
sccm_server_list
AS
(
SELECT tbl.* FROM (VALUES
( 'SCCM', 'Server1')
, ( 'SCCM', 'Server2')
, ( 'SCCM', 'Server3')
, ( 'SCCM', 'Server5')
) tbl ([DatabaseName], [ServerName])
)
SELECT DatabaseName = 'SEPM', A.[ServerName] FROM
(
SELECT [ServerName] FROM sepm_server_list
EXCEPT
SELECT [ServerName] FROM sccm_server_list
) A
UNION
SELECT DatabaseName = 'SCCM', B.[ServerName] FROM
(
SELECT [ServerName] FROM sccm_server_list
EXCEPT
SELECT [ServerName] FROM sepm_server_list
) B
Here's an example with the queries you posted.
WITH
sepm_server_list
AS
(
SELECT [DatabaseName] = 'SEPM', [ServerName] = COMPUTER_NAME FROM v_sem_computer
)
,
sccm_server_list
AS
(
SELECT [DatabaseName] = 'SCCM', [ServerName] = F.[Name] FROM (v_Collection AS C LEFT JOIN v_FullCollectionMembership AS F ON F.CollectionID = C.CollectionID) WHERE C.[Name] = 'ALL_SERVERS_neu'
)
SELECT DatabaseName = 'SEPM', A.[ServerName] FROM
(
SELECT [ServerName] FROM sepm_server_list
EXCEPT
SELECT [ServerName] FROM sccm_server_list
) A
UNION
SELECT DatabaseName = 'SCCM', B.[ServerName] FROM
(
SELECT [ServerName] FROM sccm_server_list
EXCEPT
SELECT [ServerName] FROM sepm_server_list
) B

Related

Sql query on taking the value from a case within select statement in a CTE

I am trying to write a SQL. In this, I want columns Bank, and Y-Bank in the output. Y-Bank is being calculated based on certain case statements, using CTE, but I am not able to return Y-Plant as the column.
I also created a table as shown in the input/output here:https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=749e4ca1570880e9c64c4553d18dea1a
Below is the code:
WITH CTE AS
(
SELECT
"BANK",
(select
case
when "TEST"=0 AND "TEST1">0
THEN ( SELECT COUNT("ZONES")FROM mytable I WHERE I.BANK = O.BANK AND I."ZONES"='Y' )
END AS "Y-BANK"
from(
(SELECT
CASE WHEN ( (SELECT COUNT("ZONES")FROM mytable I WHERE I.BINDER = O.BINDER AND I."ZONES"='N' ) = 0 ) AND ( (SELECT COUNT("ZONES")FROM mytable I WHERE I.BINDER = O.BINDER AND I."ZONES"='Y' ) > 0 )
THEN ( SELECT COUNT("ZONES")FROM mytable I WHERE I."TOTAL LINE"= O."TOTAL LINE"AND I."ZONES"='N' )
END AS "TEST",
CASE WHEN ( (SELECT COUNT("ZONES")FROM mytable I WHERE I.BINDER = O.BINDER AND I."ZONES"='' ) = 0 ) AND ( (SELECT COUNT("ZONES")FROM mytable I WHERE I.BINDER = O.BINDER AND I."ZONES"='Y' ) > 0 )
THEN ( SELECT COUNT("ZONES")FROM mytable I WHERE I."TOTAL LINE" = O."TOTAL LINE" AND I."ZONES"='Y' )
END AS "TEST1"
from mytable )
)
)
FROM mytable O
)
SELECT *
FROM CTE O
Can someone help me out on how can I correct it?
If I understand correctly, you want to count the number of "Y" values for each bank. You can use a window function:
select t1.*, sum(zones = 'Y') over (partition by Bank1) as y_bank
from t1
Here is a db<>fiddle.
you can use following query
select
bank1 as "bank",
(select count(*)over(partition by Bank1,Zones order by Bank1 desc) as "y_bank" from t1 where Zones = 'Y' and Bank1 = t.Bank1 limit 1) as "y_bank"
from t1 t

Implementing mariadb's NTILE() function in MYSQL 5.7

I can't use NTILE() because I'm currently on MYSQL 5.7 so I was wondering how would I go about converting this to be usable in 5.7 without NTILE.
Here's the query I'd like to convert:
SELECT
clientid,
ntile(4) over (
order by
last_order_date
) AS `rfm_recency`,
ntile(4) over (
order by
count_order
) AS `rfm_frequency`,
ntile(4) over (
order by
avg_amount
) AS `rfm_monetary`
FROM
(
SELECT
`clientid`,
MAX(`date`) AS `last_order_date`,
COUNT(`id`) AS `count_order`,
AVG(`price`) AS `avg_amount`
FROM
`design`
GROUP BY
`clientid`
) AS t
) AS p```

Inputting data from dataset to SSRS table

I am new to this and i am wondering how to input data into SSRS table and auto generate for the subsequent months. This is the format of the table.
Appreciated for any help given.
You can generate a date range using the following SQL
DECLARE #date_start AS DATETIME
SET #date_start = '01-DEC-2017'
;WITH
finalvalues
AS
(
SELECT tbl.* FROM (VALUES
( '01-Dec-2017', 6414.6563, 429.6846, -1390.8474)
, ( '02-Dec-2017', 6476.6563, 432.751, -1312.4928)
, ( '03-Dec-2017', 6538.6563, 435.8174, -1234.1382)
, ( '04-Dec-2017', 6600.6563, 438.8838, -1155.7836)
, ( '05-Dec-2017', 6662.6563, 441.9502, -1077.429)
, ( '06-Dec-2017', 6724.6563, 445.0166, -999.074399999999)
, ( '07-Dec-2017', 6786.6563, 448.083, -920.719799999999)
, ( '08-Dec-2017', 6848.6563, 451.1494, -842.365199999999)
, ( '09-Dec-2017', 6910.6563, 454.2158, -764.010599999999)
, ( '10-Dec-2017', 6972.6563, 457.2822, -685.655999999999)
, ( '11-Dec-2017', 7034.6563, 460.3486, -607.301399999999)
, ( '12-Dec-2017', 7096.6563, 463.415, -528.946799999999)
, ( '13-Dec-2017', 7158.6563, 466.4814, -450.592199999999)
, ( '14-Dec-2017', 7220.6563, 469.5478, -372.2376)
, ( '15-Dec-2017', 7282.6563, 472.6142, -293.883)
, ( '16-Dec-2017', 7344.6563, 475.6806, -215.5284)
) tbl ([Date], [IncLoad], [ITLoad], [RH])
)
,
manufactured_dates
AS
(
SELECT
day_date = DATEADD(day, dte.[number], #date_start)
FROM
master.dbo.spt_values AS dte
WHERE
1=1 -- <-- used in testing to be able to comment out other clauses below
AND dte.[type] = 'P'
AND dte.[number] <= 365 -- <-- filter how many rows you want to see here
)
SELECT
'Date' = md.[day_date]
, 'IncLoad' = AVG(incload)
, 'ITLoad' = AVG(ITLoad)
FROM
finalvalues AS fv
FULL OUTER JOIN manufactured_dates AS md ON md.[day_date] = fv.[Date]
GROUP BY
md.[day_date]

Mysql - Access column from grandparent's joined table in WHERE in subquery inside parent's FROM

Inside a WHERE inside a subquery inside a FROM inside another subquery inside a SELECT that's joined to another table, I need to access a column from that joined table.
edited to add more complete example:
SELECT
field_one,
field_two,
field_three,
field_one-field_three AS field_five,
field_six
FROM (
SELECT
IFNULL(
(
SELECT
SUM(us.field_seven) AS field_one
FROM
table_one us
WHERE
us.rto_id = rto.relevant_field_one
AND
us.created >= (
SELECT
IF(
selected_date IS NULL,
MIN(created),
selected_date
)
FROM (
SELECT
IF(
latest_date < DATE_SUB(CURDATE(), INTERVAL rtt.relevant_field_two DAY),
CURDATE(),
MAX(prevdate)
) AS selected_date,
created
FROM (
SELECT
created,
#calc_prevdate as prevdate,
DATEDIFF(#calc_prevdate, created) AS diff,
#calc_prevdate := created
FROM (
SELECT
sto.created
FROM
table_one sto
WHERE
sto.rto_id = rto.relevant_field_one
UNION ALL
SELECT
stt.created
FROM
table_two stt
WHERE
stt.rto_id = rto.relevant_field_one
ORDER BY
created DESC
) AS x
CROSS JOIN (
SELECT
#calc_prevdate := NULL
) as vars
) AS z
CROSS JOIN (
SELECT
MAX(created) AS latest_date
FROM(
SELECT
sto.created
FROM
table_one sto
WHERE
sto.rto_id = rto.relevant_field_one
UNION ALL
SELECT
stt.created
FROM
table_two stt
WHERE
stt.rto_id = rto.relevant_field_one
ORDER BY
created DESC
) as z
) AS y
WHERE
diff > rtt.relevant_field_two
) as w
)
GROUP BY us.rto_id
),0
) AS field_one,
IFNULL(
(
SELECT
SUM(tt.field_seven) AS field_three
FROM
table_two tt
WHERE
tt.rto_id = rto.relevant_field_one
AND
tt.created >= (
SELECT
IF(
selected_date IS NULL,
MIN(created),
selected_date
)
FROM (
SELECT
IF(
latest_date < DATE_SUB(CURDATE(), INTERVAL rtt.relevant_field_two DAY),
CURDATE(),
MAX(prevdate)
) AS selected_date,
created
FROM (
SELECT
created,
#calc_prevdate as prevdate,
DATEDIFF(#calc_prevdate, created) AS diff,
#calc_prevdate := created
FROM (
SELECT
sto.created
FROM
table_one sto
WHERE
sto.rto_id = rto.relevant_field_one
UNION ALL
SELECT
stt.created
FROM
table_two stt
WHERE
stt.rto_id = rto.relevant_field_one
ORDER BY
created DESC
) AS x
CROSS JOIN (
SELECT
#calc_prevdate := NULL
) as vars
) AS z
CROSS JOIN (
SELECT
MAX(created) AS latest_date
FROM(
SELECT
sto.created
FROM
table_one sto
WHERE
sto.rto_id = rto.relevant_field_one
UNION ALL
SELECT
stt.created
FROM
table_two stt
WHERE
stt.rto_id = rto.relevant_field_one
ORDER BY
created DESC
) as z
) AS y
WHERE
diff > rtt.relevant_field_two
) as w
)
GROUP BY tt.rto_id
), 0
) AS field_three,
IFNULL(
(
SELECT
COUNT(*) AS field_two
FROM
table_two tt
WHERE
tt.rto_id = rto.relevant_field_one
GROUP BY tt.rto_id
), 0
) AS field_two,
IFNULL(
(
SELECT
GREATEST(
IFNULL(MAX(us.created), 0), IFNULL(MAX(tt.created), 0)
) AS field_six
FROM
table_one us
LEFT JOIN
table_two tt ON us.rto_id = tt.rto_id
WHERE
us.rto_id = rto.relevant_field_one
GROUP BY us.rto_id
), 0
) AS field_six
FROM
relevant_table_one rto
LEFT JOIN
relevant_table_two rtt ON rto.rtt_id = rtt.id
WHERE
rto.rtt_id = ?
GROUP BY rto.relevant_field_one
) v
ORDER BY id ASC;
given that query, I need to access relevant_table_one.relevant_field_one and relevant_table_two.relevant_field_two from inside the subqueries, but the restrictions on subqueries dictates that you cant access a parents table in a subquery inside a FROM
I managed to solve this (so far I think) by adding #rfo := relevant_field_one and #rft := relevant_field_two up in the select where they were accessable and then referring to the created variables instead of the columns down in the nested query where relevant.
It's possible I'm just getting false positives but so far the solution appears to be working.

How to use rownum in mysql

In my case, I try to use this code for pa
boardList = DjangoBoard.objects.raw('SELECT Z.* FROM(SELECT X.*, ceil( rownum / %s ) as page FROM ( SELECT ID,SUBJECT,NAME, CREATED_DATE, MAIL,MEMO,HITS \
FROM web_DJANGOBOARD ORDER BY ID DESC ) X ) Z WHERE page = %s', [rowsPerPage, current_page])
But, It's Oracle's code, Not for mysql. So, rownum has error. How can I do?