Need Help in Oracle query - sql

I have a requirement, where from front end (JSP)
If market is selected, sectors mapped to Markets have to populate
Likewise, based on sector value selected, End Equipment has to
populate.
Market --1st Drop down
Sector --2nd Drop down
Webcategory -- 3rd Drop down
So initially for getting this page, there will be a database hit,
so as oracle DB developer, I have to send OUT parameter containing this mapping.
End Equipment mapped to Sector and mapped to Market.
I am using below query which is not giving my result as required.
SELECT C.MARKET_SEGMENT_NAME,
C.MARKET_SEGMENT_ID,
B.SECTOR_NAME,
B.SECTOR_ID,
A.WEB_CATEGORY_NAME,
A.WEB_CATEGORY_ID
FROM MSE_WEB_CATEGORY_MASTER A,
MSE_SECTOR_MASTER B,
MSE_MARKET_SEGMENT_MASTER C
WHERE C.MARKET_SEGMENT_ID = B.FK_MARKET_SEGMENT_ID
AND B.SECTOR_ID = A.FK_SECTOR_ID(+);
Output:
MARKET_SEGMENT_NAME MARKET_SEGMENT_ID Sector Sector_id WEB_cate Web_category_id
GOOGLE 90 Slawn 1 FLIPKART 1
BING 100 Clown 2 SNAPDEAL 2
YAHOO 110 VERICON 4 AMAZON 3
YAHOO 110 VERICON 4 E-KART 4
YAHOO 110 QUALCOMM 3
Expected Output:
MARKET_SEGMENT_NAME-MARKET_SEGMENT_ID-SECTOR_NAME-SECTOR_ID-WEB_CATEGORY_NAME-WEB_CATEGORY_ID
GOOGLE-90-Slawn-1-FLIPKART-1
BING-100-Clown-2-SNAPDEAL-2
YAHOO-110-VERICON-4-AMAZON-3
YAHOO-110-VERICON-4-E-KART-4
YAHOO-110-QUALCOMM-3
so that I can send this combination as an Output parameter from my procedure.
Since there is a restriction for not to have DB queries in JSP/presentation layer, everything is been done on DB layer.

I agree with #Ben. Your query appears to return the required data. Concatenating the values into a dash-separated string is a task for the display layer.
Having said that, here is a solution which does what you ask for. || is the concatenation operator. The NVL2() function handles the outer-joined columns, and suppresses the dash when they're blank.
SELECT C.MARKET_SEGMENT_NAME
||'-'||C.MARKET_SEGMENT_ID
||'-'||B.SECTOR_NAME
||'-'||B.SECTOR_ID
||nvl2(A.WEB_CATEGORY_NAME,'-'||A.WEB_CATEGORY_NAME,null)
||nvl2(A.WEB_CATEGORY_ID,'-'||A.WEB_CATEGORY_ID,null) concat_str
FROM MSE_WEB_CATEGORY_MASTER A,
MSE_SECTOR_MASTER B,
MSE_MARKET_SEGMENT_MASTER C
WHERE C.MARKET_SEGMENT_ID = B.FK_MARKET_SEGMENT_ID
AND B.SECTOR_ID = A.FK_SECTOR_ID(+);

Related

How can I extract comma delimited values from one column and put them each in separate column in Google Data Studio?

Update: 1,2,3 are just examples it can also be 4,24,53
I have the following setup:
I store Data in BigQuery and use BigQuery as data source for my Data Studio project.
I have a column called Alarms and the data inside that column is as follow: it can be empty or 1 or 1,2 or 1,2,3 or 5,43,60 and so on. If it's empty or has 1 value then there is nothing to worry about, but if there are 2 or more values I have to do something.
name
Alarm
Mark
John
1
Eddie
1,2
Peter
1,2,3
What I need is to be able to put every value in a separate column or create a dropdown or something.
For example something like the table below or two drop down menus one to select the name and the other shows the alarms. (I prefer the drop downs).
name
Alarm
Mark
John
1
Eddie
1
2
Peter
1
2
3
Here I select Peter and the alarm drop down shows 3 alarms. or for Eddie it just shows 2 alarms and so on.
I read something about regex but I don't really understand how to put it to the test.
I found this online: (.+?)(?:,|$) but I don't know how to capture the output.
What I need is to be able to put every value in a separate column
Consider below approach
select * from (
select * except(alarm)
from your_table,
unnest(split(alarm)) flag with offset
)
pivot (min(flag) as alarm for offset in (0,1,2,3,4))
If applied to sample data in your question -output is

Column value occurs more than X times

I'm trying to only select rows where the trends.insights_taxonomy column value occurs over X times. I've been avoiding COUNT() as I do not what to do any grouping, I want all the correlating rows to remain unique.
I'm trying to weed out outliers, so for example, if I had a database of 100k peoples favorite colors, I want to ignore colors that occur less than 50 times.
Is this where a subquery would come in?
SELECT insights.industry,insights.city,insights.country,metrics.engagements,metrics.number_of_people_at_company, trends.insights_taxonomy,
FROM production.scores.api_company,
UNNEST(insights) AS insights,
UNNEST(metrics) AS metrics,
UNNEST(trends) AS trends
WHERE insights.industry <> ""
AND insights.city <> ""
AND insights.country <> ""
AND metrics.number_of_people_at_company > 0
AND metrics.engagements > 10
Not sure the best way to format this, but the top row is the column labels and the second row are the values. In this case I only want rows where Cisco Systems occurs more than X times.
industry | city | country | engagements | people_at_company | taxonomy
Legal Counsel and Prosecution | Madison | United States | 11 | 5 | Cisco Systems
If you don't want to group your resulting data, then you need to determine your qualifying rows before you get your resulting data. Write a grouping query to determine the qualifying rows, and then you can either JOIN the data set against your query above to gather everything without groupings, or perform a WHERE x IN (your grouping subquery returning valid things you want to see the complete data for).
I figured it out using a sub query, hopefully this is helpful for someone else.
SELECT insights.industry,insights.city,insights.country,metrics.engagements,metrics.number_of_people_at_company, trends.insights_taxonomy, trends.total_interactions
FROM production.scores.api_company,
UNNEST(insights) AS insights,
UNNEST(metrics) AS metrics,
UNNEST(trends) AS trends
WHERE trends.insights_taxonomy IN
(SELECT trends.insights_taxonomy
FROM production.scores.api_company,
UNNEST(trends) AS trends
GROUP BY insights_taxonomy
HAVING count(*) > 100)
AND insights.city <> ""
AND insights.country <> ""
AND metrics.number_of_people_at_company > 0

<SQL>Extract data from table which is txt format (contain 1 fields only)

In our exsiting system we have a table to store the reported user info (let's say UserInfoRaw), this table contains 1 fields (detail) only , the sample data would be sth like below:
^StartNewUser
^UserName
Simon
^EnableFacebook
Y
^EnableTwitter
N
^EndNewUser
^StartNewUser
^UserName
Vicky
^EnableFacebook
N
^EndNewUser
Currently we need to convert this format to a query-able table, lets say "User-info" which contain below 3 fields , the output should be
UserName facebook twitter
==================================================
Simon Y N
Vicky N N
The Constraint is
1. I do know the tage fields i need to extract (said , ^EnableFacebook is a tag name that i know , can use for selection)
2. We're extract by user level, for each user they MUST have ^StartNewUser/^EndNewUser in the txt , this is pre-assumption.
3. The attribute tag may not exist for some cases (eg , Vicky's ^EnableTwitter tag) , it should treat this field as N while extraction.
4. We can use pure SQL here only as this is an interim solution for our MI Team , they can run SQL only and currently we can't do any program release to automate this process at this moment.
Currently we have come up a solution by RRN but need many interim table
1: Produce OUT1 which include the row number for start/end for each user
SELECT RRN(A) ,detail from UserInfoRaw A where A.detail in ('^StartNewUser' , '^EndNewUser')
OUT1 :
1 ^StartNewUser
8 ^EndNewUser
9 ^StartNewUser
14 ^EndNewUser
2: Produce OUT2 which include the row number for user name
Select RRN(A) ,detail from UserInfoRaw A where RRN(A) IN
(select RRN(B)+1 from UserInfoRaw B where B.detail = '^UserName')
OUT2 :
3 Simon
11 Vicky
3: Produce JOIN12 which include the row mapping for ^StartNewUser / ^UserName
Select MAX(A.row) as startRow , B.row as nameRow from OUT1 A,OUT2 B
where A.detail = '^StartNewUser' AND A.row <B.row
GROUP BY B.row
order by A.row
JOIN12 :
1 3
9 11
4: Join 3 table by the row of ^startNewUser to get the 1 field mapping
Select C.startRow ,A.detail , C.nameRow,B.detail from OUT1 A, OUT2 B, JOIN12 C where A.row=C.startRow and B.row=C.nameRow
Result :
1 ^startNewUser 3 Simon
9 ^startNewUser 11 Vicky
By this approach we can produce a 1 field mapping , and using similar step we can get all the result field table we want.
But we have 10+ fields to extract (mayeb more if business request) , so we're seek help here to see if we have better idea for this case. Thanks!
(ps: if you're a AS400 guy and you know how to produce result by wrkqry that would be the best :) you know what MI team im referring to... really mess..)
By your own admission, you have a text file, not a table.
SQL wasn't designed to deal with files (text or otherwise), it was designed to deal with databases, containing tables and relations.
Therefore, don't use SQL statements for this, process it as a text file. It's going to be faster and simpler. My default assumption is that a traditional RPGLE program doing READ is going to beat any attempt to do this sort of processing in SQL, simply because this is the type of workload it was designed for. Or use any other language that can process files.
(It would be easier to do this in SQL with a stored procedure and firing up a cursor, but it would be unwieldy at best, because SQL lacks many of the features makes doing this in a 'normal' programming language so much easier, like local, private functions)
tl;dr
I have a hammer, what's the best way to use it to cut a board in half?

how to write stored procedure to select a particular column using functions

I have two tables
tblSTATUS
StatusID|PlanID|Description|EmailSubject|EmailFrom|EmailTo||Comment
1 8 Approved aaa
2 7 Rejected bbb
3 7 Rejected ccc
4 42 Rejected ccc
tblSTATUSREASON
PlanID|REASONS
7 failed
7 not eligible
42 not eligible
when i send email to particular person if their plan is (only) rejected it stores in table tblstatusreason the reasons for rejected and PlanID used which is dependent on tblStatus.
i should retrieve evrything in grid view C# code using stored procedures and displaying it for user according to description.
Now my problem is i can retrieve and display all the other columns but i dont know how to display rReasons so i want to select [REASONS] from tblSTATUSREASON where for that particluar description = rejected from tblSTATUS and also i dont want to change my tables/columns.I need sql stored procedure for this particular thing
You need also where
select R.PlanID,REASONS
FROM tblSTATUSREASON R
INNER JOIN tblSTATUS S
ON R.PlanID = S.PlanID
AND description = 'rejected'

How can I merge rows (of a particular column) having the same value in SQL Server Reports RDL?

I'm new to working with SQL Server 2005 Reporting Servives using RDLs in BIDS.
I need to modify an existing report so that I can merge cells in adjacent rows which would have the same value for that particular column.
Ex: consider this is the table returned from the stored procedure used by the reports RDL.
_________________________________________________
Id SubCategory Field1 Field2 Total
_________________________________________________
1 a Bob US 17
1 b John UK 17
2 a Mary AUS 12
3 d Ram IND 19
4 b Alex UK 09
4 c Abby FR 09
5 e Tim IT 03
_________________________________________________
Table Example - Couldn' Format Text :( Image here : http_://i.stack.imgur.com/gWEH5.png_
What I need to do is I want the cells merged into one where two adjacent rows in the same column have the same value.
Like Id 1 is repeated twice, so the cells for these must be merged. (Also 4)
Similarly for the last column Total for cells with Ids 1 and 4 must be merged.
The RDL has "TextBox" for columns, I saw some other questions in this forum but were related to Tablix or Matrix, so I thought it would be better if I mentioned it.
I need this merging to be done in the RDL, and this should also be present when exported to Excel.
Hoping someone will be able to help soon.
Change the SQL query to use a group by on ID.
in reporting services drag Id On Row group Of Tablix Upaer than Detail Group
and drag other Fields(except Total) Besid ID and befor vertical dash line In the table.
and drag Total to first cell afte vertical dash line