How to groupby in subquery SQL - sql

I have the following query:
SELECT "IMPORTACIONCOLUMN3", "role", COUNT("IMPORTACIONCOLUMN1")
FROM MYTABLE
WHERE ID = 9
GROUP BY "IMPORTACIONCOLUMN3", "role"
This gives me the following result:
I would like to achieve the following:
The "unique" values are grouped together (i.e. Instead of having 4 values of "Robot 1" these are grouped togehter in just 1 cell summing the count values.
The second group by or subquery has to be the same count, but with role instead of "IMPORTACIONCOLUM3"
Is it possible (for the second picture) to "link" the values either by index or adding an extra column to reference them (i.e. There's two "Solicitante" with a count value of "52" but it refers to "Robot 1" and other to "Solicitante" with count value of "58" links to "Robot 2"
The second image represent visually what I'm trying to explain.
I have been trying on my own but only have reached the following:
select "IMPORTACIONCOLUMN3", count("IMPORTACIONCOLUMN1")
from
(
select "IMPORTACIONCOLUMN1", count("role"), "IMPORTACIONCOLUMN3"
from MYTABLE
WHERE ID = 9
group by "IMPORTACIONCOLUMN1", "IMPORTACIONCOLUMN3"
) as tmp
group by "IMPORTACIONCOLUMN3"
But it is not yet the result I am looking for.
Thanks in advance for your help and tips!
EDIT:
Explaining my desired output in detail
Each one of "Robot 1, 2, 3" have roles such as "Solicitante", "Gerente", etc. with different values.
i.e. The first row "Humano" value "243" is the sum of "Agente de Compras - 95", "Gerente Financiero - 37", "Gerente Solicitante - 45", "Proovedor - 31", "Solicitante - 60".
I am linking these by the column "GRAFICOCOLUMNARECURSIVOID" with contains the index of whatever "Robot" these "roles" are from.
I would like to achieve a query containing subquerys that allows me to have this output.

Try this for question number 1:
SELECT "IMPORTACIONCOLUMN3", COUNT("IMPORTACIONCOLUMN1")
FROM MYTABLE
WHERE ID = 9
GROUP BY "IMPORTACIONCOLUMN3"
the problem is Role: Robot 1 have 4 roles
and this for question 2:
SELECT "role", COUNT("IMPORTACIONCOLUMN1")
FROM MYTABLE
WHERE ID = 9
GROUP BY "role"
Question 3, I don't understand what you are asking for. Please make an example.

Related

SQL WHERE filter

I suck at explaining this issue, but I hope someone understands.
How do I filter something out of a column if it meets just one criteria out of the whole value?
So in this example I want to filter out any value that includes "First Touch", even if the value has other SpecialNeedsTagNames as well. For example "First Touch, Do Not Contact, Self-Serve Pilot" etc..
SELECT count(*) AS count
FROM
(select *
from OperationalReporting.VW_Delivery
WHERE DeliveryCountry in ('NO',
'FI',
'DK',
'SE',
'IS')) AS expr_qry
WHERE "CountryOfUse" = 'Sweden'
AND "OrderStatus" IN ('Booked',
'Order Placed')
AND "IsDelivered" = 'false'
AND "Model" NOT IN ('Model QW',
'Model PX',
'Model ZY')
AND ((SalesAdvisorHomeStore like 'EU-SE%'
OR CommisionabaleStore like 'EU-SE%')
AND (SpecialNeedsTagName is null
or (SpecialNeedsTagName not like '%First Touch%')))
ORDER BY count DESC
LIMIT 50000;
Currently it only filters out the values that ONLY have "First Touch", as soon as it has a combined value it still outputs in the column.Faulty values
So first things first, you may want to unify all values in you column by giving UPPER or LOWER. The where statement would look like this:
WHERE UPPER(SpecialNeedsTagName) LIKE 'FIRST TOUCH%'
If you want to make sure that there are no nulls add AND IS NOT NULL.
This where will return you all of results that starts with FIRST TOUCH. If you are expecting that this phrase may occure in the middle of string add % at the beginning ('%FIRST TOUCH%')
Try This :
SELECT count(*) AS count
FROM
(select *
from XX.XX_XX
WHERE NOT EXISTS (select * from XX.XX_XX where UPPER(SpecialNeedsTagName)
LIKE '%First Touch%'))
ORDER BY count DESC
LIMIT 50000;

Complex INSERT INTO SELECT statement in SQL

I have two tables in SQL. I need to add rows from one table to another. The table to which I add rows looks like:
timestamp, deviceID, value
2020-10-04, 1, 0
2020-10-04, 2, 0
2020-10-07, 1, 1
2020-10-08, 2, 1
But I have to add a row to this table if a state for a particular deviceID was changed in comparison to the last timestamp.
For example this record "2020-10-09, 2, 1" won't be added because the value wasn't changed for deviceID = 2 and last timestamp = "2020-10-08". In the same time record "2020-10-09, 1, 0" will be added, because the value for deviceID = 1 was changed to 0.
I have a problem with writing a query for this logic. I have written something like this:
insert into output
select *
from values
where value != (
select value
from output
where timestamp = (select max(timestamp) from output) and output.deviceID = values.deviceID)
Of course it doesn't work because of the last part of the query "and output.deviceID = values.deviceID".
Actually the problem is that I don't know how to take the value from "output" table where deviceID is the same as in the row that I try to insert.
I would use order by and something to limit to one row:
insert into output
select *
from values
where value <> (select o2.value
from output o2
where o2.deviceId = v.deviceId
order by o2.timestamp desc
fetch first 1 row only
);
The above is standard SQL. Specific databases may have other ways to express this, such as limit or top (1).

Assign custom labels to sql query

This is a pretty simple question, I think, but I cannot find an answer for it here, or through online searching.
I am running a count query, and am having trouble with value labels and column headings
Query is
SELECT
mail_code AS "code",
COUNT(mail_code) AS "count"
FROM data.base
GROUP BY mail_code
;
I get back:
C Count
- -----
Y 110
X 785
Z 92
Questions:
How do I get the first variable (code) to display its full name, instead of a single letter?
How do I change Y, X, and Z to read "phone," "mail," and "email" ...or anything else for that matter?
The length of the mail_code variable is 1 byte...is that why only the first letter is showing up as my varname?
...I was initially warned that based on my title, it might get downvoted. OK, but I tried to look elsewhere for the answer and could not find it, IE I tried due diligence.
Thank you in advance.
Mostly, they keep a look-up table with foreign - primary key relationship. i.e. write explanation in a table with code_name and explanation columns
with values X , phone ; Y, mail : Z , email respectively,
and Join them with a SQL statement :
select d.mail_code as "code", c.explanation as "communication type", count(1) as "count"
from data_base d inner join codes c on ( d.mail_code = c.mail_code )
group by d.mail_code, c.explanation;
Where DDLs are as following to create tables :
create table codes(mail_code varchar2(1) primary key,explanation varchar2(15));
create table data_base( mail_code varchar2(1) references codes(mail_code));
Demo
Thank you all, I got about 75% of the way there.
Syntax:
COLUMN mail_code FORMAT A10
SELECT
DECODE (mail_code,
'X', 'mail',
'Y', 'phon',
'Z', 'emai') AS "code",
COUNT(*) AS "count"
FROM data.base
GROUP BY mail_code
;
Returned
code count
---- -----
mail 110
phon 785
emai 92
Double quotes are needed b/c Oracle. It'll work to change name to 'code' wo double quotes but comes up as all caps (CODE) wo them, and I can choose w double quotes.
Thank you all for your help with this!

Comma separated list SQL

I have a table with 3 columns: "Dept Number" and "Class Number" and "Description". If the user enter values 21-1 and 24-12 where the left term represents the department number and right term represents the class number, I want to search the table for those items based on its dept number and class number (Here, dept# = 21 and class# = 1) and displays its description. How can I write the SQL query for that?
Note: the user can enter more than 1 value. So, I am thinking about making 2 lists: 1 for dept# and other for class#. But, I am not sure how to search the database using these lists
Here is a code example for SQL Server and Oracle.
SQL Server:
SELECT *
FROM yourTable
WHERE dept = CAST(SUBSTRING(N'21-1',1,CHARINDEX(N'-',N'21-1')-1) as int)
AND class = CAST(SUBSTRING(N'21-1',CHARINDEX(N'-',N'21-1')+1, LEN(N'21-1')) as int)
Oracle
SELECT *
FROM yourTable
WHERE dept = CAST(SUBSTR(N'21-1',1,CHARINDEX(N'-',N'21-1')-1) as int)
AND class = CAST(SUBSTR(N'21-1',CHARINDEX(N'-',N'21-1')+1, LENGTH(N'21-1')) as int)
Please tag your question correctly. I hope this helps you.

PL SQL Pivot Table VS Custom Json solution

I'm at a point within one of my Oracle APEX projects where I need to implement different levels of security for specific individuals for specific applications.
To start, I created a cartesian containing the information from the user table, the app table, and the role table.
It looks like this:
SELECT
A.user_id, B.app_id, C.role_id
FROM user A, app B, role C
ORDER BY A.user_id ASC, B.app_id ASC, C.role_id ASC
This allows me to return EVERY combination of user, app, and role. w/o using a where clause it returns over 303k rows. currently almost 500 users, 6 roles, and over 100 apps.
when I select from this view for a specific user its returning in approximately 10 ms which is acceptable.
Now, I also have a vw that stores each user's app/role assignment. I've joined this table to the cartesian in the following fashion.
SELECT
A.*,
DECODE(B.app_right_id, null, 0, 1) AS user_access
FROM
vw_user_app_role A -- My cartesian view
LEFT JOIN vw_tbl_user_app_role B
ON A.user_id = B.user_id
AND A.app_id = B.app_id
AND A.role_id = B.role_id
This returns a very usable set of data that resembles
user_id app_id role_id user_access
50 5 1 0
50 10 2 1
50 15 3 1
75 5 1 1
75 10 2 0
75 15 3 0
I'm considering what my next step should be, If I should create a pivot of the data where the app_id would be the row, the role_id would be the columns, and the user_access would be the "data". The "data" would ultimately be rendered as a check box on a website with the appropriate row/column headings.
I'm also considering using a pure ajax/json solution where I will build the json string using pl sql and return the entire string to the client to be processed via jquery.
I'm concerned with the difficulty of the first option (i'm very new to pl sql, and I'm unsure of how to generate a pivot table to be used in this version of oracle (v 10) ) and I'm concerned with the expense of creating an entire json string that will contain so much data.
Any suggestions would be greatly appreciated.
EDIT
I've achieved the pivot table that I desired via the following sql:
SELECT
B.application_nm,
A.user_id,
MAX(DECODE(b.role_name, 'role 1', A.USER_ACCESS, NULL)) "role 1",
MAX(DECODE(b.role_name, 'role 2', A.USER_ACCESS, NULL)) "role 2",
MAX(DECODE(b.role_name, 'role 3', A.USER_ACCESS, NULL)) "role 3",
MAX(DECODE(b.role_name, 'role 4', A.USER_ACCESS, NULL)) "role 4",
MAX(DECODE(b.role_name, 'role 5', A.USER_ACCESS, NULL)) "role 5",
MAX(DECODE(b.role_name, 'role 6', A.USER_ACCESS, NULL)) "role 6"
FROM
vw_user_app_access A LEFT JOIN vw_tbl_app B ON A.app_id = B.app_id
LEFT JOIN vw_tbl_roles C ON A.role_id = C.role_id
GROUP BY B.application_name, A.user_id
ORDER BY A.user_id DESC
Only problem is when in the future we have to add 'role 7'. I have to then go back into this query and add the line MAX(DECODE(b.role_name, 'role 7', A.USER_ACCESS, NULL)) "role 7"
Thinking ahead, this may be an inconvenience, but considering APEX's framework, I would have to go into the report any way to update the number of columns manually i believe.
I'm thinking this may be the "best" solution for now, unless anyone has any other suggestions...
It is possible for an Apex report region based on a dynamic SQL query to return a different number of columns as the query changes. I have set up a simple demo on apex.oracle.com. Type a new column name into the Columns tabular form and press "Add Row", and the Matrix report is re-drawn with an extra column of that name.
You have to:
Base the report on a function that returns the SQL to be run as a string
Select the region attribute "Use Generic Column Names (parse query at runtime only)"
Set the report Headings Type to PL/SQL and then use a function to dynamically return the required column headings as a colon-separated list. Note that this can be different from the column name, although my example uses the same text for both.
If my example isn't clear enough I'll add more info later - I'm out of time now.