This is the given table:
I need to get this data without creating temporary table based on the above table:
We can't use temporary table. I need to display the data with sql query only.
Try this
SELECT company name,'No' as value, clicks as data
from table1
union all
SELECT company,'Yes', (clicks - impression)
from table1
order by name,val
You can use UNION ALL to unpivot your table:
SELECT
company,
'No' AS val,
impression AS data
FROM tbl
UNION ALL
SELECT
company,
'Yes' AS val,
clicks - impression AS data
FROM tbl
ORDER BY company, val
You need UNION ALL
SELECT company,'No' val, impression as data from your_table
union all
SELECT company,'Yes' val, clicks-impression as data from your_table
select company as name, 'No' as val, impression as data
from tbl
union
select company as name, 'Yes' as val, clicks - impression as data
from tbl
SELECT COMPANY AS NAME, 'YES' AS VAL, (CLICKS-IMPRESSIONS) AS DATA
FROM ORIGINAL_TABLE
UNION
SELECT COMPANY AS NAME, 'NO' AS VAL, IMPRESSIONS AS DATA
FROM ORIGINAL_TABLE
If you want them sorted: Try this:
SELECT A.NAME, A.VAL, A.DATA FROM
(SELECT COMPANY AS NAME, 'YES' AS VAL, (CLICKS-IMPRESSIONS) AS DATA
FROM OR_TABLE
UNION
SELECT COMPANY AS NAME, 'NO' AS VAL, IMPRESSIONS AS DATA
FROM OR_TABLE) A
ORDER BY A.NAME ASC;
Related
I have created a table that looks something like this:
I want to Pivot this table such that it looks like;
I have tried some other posts about PIVOTs but I could not understand how to do it with more than 2 columns, nor when the values inside the table are sums from different variables.
You want to transpose the recordset (unpivot, then pivot on a different exis). A portable solution is to use union, and then conditional aggregation:
select
name,
max(case when category = 'A' then val end) as valA,
max(case when category = 'B' then val end) as valB
from (
select category, premium val, 'premium' name from mytable
union all select category, net_premium, 'net_premium' from mytable
union all select category, claims, 'claims' from mytable
union all select category, fees, 'fees' from mytable
union all select category, expenses, 'expenses' from mytable
union all select category, commissions, 'commissions' from mytable
) x
group by name
Important: for union to work, the datatypes of all columns being unioned must be the same (it seems like it is the case with your data, that as decimal values everywhere). If not, you need to do conversions in order to align the datatypes.
I want to return the number of rows with columns from two Tables related by UNION.
I wrote this query
SELECT(
(SELECT * FROM(
(SELECT
ID_COMPTE,
TITLE,
LINK,
DATE_CREAT,
DATE_MODIF,
'TF1' AS "TYPE_FICHIER",
case when DATE_MODIF is null then DATE_CREAT else DATE_MODIF end as LAST_UPDATE FROM FIRST_TABLE FFF where ID_COMPTE= 11111111)
UNION
(SELECT
ID_COMPTE,
TITLE,
LINK,
DATE_CREAT,
DATE_MODIF,
'TF2' AS "TYPE_FICHIER",
case when DATE_MODIF is null then DATE_CREAT else DATE_MODIF end as LAST_UPDATE FROM SECOND_TABLE SSS where ID_COMPTE= 11111111)
order by LAST_UPDATE desc
) parentSelect WHERE ROWNUM BETWEEN 0 AND 2)), count(firstSelect) FROM firstSelect;
The purpose is to return the the last two rows with the count of all the rows of Table 1 and Table 2.
The query without the count works fine it's just the count that cause problem, I don't know how to insert it.
I tried also to use count() for each SELECT and SUM in the parent SELECT but it doesn't work.
This concept should work for you. Basically you select the data you want in the with clause. Then in the main select, you select your data, and the count.
WITH
base AS
(
SELECT 'TEST1' DATA FROM DUAL
UNION ALL
SELECT 'TEST2' DATA FROM DUAL
UNION ALL
SELECT 'TEST3' DATA FROM DUAL
)
SELECT (SELECT COUNT(*) FROM base) AS KOUNT, base.*
FROM base
;
You can use #Temp ( TempTable ).
Insert or manupulate the rows in it which you want to have and finally return it from stored procedure.
I want to select a third column based on two distant columns within the same table.
I could only think of this:
select tl.thirdcolumn
from table1 t1
WHERE
EXISTS
(
Select distinct tl.firstcolumn , t1.secondcolumn
From t1
)
This:
select distinct tl.thirdcolumn
from table t1
won't work as I don't want the distinct thirdrow. I want the thirdrow to be based on the first two rows being distinct.
I guess its a kind of nested sql statment with a select top 1... idk
CATEGORY NAME Query
---------------------------------------------------
STUDENTS NUMBER_OF_CHAPTERS QueryA
STUDENTS NUMBER_OF_STUDENT_MEMBERS QueryB
STUDENTS NUMBER_OF_STUDENT_MEMBERS QueryB
MEMBERS NUMBER_OF_MEMBERS_WORLDWIDE QueryC
MEMBERS NUMBER_OF_MEMBERS_WORLDWIDE QueryC
Your question is rather hard to follow, but I think you might simply want group by:
select tl.firstcolumn , t1.secondcolumn, max(tl.thirdcolumn)
from table1 t1
group by tl.firstcolumn , t1.secondcolumn;
If you want rows where the pair of values only appears once, then add having count(*) = 1:
select tl.firstcolumn , t1.secondcolumn, max(tl.thirdcolumn)
from table1 t1
group by tl.firstcolumn , t1.secondcolumn
having count(*) = 1;
Query -
SELECT
CATEGORY,NAME,QUERY
FROM
(
WITH TAB AS (
SELECT
'STUDENTS' AS CATEGORY,
'NUMBER_OF_CHAPTERS' AS NAME,
'QUERYA' AS QUERY
FROM
DUAL
UNION ALL
SELECT
'STUDENTS' AS CATEGORY,
'NUMBER_OF_STUDENT_MEMBERS' AS NAME,
'QUERYB' AS QUERY
FROM
DUAL
UNION ALL
SELECT
'STUDENTS' AS CATEGORY,
'NUMBER_OF_STUDENT_MEMBERS' AS NAME,
'QUERYB' AS QUERY
FROM
DUAL
UNION ALL
SELECT
'MEMBERS' AS CATEGORY,
'NUMBER_OF_MEMBERS_WORLDWIDE' AS NAME,
'QUERYC' AS QUERY
FROM
DUAL
UNION ALL
SELECT
'MEMBERS' AS CATEGORY,
'NUMBER_OF_MEMBERS_WORLDWIDE' AS NAME,
'QUERYC' AS QUERY
FROM
DUAL
) SELECT
CATEGORY,
NAME,
QUERY,
COUNT(*) OVER(PARTITION BY
CATEGORY,
NAME
ORDER BY
CATEGORY,
NAME,
QUERY
) AS RNK
FROM
TAB
)
WHERE
RNK = 1;
Output -
"CATEGORY","NAME","QUERY"
"STUDENTS","NUMBER_OF_CHAPTERS","QueryA"
I would like to select only one email address per id, if the id has both a work and personal email, I would only like to display the work email.
with emails as(
select '1' id, 'work' email_type, 'abc#gmail.com' email from dual union all
select '2' id, 'work' email_type, '123#yahoo.com' email from dual union all
select '2' id, 'personal' email_type, '456#msn.com' email from dual union all
select '3' id, 'personal' email_type, 'test#work.com' email from dual
)
For this example I would like to display:
id email_type email
1 work abc#gmail.com
2 work 123#yahoo.com
3 personal test#work.com
You can prioritize those values in row_number and get the first row for each id.
select id,email_type,email
from (select id,email_type,email
,row_number() over(partition by id order by case when email_type='work' then 1 else 2 end) as rn
from emails) t
where rn = 1
Assuming only possible value for email_type are work and personal, you can use window function row_number:
select *
from (
select t.*,
row_number() over (
partition by id order by email_type desc
) as seqnum
from emails t
) t
where seqnum = 1;
You can use a subquery to figure out if there is a work email or not. With your sample data, you can use the MAX function to return the "work" type if it exists, and if it doesn't it will just return "personal". Joining back on that will give the appropriate result.
WITH T1 AS (SELECT id, MAX(email_type) AS e_type FROM table_name GROUP BY id)
SELECT T1.id, T1.e_type, T2.email
FROM T1 LEFT JOIN table_name T2 ON T1.id = T2.id AND T1.e_type = T2.email_type
ORDER BY T1.id
I have 2 series of unions which I wish to join by another union. In the first one, I have 3 Selects and in the second one I have 2 different Selects.
Select id, min(value)
from table1 t1
join (Select id, value
Union
Select id, value
Union
Select id, value) as foo
on foo.id=t1.id
Group by id
Select id, max(value)
from table1 t1
join (Select id, value
Union
Select id, value) as bar
on bar.id=t1.id
Group by id
I tried to do a union between these two, but it made things pretty complicated. My biggest issue is with my alias. My second is with the case linked to my value columns, which I wish to name value.
Select (alias).id,
Case
When foo.value= 0 or bar.value=1 THEN 1
Else 0
End as value
from table1 t1
Join (Select id, min(value)
from table1 t1
join (Select id, value
Union
Select id, value
Union
Select id, value) as foo
on foo.id=t1.id
Group by id
UNION
Select id, max(value)
from table1 t1
join (Select id, value
Union
Select id, value) as bar
on bar.id=t1.id
Group by id) as (alias)
on ??.id=??.id
I wrote my case the way I think it should be written, but normally, when there are more than one column with the same name, SQL states it as ambiguous. I am still unsure if I should use UNION or INTERSECT, but I assume either of them would be done the same way. How should I deal with this?
I'm reading this right, you probably want something like this
SELECT ...
FROM ( ... union #1 ) AS u1
JOIN (... union #2 ) AS u2 ON u1.id = u2.id