I'm developing a query in oracle which consults if certain record doesn't exist (SELECT) for making the INSERT step, this is my code
IF
(SELECT COUNT(*) FROM (SELECT * FROM HEX_KE_LOC WHERE CLVE_LOC = '20000')) = 0
THEN
{INSERT INTO
HEX_KE_LOC (CLVE_LOC,CLVE_STDO,MUN,CLVE_PAIS,UID_BUS_UNI,CLVE_ALTR,DSC,CIT,ZIPCODE)
VALUES
('20000','AGS','001','MX','0014','1','Zona Centro','01','20000');
}
END IF;
but I can't make it work, any help I'll appreciate
My preferred way to do this is with a MERGE statement with no update clause:
MERGE INTO HEX_KE_LOC USING dual
ON ( CLVE_LOC = '20000' )
WHEN NOT MATCHED THEN INSERT (CLVE_LOC,CLVE_STDO,MUN,CLVE_PAIS,UID_BUS_UNI,CLVE_ALTR,DSC,CIT,ZIPCODE)
VALUES ('20000','AGS','001','MX','0014','1','Zona Centro','01','20000');
Related
In dataverse table relations, I wants to get all the relationship behavioral types for all the table relations.
In the above image, I had shown the one relationship behavioral actions and I wanted to query (SQL query) or any export mechanism to get the entire relationship behavioral actions(specially delete action) for contact table. is it possible to get?
ex.
name
relationship type
type
action
behavior
contact_customer_accounts
Many-to-one
Parential
delete
cascade all
contact_customer_contacts
Many-to-one
System
delete
Remove Link
I have an Excel sheet of 1 million rows that span 12 months from January 2020-January 2021. I have to display all of the attributes for the output.
This is an example of the 2 attributes I'm looking at for the code, but there are 15 attributes in total:
Attribute: Subscriber_Number | Date_Email_Sent
19202 03Dec2020
12829 03Dec2020
13455 05Dec2020
13455 05Dec2020
13455 02 Dec2020
13455 04 Dec2020
An email cannot be sent to a subscriber more than 3 times a week. If that happens, that would become an issue. My goal is to find subscribers who have been called more than 3 times a week. In the example data I gave, the code output should show that Subscriber 13455 was called 4 times in a week. The code output should NOT include 12829 because it just occurs one time.
This is a code that I tried:
PROC SQL;
Select week (Date_Email_Sent, 'u') as weeknum, COUNT(Subscriber_Number) as per_week FROM Data;
Where SubscriberNumber >2
Group By week(Date_Email_Sent, 'U')
quit;
I got a large results were detected line, so I discarded results. There shouldn't be that many times that an email was sent to a subscriber (<10,000 instances), so I'm sure that I wrote the code incorrectly.
With standard sql, you can find those records using:
select Subscriber_Number, Date_Email_Sent
from Data
group by Subscriber_Number, Date_Email_Sent
having count(*) > 3
If you want subscribers who received more than 3 emails in a calendar week, then the logic should look like this:
PROC SQL;
Select week(Date_Email_Sent, 'u') as weeknum, Subscriber_Number,
count(*) as per_week
from Data;
Where SubscriberNumber >2
Group By week(Date_Email_Sent, 'U'), Subscriber_Number
having count(*) > 3;
quit;
How can I write second query instead of 'users_statistic' in first query?
SELECT *
from 'users' LEFT JOIN 'users_statistic'
ON users.id=users_statistic.user_id
SELECT *, SUM(clicks) as sum_clicks, SUM(page_views) as sum_page_views
FROM 'users_statistic'
GROUP BY user_id
Second query return table
user_id sum_clicks sum_page_clicks
1 754 543
2 234 987
In table 'users' is field id, which equivalent user_id in table 'users_statistic'. Also there are no fields sum_clicks and sum_page_clicks in table 'users'. I want to add fields sum_clicks and sum_page_clicks from second query to table 'users'
You can do the aggregation directly in the 1st query:
SELECT u.id, u.name,
COALESCE(SUM(s.clicks), 0) sum_clicks,
COALESCE(SUM(s.page_views), 0) sum_page_views
FROM users u LEFT JOIN users_statistic s
ON u.id = s.user_id
GROUP BY u.id
I use COALESCE() so that you get 0 instead of null if for a user there are no clicks or page views.
I have loaded some values in #Temp table like below,
Select 12 as ReporterID,'<tr><td>Saravanan</td></tr>' As Body into #Temp
Insert into #Temp values(12,'<tr><td>Yogaraj</td></tr>')
Insert into #Temp values(22,'<tr><td>Daniel</td></tr>')
Insert into #Temp values(22,'<tr><td>Mathan</td></tr>')
I want the output like,
Reporter Body
12 '<tr><td>Saravanan</td></tr>tr><td>Yogaraj</td></tr>'
22 '<tr><td>Daniel</td></tr>tr><td>Mathan</td></tr>'
I have tried below code it convert it into xml. but i want it in html.
SELECT DISTINCT tttm.ReporterID
, STUFF((
SELECT ',' + t.Body
FROM #Temp T
WHERE T.ReporterID = tttm.ReporterID
FOR XML PATH('table')
), 1, 1, '') AS tags_title
--into #XML
from #Temp tttm
someone please help me to find out the solution. Thanks in advance.
Note: This probably isn't the best solution, but I believe it accomplishes what you want.
What you can do is have a nested query: The outer query groups by ReporterID, and the inner query gives you the XML you need. This is similar to your attempt. But with my solution, I'm stripping out the td text, and then putting it back into the requisite elements in the FOR XML part of the query:
SELECT
ReporterID,
(
SELECT Body.query('//td/text()') AS td
FROM #tmp AS t2
WHERE t2.ReporterID = t1.ReporterID
FOR XML PATH('tr'), ROOT('table')
) AS combined_body
FROM #tmp AS t1
GROUP BY ReporterID
;
working fiddle here.
I've this table with the following data
Job Quantity Status Repeat
1 100 OK 2
2 400 HOLD 0
3 200 HOLD 1
4 450 OK 3
Based on the value in the Repeat column for each row, the row should be repeated again. For example for the Job 1, Repeat value is 2 so the Job 1 should repeat two more times.
The resultant table should be as below
Job Quantity Status Repeat
1 100 OK 2
1 100 OK 2
1 100 OK 2
2 400 HOLD 0
3 200 HOLD 1
3 200 HOLD 1
4 450 OK 3
4 450 OK 3
4 450 OK 3
4 450 OK 3
Can someone please help me out with this query?
am using oracle 10g
You could use a recursive CTE:
with cte(Job, Repeat, i) as
(
select Job
, Repeat
, 0
from YourTable
union all
select Job
, Repeat
, i + 1
from cte
where cte.i < cte.Repeat
)
select *
from cte
order by
Job
, i
Live example at SQL Fiddle.
Supposing you won't generate more than 1000 rows per row:
with num as (select level as rnk from dual connect by level<=1000)
select Job, Quantity, Status, Repeat, rnk
from t join num on ( num.rnk <= repeat )
order by job, rnk;
Here is a test:
http://sqlfiddle.com/#!4/4519f/12
UPDATE: As Jeffrey Kemp said, you can "detect" the maximum with a subquery:
with num as (select level as rnk
from dual
connect by level<=(select max(repeat) from t)
)
select job, quantity, status, repeat, rnk
from t join num on ( num.rnk <= repeat )
order by job, rnk;
Instead of doing this manipulation with query you can get this data first in a data table and add rows to a new data table based on the value of repeat and then bind this new data table.