Netezza SQL to find Client IP - netezza

Is there any way to return the client IP address in Netezza? In Oracle we run below query .
SELECT SYS_CONTEXT('USERENV','IP_ADDRESS') FROM dual;
Thanks

This query can get you all the information you need about the current_session.
select client_ip
from _v_session_detail
where session_id= CURRENT_SID

You can use "show session" to provide that information if you aren't trying to access it as a column in a table.
SYSTEM.ADMIN(ADMIN)=> SYSTEM.ADMIN(ADMIN)=> show session;
SESSION_ID | PID | USERNAME | DBNAME | SCHEMA | TYPE | CONNECT_TIME | SESSION_STATE_NAME | SQLTEXT | PRIORITY_NAME | CLIENT_PID | CLIENT_IP | CLIENT_OS_USERNAME
------------+-------+----------+--------+--------+------+---------------------+--------------------+--------------+---------------+------------+-----------+--------------------
16228 | 10272 | ADMIN | SYSTEM | ADMIN | sql | 2014-12-10 10:56:48 | active | show session | normal | 10271 | 127.0.0.1 |
(1 row)
You can also query against the _v_session, which will report on sessions you have visibility/authorization to see, but doesn't necessarily tell you which one is yours. For a non-administrative user this is usually only your sessions, so it should be easy to tell.
SYSTEM.ADMIN(ADMIN)=> select * from _v_session;
ID | PID | USERNAME | DBNAME | TYPE | CONNTIME | STATUS | COMMAND | PRIORITY | CID | IPADDR | CLIENT_OS_USERNAME
-------+-------+----------+--------+------+---------------------+--------+--------------------------+----------+-------+-----------+--------------------
16228 | 10272 | ADMIN | SYSTEM | sql | 2014-12-10 10:56:48 | active | select * from _v_session | 3 | 10271 | 127.0.0.1 |
(1 row)
If you want information only about the particular session in which you are calling the query, then this will do the trick.
SYSTEM.ADMIN(ADMIN)=> select * from _v_session where id = current_sid;
ID | PID | USERNAME | DBNAME | TYPE | CONNTIME | STATUS | COMMAND | PRIORITY | CID | IPADDR | CLIENT_OS_USERNAME
-------+-------+----------+--------+------+---------------------+--------+-------------------------------------------------+----------+-------+-----------+--------------------
16837 | 22310 | ADMIN | SYSTEM | sql | 2014-12-10 19:06:21 | active | select * from _v_session where id = current_sid | 3 | 22309 | 127.0.0.1 |
(1 row)
I should note that what you're looking for here is already being tracked by the query history database, which is most likely already configured on your system.

Related

EF Core database connection kept active

I have an .Net Core application using EF Core with postgres. While the application is running, IF I run
docker exec -i postgres-db psql -U admin eventsDb --command "select pid as process_id, usename as username, datname as database_name, client_addr as client_address, application_name, backend_start, state, state_changefrom pg_stat_activity";
There are 7 processes running:
pid | username | database_name | client_address | application_name | backend_start | state | state_change
-----+----------+---------------+----------------+------------------+-------------------------------+--------+-------------------------------
33 | | | | | 2021-04-15 14:10:43.496459+00 | |
35 | admin | | | | 2021-04-15 14:10:43.497432+00 | |
232 | admin | eventsDb | 172.18.0.1 | | 2021-04-15 14:27:21.61369+00 | idle | 2021-04-15 14:27:21.675184+00
246 | admin | eventsDb | | psql | 2021-04-15 14:27:38.210868+00 | active | 2021-04-15 14:27:38.216957+00
31 | | | | | 2021-04-15 14:10:43.495634+00 | |
30 | | | | | 2021-04-15 14:10:43.494741+00 | |
32 | | | | | 2021-04-15 14:10:43.496089+00 | |
(7 rows)
If I stop the application has 6 processes:
$ docker exec -i postgres-db psql -U admin eventsDb --command "select pid, usename as username, datname as database_name, client_addr as client_address, application_name, backend_start, state, state_change from pg_stat_activity";
pid | username | database_name | client_address | application_name | backend_start | state | state_change
-----+----------+---------------+----------------+------------------+-------------------------------+--------+-------------------------------
33 | | | | | 2021-04-15 14:10:43.496459+00 | |
35 | admin | | | | 2021-04-15 14:10:43.497432+00 | |
261 | admin | eventsDb | | psql | 2021-04-15 14:28:10.544338+00 | active | 2021-04-15 14:28:10.546737+00
31 | | | | | 2021-04-15 14:10:43.495634+00 | |
30 | | | | | 2021-04-15 14:10:43.494741+00 | |
32 | | | | | 2021-04-15 14:10:43.496089+00 | |
(6 rows)
The connection string is:
"Host=localhost;Username=admin;Password=<password>;Database=eventsDb;"
The question is:
Is this the normal behavior for EF Core when no database IO operation performed?

SQL Help to turn one data from Two Columns into a Rows

I am trying to create a view for Windows Performance Monitors. The issue I have is that Table I have called "AllPerf" has 2 of the columns called "PerfCounter" and "Application" have several different names of Performance counters and Applications in them.
Ideally, what I want for my columns is: Time, Computer, Application name, and then the names of all the PerfCounter rows into columns.
I created a view of only the row names I want, and with the current view I created below, I get this output:
-----------------------------------------------------------------------
| mem.AlertTime | Computer | PerfCounter | Application | Value |
-----------------------------------------------------------------------
| 2019-03-15 14:49:02 | WEB-04 | Vrt_Bytes | System |0.1368 |
| 2019-03-15 14:49:02 | WEB-05 | Vrt_Bytes | System |2440 |
| 2019-03-15 14:49:02 | WEB-06 | Handles | w3wp |1508 |
| 2019-03-15 14:49:02 | WEB-04 | Page_Faults | System |0.00419 |
| 2019-03-15 14:49:02 | WEB-04 | Prvt_Bytes | System |0.1368 |
-----------------------------------------------------------------------
I've tried the solutions in this link below, but when I can successfully list row names as columns, I get no Values populated under the columns.
Efficiently convert rows to columns in sql server
And since I don't have much experience with SQL in general, I can't seem to extrapolate the simple examples with my more complex data
This is what i'm using for my SELECT statement for my current view.
SELECT mem.AlertTime,
Computer,
CASE WHEN mem.PerfCounter = 'Virtual Bytes' THEN 'Virt_Bytes'
WHEN mem.PerfCounter = 'Private Bytes' THEN 'Prvt_Bytes'
WHEN mem.PerfCounter = 'Page Faults/sec' THEN 'Page_Faults_Sec'
WHEN mem.PerfCounter = 'Thread Count' THEN 'Threads'
WHEN mem.PerfCounter LIKE '%Handle%' THEN 'Handles'
END AS PerfCounter,
PerfInstance AS Application,
Value
FROM dbo.AllPerf AS mem
And what I want is something like this:
--------------------------------------------------------------------------------------------
| mem.AlertTime | Computer |Application| Vrt_Bytes| Prvt_Bytes| Handles| Page_Faults |
-------------------------------------------------------------------------------------------
| 2019-03-15 14:49:02 | WEB-04 | System | 12440 | 24.13 | 13 | 0.14 |
| 2019-03-15 14:49:02 | WEB-04 | w3wp | 7396 | 4.2309 | 13 | 0 |
| 2019-03-15 14:49:02 | WEB-05 | w3wp | 1538 | 0.1368 | 1538 | 0 |
| 2019-03-15 14:49:02 | WEB-05 | System | 6629 | 6500 | 1835 | 5 |
| 2019-03-15 14:49:02 | WEB-06 | System | 2440 | 0.1368 | 13 | 0 |
--------------------------------------------------------------------------------------------
And If I had my pie-in-the-sky wish, I would covert the MBytes under Mem_Bytes to GB, but I couldn't successfully make CASE statements and include the math in the result
I created a variable table #MYTAB in which I used to test the script below
You can replace #MYTAB by the whole view you used in your post
Here is the code, let me know if it is Ok for you
DECLARE #MYTAB AS table(AlertTime datetime,Computer varchar(50),PerfCounter varchar(50),Application varchar(50),Value decimal(10,5))
insert into #mytab
values('2019-03-15 14:49:02','WEB-04' , 'Vrt_Bytes', 'System' ,'0.1368') ,
('2019-03-15 14:49:02' , 'WEB-05' , 'Vrt_Bytes' , 'System' ,2440 ),
('2019-03-15 14:49:02' , 'WEB-06' , 'Handles' , 'w3wp' ,1508 ),
('2019-03-15 14:49:02' , 'WEB-04' , 'Page_Faults' , 'System' ,0.00419 ),
('2019-03-15 14:49:02' , 'WEB-04' , 'Prvt_Bytes' , 'System' ,0.1368 )
select alerttime,computer,application,piv.*
from #MYTAB d
pivot
( max(value) for perfcounter in ([vrt_bytes],[handles],[page_faults]) ) piv

Mariadb Parititioning

There is table named history in Zabbix database, I have created partitions on this table.
And the partition type is range and column type is UNIX_TYPESTAMP.
After the date is changed zabbix service does not insert data to the related partition.
What is the problem?
And how do I display all partitions?
Could you please help how do I write data to the related partitions?
Sample of Partition creation statement;
.
.
.
ALTER TABLE zabbix.history_test PARTITION BY RANGE(clock)(PARTITION
p28082021 VALUES LESS THAN(UNIX_TIMESTAMP("2021-08-28 00:00:00"
))ENGINE=InnoDB);
Server version: 10.1.31-MariaDB MariaDB Server
EXPLAIN PARTITIONS SELECT * FROM zabbix.history;
+------+-------------+---------+------------+------+---------------+------
| id | select_type | table | partitions | type | possible_keys | key |
key_len | ref | rows | Extra |
| 1 | SIMPLE | history | p28082021 | ALL | NULL | NULL
| NULL | NULL | 18956757 | |
SELECT DISTINCT PARTITION_EXPRESSION FROM
INFORMATION_SCHEMA.PARTITIONS WHERE TABLE_NAME='history' AND
TABLE_SCHEMA='zabbix';
+----------------------+
| PARTITION_EXPRESSION |
+----------------------+
| clock |
+----------------------+
MariaDB [(none)]> SELECT PARTITION_ORDINAL_POSITION, TABLE_ROWS, PARTITION_METHOD
FROM information_schema.PARTITIONS
WHERE TABLE_SCHEMA = 'zabbix' AND TABLE_NAME = 'history';
+----------------------------+------------+------------------+
| PARTITION_ORDINAL_POSITION | TABLE_ROWS | PARTITION_METHOD |
+----------------------------+------------+------------------+
| 1 | 18851132 | RANGE |
+----------------------------+------------+------------------+
SELECT FROM_UNIXTIME(MAX(clock)) FROM zabbix.history;
+---------------------------+
| FROM_UNIXTIME(MAX(clock)) |
+---------------------------+
| 2018-04-07 23:06:06 |
+---------------------------+
SELECT FROM_UNIXTIME(MIN(clock)) FROM zabbix.history;
+---------------------------+
| FROM_UNIXTIME(MIN(clock)) |
+---------------------------+
| 2018-04-06 01:06:23 |
+---------------------------+
This document help me to create partition on clock column.
There are stored procedures, that create partitions,you can check it.
https://www.zabbix.org/wiki/Docs/howto/mysql_partition

SQL Pivot, Duplicate Key Values, No Aggregate [duplicate]

Here is a part of my MSSQL 2008 [ERROR CODE] table, which I want to transpose to following structure. I tried searching a workaround but could not find a solution to accomplish the task. Using Pivot I think is not feasible as I cannot use aggregate function. Can someone please help me to how to make this possible?
+----------+-------+---------------------------------------------------+
| SKILL ID | SKILL | PARAMETER |
+----------+-------+---------------------------------------------------+
| 1 | 121 | STANDARD VERBIAGE & PROCEDURES |
| 1 | 121 | ISSUE IDENTIFICATION |
| 1 | 121 | CALL COURTESY |
| 1 | 121 | ISSUE RESOLUTION |
| 2 | BO | COLLECTION PROCESS ADHERENCE |
| 2 | BO | INTELLIGENCE PARAMETER |
| 3 | EM | SOFT SKILLS |
| 3 | EM | PRODUCT KNOWLEDGE |
| 3 | EM | CALL CLOSING |
| 3 | EM | CALL OPENING |
| 4 | FLC | RESOLUTION |
| 4 | FLC | NONE |
| 5 | FTA | OTHERS |
| 5 | FTA | HYGIENE FACTORS |
| 5 | FTA | ACCOUNT SCREEN |
| 5 | FTA | ORDER , DOCUMENTATION AND CONFIGURATION |
| 5 | FTA | VALIDATION SCREEN |
| 5 | FTA | PARTY SCREEN |
| 5 | FTA | ORDER , DOCUMENTATION AND CONFIGURATION |
| 6 | NCE | COMPLIANCE |
| 6 | NCE | CRM |
| 6 | NCE | ACCOUNT LEVEL /INSTALLATION DETAILS CONFIRTMATION |
| 6 | NCE | CONTENTS/BILL DETAILS |
| 6 | NCE | SELFCARE |
| 6 | NCE | FEEDBACK/SATISFACTION |
| 6 | NCE | OBJECTION RESOLUTION |
| 6 | NCE | CUSTOMER HANDLING |
| 6 | NCE | RED ALERT |
| 7 | RTO | ZERO TOLERANCE |
| 7 | RTO | OVERALL IMPRESSION |
| 7 | RTO | SUMMARY AND CLOSING |
| 7 | RTO | PROCESS KNOWLEDGE |
| 7 | RTO | OPENING |
| 8 | SHMNP | SKILL AREA |
| 8 | SHMNP | CONVINCING SKILLS |
+----------+-------+---------------------------------------------------+
This is may expected output
+-------+--------------------------------+------------------------+---------------------------------------------------+
| SKILL | PARAMETER1 | PARAMETER2 | PARAMETER3 |
+-------+--------------------------------+------------------------+---------------------------------------------------+
| 121 | STANDARD VERBIAGE & PROCEDURES | ISSUE IDENTIFICATION | CALL COURTESY |
| BO | COLLECTION PROCESS ADHERENCE | INTELLIGENCE PARAMETER | NULL |
| EM | SOFT SKILLS | PRODUCT KNOWLEDGE | CALL CLOSING |
| FLC | RESOLUTION | NONE | NULL |
| FTA | OTHERS | HYGIENE FACTORS | ACCOUNT SCREEN |
| NCE | COMPLIANCE | CRM | ACCOUNT LEVEL /INSTALLATION DETAILS CONFIRTMATION |
| RTO | ZERO TOLERANCE | OVERALL IMPRESSION | SUMMARY AND CLOSING |
| SHMNP | SKILL AREA | CONVINCING SKILLS | NULL |
+-------+--------------------------------+------------------------+---------------------------------------------------+
You can use the PIVOT function to get the result, you will just have to use row_number() to help.
The base query for this will be:
select skill_id, skill, parameter,
row_number() over(partition by skill, skill_id order by skill_id) rn
from yt;
See SQL Fiddle with Demo. I use row_number() to apply a distinct value to each row within the skill and skill_id, you will then use this row number value as the column to PIVOT.
The full code with the PIVOT applied will be:
select skill_id, skill,[Parameter_1], [Parameter_2], [Parameter_3]
from
(
select skill_id, skill, parameter,
'Parameter_'+cast(row_number() over(partition by skill, skill_id
order by skill_id) as varchar(10)) rn
from yt
) d
pivot
(
max(parameter)
for rn in ([Parameter_1], [Parameter_2], [Parameter_3])
) piv;
See SQL Fiddle with Demo.
In your case, it seems like you will have an unknown number of parameters for each skill. If that is true, then you will want to use dynamic SQL to get the result:
DECLARE #cols AS NVARCHAR(MAX),
#query AS NVARCHAR(MAX)
select #cols = STUFF((SELECT distinct ',' + QUOTENAME('Parameter_'
+cast(row_number() over(partition by skill, skill_id
order by skill_id) as varchar(10)))
from yt
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
set #query = 'SELECT skill_id, skill,' + #cols + ' from
(
select skill_id, skill, parameter,
''Parameter_''+cast(row_number() over(partition by skill, skill_id
order by skill_id) as varchar(10)) rn
from yt
) x
pivot
(
max(parameter)
for rn in (' + #cols + ')
) p '
execute(#query);
See SQL Fiddle with Demo

SQL Server : long to wide data using PIVOT? [duplicate]

Here is a part of my MSSQL 2008 [ERROR CODE] table, which I want to transpose to following structure. I tried searching a workaround but could not find a solution to accomplish the task. Using Pivot I think is not feasible as I cannot use aggregate function. Can someone please help me to how to make this possible?
+----------+-------+---------------------------------------------------+
| SKILL ID | SKILL | PARAMETER |
+----------+-------+---------------------------------------------------+
| 1 | 121 | STANDARD VERBIAGE & PROCEDURES |
| 1 | 121 | ISSUE IDENTIFICATION |
| 1 | 121 | CALL COURTESY |
| 1 | 121 | ISSUE RESOLUTION |
| 2 | BO | COLLECTION PROCESS ADHERENCE |
| 2 | BO | INTELLIGENCE PARAMETER |
| 3 | EM | SOFT SKILLS |
| 3 | EM | PRODUCT KNOWLEDGE |
| 3 | EM | CALL CLOSING |
| 3 | EM | CALL OPENING |
| 4 | FLC | RESOLUTION |
| 4 | FLC | NONE |
| 5 | FTA | OTHERS |
| 5 | FTA | HYGIENE FACTORS |
| 5 | FTA | ACCOUNT SCREEN |
| 5 | FTA | ORDER , DOCUMENTATION AND CONFIGURATION |
| 5 | FTA | VALIDATION SCREEN |
| 5 | FTA | PARTY SCREEN |
| 5 | FTA | ORDER , DOCUMENTATION AND CONFIGURATION |
| 6 | NCE | COMPLIANCE |
| 6 | NCE | CRM |
| 6 | NCE | ACCOUNT LEVEL /INSTALLATION DETAILS CONFIRTMATION |
| 6 | NCE | CONTENTS/BILL DETAILS |
| 6 | NCE | SELFCARE |
| 6 | NCE | FEEDBACK/SATISFACTION |
| 6 | NCE | OBJECTION RESOLUTION |
| 6 | NCE | CUSTOMER HANDLING |
| 6 | NCE | RED ALERT |
| 7 | RTO | ZERO TOLERANCE |
| 7 | RTO | OVERALL IMPRESSION |
| 7 | RTO | SUMMARY AND CLOSING |
| 7 | RTO | PROCESS KNOWLEDGE |
| 7 | RTO | OPENING |
| 8 | SHMNP | SKILL AREA |
| 8 | SHMNP | CONVINCING SKILLS |
+----------+-------+---------------------------------------------------+
This is may expected output
+-------+--------------------------------+------------------------+---------------------------------------------------+
| SKILL | PARAMETER1 | PARAMETER2 | PARAMETER3 |
+-------+--------------------------------+------------------------+---------------------------------------------------+
| 121 | STANDARD VERBIAGE & PROCEDURES | ISSUE IDENTIFICATION | CALL COURTESY |
| BO | COLLECTION PROCESS ADHERENCE | INTELLIGENCE PARAMETER | NULL |
| EM | SOFT SKILLS | PRODUCT KNOWLEDGE | CALL CLOSING |
| FLC | RESOLUTION | NONE | NULL |
| FTA | OTHERS | HYGIENE FACTORS | ACCOUNT SCREEN |
| NCE | COMPLIANCE | CRM | ACCOUNT LEVEL /INSTALLATION DETAILS CONFIRTMATION |
| RTO | ZERO TOLERANCE | OVERALL IMPRESSION | SUMMARY AND CLOSING |
| SHMNP | SKILL AREA | CONVINCING SKILLS | NULL |
+-------+--------------------------------+------------------------+---------------------------------------------------+
You can use the PIVOT function to get the result, you will just have to use row_number() to help.
The base query for this will be:
select skill_id, skill, parameter,
row_number() over(partition by skill, skill_id order by skill_id) rn
from yt;
See SQL Fiddle with Demo. I use row_number() to apply a distinct value to each row within the skill and skill_id, you will then use this row number value as the column to PIVOT.
The full code with the PIVOT applied will be:
select skill_id, skill,[Parameter_1], [Parameter_2], [Parameter_3]
from
(
select skill_id, skill, parameter,
'Parameter_'+cast(row_number() over(partition by skill, skill_id
order by skill_id) as varchar(10)) rn
from yt
) d
pivot
(
max(parameter)
for rn in ([Parameter_1], [Parameter_2], [Parameter_3])
) piv;
See SQL Fiddle with Demo.
In your case, it seems like you will have an unknown number of parameters for each skill. If that is true, then you will want to use dynamic SQL to get the result:
DECLARE #cols AS NVARCHAR(MAX),
#query AS NVARCHAR(MAX)
select #cols = STUFF((SELECT distinct ',' + QUOTENAME('Parameter_'
+cast(row_number() over(partition by skill, skill_id
order by skill_id) as varchar(10)))
from yt
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
set #query = 'SELECT skill_id, skill,' + #cols + ' from
(
select skill_id, skill, parameter,
''Parameter_''+cast(row_number() over(partition by skill, skill_id
order by skill_id) as varchar(10)) rn
from yt
) x
pivot
(
max(parameter)
for rn in (' + #cols + ')
) p '
execute(#query);
See SQL Fiddle with Demo

Resources