Query help: query to return column that represents multiple rows
I have a table with a name and location column. The same name can occur multiple times with any arbitrary location, i.e. duplicates are allowed.
I need a query to find all names that occur in both of two separate locations.
For example,
bob usa
bob mexico
dot mexico
dot europe
hal usa
hal europe
sal usa
sal mexico
The query in question, if given the locations usa and mexico, would return bob and sal.
Thanks for any help or advice,
-=beeky
How about this?
SELECT NAME
FROM <LOCATIONS_TABLE>
WHERE LOCATION IN ('usa','mexico')
GROUP BY NAME
HAVING COUNT(DISTINCT LOCATION) >= 2Results:
SQL> WITH person_locations AS
2 (
3 SELECT 'bob' AS NAME, 'USA' AS LOCATION FROM DUAL UNION ALL
4 SELECT 'bob' AS NAME, 'Mexico' AS LOCATION FROM DUAL UNION ALL
5 SELECT 'dot' AS NAME, 'Mexico' AS LOCATION FROM DUAL UNION ALL
6 SELECT 'dot' AS NAME, 'Europe' AS LOCATION FROM DUAL UNION ALL
7 SELECT 'hal' AS NAME, 'USA' AS LOCATION FROM DUAL UNION ALL
8 SELECT 'hal' AS NAME, 'Europe' AS LOCATION FROM DUAL UNION ALL
9 SELECT 'sal' AS NAME, 'USA' AS LOCATION FROM DUAL UNION ALL
10 SELECT 'sal' AS NAME, 'Mexico' AS LOCATION FROM DUAL
11 )
12 SELECT NAME
13 FROM person_locations
14 WHERE LOCATION IN ('USA','Mexico')
15 GROUP BY NAME
16 HAVING COUNT(DISTINCT LOCATION) >= 2
17 /
NAM
bob
salHTH!
Edited by: Centinul on Oct 15, 2009 2:25 PM
Added sample results.
Similar Messages
-
How To Concatenate Column Values from Multiple Rows into a Single Column?
How do I create a SQL query that will concatenate column values from multiple rows into a single column?
Last First Code
Lesand Danny 1
Lesand Danny 2
Lesand Danny 3
Benedi Eric 7
Benedi Eric 14
Result should look like:
Last First Codes
Lesand Danny 1,2,3
Benedi Eric 7,14
Thanks,
David JohnsonStarting with Oracle 9i
select last, first, substr(max(sys_connect_by_path(code,',')),2) codes
from
(select last, first, code, row_number() over(partition by last, first order by code) rn
from a)
connect by last = prior last and first = prior first and prior rn = rn -1
start with rn = 1
group by last, first
LAST FIRST CODES
Lesand Danny 1,2,3
Benedi Eric 7,14Regards
Dmytro -
Reg : Concatenation of a column value with multiple rows... URGENT
Hello,
Could any of u help me in concatenating a column value with
multiple rows ???
For ex : I've the following data from emp table :
DEPTNO ENAME
10 KING'S
30 BLAKE
10 CLARK
10 TOM JONES
30 ALLEN
30 JAMES
20 SMITH
20 SCOTT
20 MILLER
10 MILLER
20 rajeev
I want the following output :
deptno Concat_value
10 KING'S,CLARK,TOM JONES,MILLER
20 Rajeev,MILLER,SMITH,SCOTT
30 BLAKE,ALLEN,JAMES
Thanks in Advance,
SriniHello Naveen,
Thanks for ur answer. But I need a single SQL query for getting
what I want. I know the solution in PL/SQL.
Please try it in a single SQL....
Thanks again,
Srini -
Interactive report – column heading in multiple rows
I am using interactive report. My question to the expert/guru’s is: - How do I change column heading into multiple row with text wrap.
For example:- My column heading is
Is Employee Trained ? -------> (single row display)
I want to make it display like
Is Employee
Trained ? ------> (Multi row display)
SagarHi,
What you could do is, disable the download csv function from IR (Interactive Report Attributes--> Search Bar-- Uncheck Download) and in the region header create a link and redirect it to another page which will have the csv report output.
e.g. <a href="#" onclick="javascript:redirect('f?p=&APP_ID.:3:&SESSION.::&DEBUG.:3::');"" >Download Report </a>
Here I am redirecting the link to Page 3. On Page 3 create a sql report with the same query and make report template to csv. Thanks,
Manish -
Concatenate a column value across multiple rows - PDW
We are using PDW based on SQL2014. We require an efficient logic on how to concatenate a column value across multiple rows. We have the following table
T1
(CompanyID, StateCD)
Having following rows:
1 NY
1 NJ
1 CT
2 MA
2 NJ
2 VA
3 FL
3 CA
We need a code snippet which will return following result set:
1
CT,NJ,NY
2
MA,NJ,VA
3
CA,FL
We have tried built-in function STUFF with FOR XML PATH clause and it is not supported in PDW. So, we need a fast alternative.Hi Try this:
SELECT * INTO #ABC
FROM
SELECT 1 AS ID,'NY' AS NAME
UNION
SELECT 1 AS ID,'NJ' AS NAME
UNION
SELECT 1 AS ID,'CT' AS NAME
UNION
SELECT 2 AS ID,'MA' AS NAME
UNION
SELECT 2 AS ID,'NJ' AS NAME
UNION
SELECT 2 AS ID,'VA' AS NAME
UNION
SELECT 3 AS ID,'FL' AS NAME
UNION
SELECT 3 AS ID,'CA' AS NAME
)A
CREATE TABLE ##CDB (ID INT, NAME NVARCHAR(800))
DECLARE @TMP VARCHAR(MAX),
@V_MIN INT,
@V_MAX INT,
@V_COUNT INT
SELECT @V_MIN=MIN(ID),@V_MAX=MAX(ID) FROM #ABC
SET @V_COUNT=@V_MIN
WHILE @V_COUNT<=@V_MAX
BEGIN
SET @TMP = '' SELECT @TMP = @TMP + CONVERT(VARCHAR,NAME) + ', ' FROM #ABC
WHERE ID=@V_COUNT
INSERT INTO ##CDB (ID, NAME) SELECT @V_COUNT AS ID ,CAST(SUBSTRING(@TMP, 0, LEN(@TMP)) AS VARCHAR(8000)) AS NAME
SET @V_COUNT=@V_COUNT+1
END
SELECT * FROM ##CDB
OR
SELECT * INTO #ABC
FROM
SELECT 1 AS ID,'NY' AS NAME
UNION
SELECT 1 AS ID,'NJ' AS NAME
UNION
SELECT 1 AS ID,'CT' AS NAME
UNION
SELECT 2 AS ID,'MA' AS NAME
UNION
SELECT 2 AS ID,'NJ' AS NAME
UNION
SELECT 2 AS ID,'VA' AS NAME
UNION
SELECT 3 AS ID,'FL' AS NAME
UNION
SELECT 3 AS ID,'CA' AS NAME
UNION
SELECT 5 AS ID,'LG' AS NAME
UNION
SELECT 5 AS ID,'AP' AS NAME
)A
CREATE TABLE ##CDB (ID INT, NAME NVARCHAR(800))
DECLARE @TMP VARCHAR(MAX),
@V_MIN INT,
@V_MAX INT,
@V_COUNT INT
SELECT @V_MIN=MIN(ID),@V_MAX=MAX(ID) FROM #ABC
SET @V_COUNT=@V_MIN
WHILE @V_COUNT<=@V_MAX
BEGIN
SET @TMP = '' SELECT @TMP = @TMP + CONVERT(VARCHAR,NAME) + ', ' FROM #ABC
WHERE ID=@V_COUNT
SELECT @V_COUNT AS ID ,CAST(SUBSTRING(@TMP, 0, LEN(@TMP)) AS VARCHAR(8000)) AS NAME INTO #TEMP
INSERT INTO ##CDB (ID, NAME) SELECT ID, NAME FROM #TEMP WHERE NAME<>''
DROP TABLE #TEMP
SET @V_COUNT=@V_COUNT+1
END
SELECT * FROM ##CDB
Thanks Shiven:) If Answer is Helpful, Please Vote -
Why no tab add-ins that enable multiple rows for Firefox 4???
I geneally have lots of tabs open at any given time. I traditionally use TabKit, TabMix, and others that let me use multiple rows of tabs (among other options). I have not been able to find ANY add-ons that support multiple rows. There is sadness across the land.
The mentioned "Multirow Bookmarks Toolbar Plus" add-on specifically addresses bookmarks, while the question is regarding the tabs.
-
Filtering a Power Query Table Based on a Column That's a Table
I have a table produced by an outer join ...
"Bananas" is not one of [My Fruit] values. I'd like to filter my join results to only include [My Fruit] values, and I thought I could do that without expanding "NewCol".
Am I mistaken in my belief that such a selection can be done without expanding the "NewCol"?hi Mark Weisman,
I suppose your [My Fruit] value is single values. You can try below :
Table.SelectRows( Join1, each List.Contains( [NewCol] [Fruit] , [My Fruit] ) )
Note: [Fruit] is the column that contain value want to filter.
Below is my testing sample :
let
Source = Table.FromRows({ {"North", "Apples"}, {"North", "Orange"}, {"West", "Bananas"},{"West", "Lemon"}}, {"Region", "Fruit"}),
Group = Table.Group(Source, {"Region"}, {{"NewCol", each _, type table}}),
Select = Table.SelectRows( Group, each List.Contains( [NewCol] [Fruit] , "Lemon" ) )
in
Select
Regards, -
Splitting comma seperated column data into multiple rows
Hi Gurus,
Please help me for solving below scenario. I have multiple value in single column with comma seperated values and my requirement is load that data into multiple rows.
Below is the example:
Source Data:
Product Size Stock
ABC X,XL,XXL,M,L,S 1,2,3,4,5,6
Target Data:
Product Size Stock
ABC X 1
ABC XL 2
ABC XXL 3
ABC M 4
ABC L 5
ABC S 6
Which transformation we need to use for getting this output?
Thanks in advance !Hello,
Do you need to do this tranformation through OWB mapping only? And can you please tell what type of source you are using? Is it a flat file or a table?
Thanks -
Drill Down between 2 stacked column chart with multiple rows.
Hi,
Can anyone please help me.
1) i have to create stacked column chart with Region and cout of calls for previous 3 quarters.
2) the 2nd stacked column should display the priority with the count of calls for the respective selection of region on the 1st chart..
to achieve this,
i'm using 2 stacked column charts and 1 lebel baseb menu.
from this i can able to get the drill down from label based menu to 2nd stacked column chart but i couldn't able to done the same from 1st stacked coumn chart.
Becoz my data is multiple row for each quarter.
please suggest me to achieve the drill down between 2 stacked column charts.
assume that my data like below.:
coulmns for 1st stacked column chart : Region, Q2,Q3 &Q4
Region Q2 Q3 Q4
Other 3658 3497 497
NA - North
America 3 3 1101
APJK 1 4 597
UK 324
EMEA North 1 1 288
CORPORATE 1 5 215
LA -
Latin America1 2 208
EMEA 1 199
EMEA South 1 1 169
Coulmns for 2nd coumn chart: Region, Priority, Q2,Q3,Q4 ()which is derived from 1st table.
Region Priority Q2 Q3 Q4
Other D: End of Next Day 1568 1613 239
Other C: End of Day 983 893 119
Other A: 2 Hour 631 610 70
Other B: 4 Hour 396 318 56
NA -
North AmericaD: End of Next Day 2 2 514
APJK D: End of Next Day 3 297
NA -
North AmericaC: End of Day 284
NA -
North AmericaA: 2 Hour 1 170
UK D: End of Next Day 157
APJK C: End of Day 1 1 152
Other E: 3 - 5 Business Days 68 58 13
EMEA North D: End of Next Day 1 136
CORPORATE D: End of Next Day 4 107
NA - North America B: 4 Hour 1 104
LA - Latin America D: End of Next Day 1 1 86
EMEA D: End of Next Day 86
UK C: End of Day 85
APJK A: 2 Hour 79
EMEA South D: End of Next Day 72
EMEA North A: 2 Hour 69
EMEA North C: End of Day 56
APJK B: 4 Hour 55
EMEA C: End of Day 1 53
LA - Latin America C: End of Day 54
UK A: 2 Hour 49
EMEA South C: End of Day 1 44
CORPORATE A: 2 Hour 1 43
CORPORATE C: End of Day 44
LA - Latin America A: 2 Hour 1 42
EMEA South A: 2 Hour 1 32
EMEA B: 4 Hour 30
UK B: 4 Hour 30
EMEA A: 2 Hour 27
EMEA North B: 4 Hour 1 26
NA -
North AmericaE: 3 - 5 Business Days 25
LA - Latin America B: 4 Hour 23
EMEA South B: 4 Hour 20
CORPORATE B: 4 Hour 17
APJK E: 3 - 5 Business Days 13
Other F: 6 - 10 Business Days 6 4
Other G: 11 - 20 Business Days 6 1Hi Srinivas,
I'm using label based menu "filterd rows" only but i couldn't bind the drill down with the 1st stacked column chart.
for mor details:
1) i'm having 2 stacked coumn charts & 1 lebel based menu but eventhough i couldn't able to achieve.
if i select the lebel based values then the 2nd stacked column charts is giving detailed valus but if i keep my mouse on the 1st stacked column chart the there is no changes on the 2nd chart..
somewhere i'm mising the binding data between 1st stacked coumn chart & lebel based menu on drill down part.
Becoz i'm having multiple rows. so can you please try your and send me the xlf file using your concept. -
Free alternatives to JTable that allow multiple rows in the header?
My goal is to make a table with two or more rows in the column header. For instance, imagine I want to have the numbers of the days in the calendar as columns, then I want to have a row over them with the months, spanning over multiple days, then I want to have a row over that with the year, spanning over multiple months. I want these rows to be in the column header so they're always visible as you scroll down.
Right now, I'm doing this with the first rows of the grid, out of the header, so they get out of view when the user scrolls down. Also, by being in the actual grid, they screw up sorting of the table.
So I need 2 features. Multiple rows in the column header and the ability to merge multiple cells in the header.
I've see some code to do this but it seemed too complicated so I want to see if I can find a component that does this out of the box first. I've seen 2 3rd party JTable-based components that do this but they're paid products and paying the full price of a component bundle for these features isn't a very attractive idea.
So, does anyone know of a good table component that does this?Have a look at JXTable from the SwingX project. You should be able to find it via Google.
-
ALV column header with multiple rows and ALV column header span
How could I have in an ALV a column header on several lines and/or a column header that spans on multiple columns?
i.e. I would like to have in an ALV two columns, each with it's own distinct header and additionally another column header for both of the columns, that spans across both columns. How could I program this?Hi,
follow below mentioned code
SET THE LABEL OF THE COLUMN
DATA: hr_weeknum TYPE REF TO cl_salv_wd_column_header.
CALL METHOD lr_weeknum->get_header
RECEIVING
value = hr_weeknum.
CALL METHOD lr_weeknum->set_resizable
EXPORTING
value = abap_false.
hr_weeknum->set_prop_ddic_binding_field(
property = if_salv_wd_c_ddic_binding=>bind_prop_text
value = if_salv_wd_c_ddic_binding=>ddic_bind_none ).
set the text of the column
CALL METHOD hr_weeknum->set_text
EXPORTING
value = 'C Form'.
Regards,
Srini. -
How to convert colon separated column values in multiple rows in report
Hi All,
I want to display colon separated values from a column, in a multi row in report.
For example i have a column1 in a table with value 'A:B:C' , column2 has value '1'.
i want to show in a report three rows using these two columns like
column1 column2
A 1
B 1
C 1Here's one way:
SQL> create table test (col1 varchar2(20), col2 number);
Table created.
SQL> insert all
2 into test values ('A:B:C', 1)
3 into test values ('Dg:Ezs', 2)
4 select * from dual;
2 rows created.
SQL> select
2 t.col2,
3 regexp_substr(t.col1, '\w+', 1, t2.column_value) c1
4 from test t,
5 table(cast(multiset(select level
6 from dual
7 connect by level <= length(t.col1) - length(replace(t.col1, ':', '')) + 1
8 ) as sys.odcinumberlist )) t2
9 order by 2, 1;
COL2 C1
1 A
1 B
1 C
2 Dg
2 Ezs
SQL>Edited by: Littlefoot on Jan 31, 2012 10:13 AM -
ALV Grid Column name in multiple rows
Hi,
When ALV Grid is display columns are displayed horizontally in a single line.
Is there ne way to display the column names in 2 rows
e.g : Functional Location column should be shown as
Functional
LocationHi,
Actually it is not possible to disaply in multiple lines.
ALV grid Multiple Header ... ?
Re: Multiple lines in alv column headings
This problem was discussed many times in this forum, u can search for more results.
Message was edited by: Judith Jessie Selvi -
Query help - Using Alias in column equation
Hi All,
I have selected some data as an alias and would like to refer to the alias in another equation column. I cannot figure out how to do this correctly.
Here is my full query below, with a NULL called '3 Mnth Average' column because I am unsure how to reference the Alias and data type correctly for the required equation.
For example, I want to return the '3 Mnth Average' result being the '3 Mnth Total' Alias divided by 3. ('3 Mnth Total' / 3).... Every time I try... i fail.
Any help would be most appreciated.
Best regards,
John
SELECT T0.ITEMCODE, T0.ITEMNAME,
Cast((T0.ONHAND - T0.IsCommited) As INT) As 'Available',
Cast(T0.ONHAND As INT) As 'In Stock', Cast(T0.IsCommited As INT) As 'Committed',
Cast((SELECT SUM(T1.[Quantity]) FROM INV1 T1 WHERE T1.ITEMCODE = T0.ITEMCODE
AND DATEPART(mm,T1.[DocDate]) >= DATEPART(mm,getdate())-3
AND DATEPART(mm,T1.[DocDate]) <= DATEPART(mm,getdate())) As INT) As '3 Mnth Total',
(NULL) As '3 Mnth Average',
CAST ((SELECT SUM(T1.QUANTITY) FROM INV1 T1 with (NOLOCK)
WHERE MONTH(T1.DOCDATE) = 1 AND T1.ITEMCODE =
T0.ITEMCODE AND YEAR(T1.DOCDATE) = YEAR(GETDATE())) As INT) AS 'JAN QTY',
(SELECT SUM(T1.QUANTITY) FROM INV1 T1 with (NOLOCK)
WHERE MONTH(T1.DOCDATE) = 2 AND T1.ITEMCODE =
T0.ITEMCODE AND YEAR(T1.DOCDATE) = YEAR(GETDATE())) AS 'FEB QTY',
(SELECT SUM(T1.QUANTITY) FROM INV1 T1 with (NOLOCK)
WHERE MONTH(T1.DOCDATE) = 3 AND T1.ITEMCODE =
T0.ITEMCODE AND YEAR(T1.DOCDATE) = YEAR(GETDATE())) AS 'MAR QTY',
(SELECT SUM(T1.QUANTITY) FROM INV1 T1 with (NOLOCK)
WHERE MONTH(T1.DOCDATE) = 4 AND T1.ITEMCODE =
T0.ITEMCODE AND YEAR(T1.DOCDATE) = YEAR(GETDATE())) AS 'APR QTY',
(SELECT SUM(T1.QUANTITY) FROM INV1 T1 with (NOLOCK)
WHERE MONTH(T1.DOCDATE) = 5 AND T1.ITEMCODE =
T0.ITEMCODE AND YEAR(T1.DOCDATE) = YEAR(GETDATE())) AS 'MAY QTY',
(SELECT SUM(T1.QUANTITY) FROM INV1 T1 with (NOLOCK)
WHERE MONTH(T1.DOCDATE) = 6 AND T1.ITEMCODE =
T0.ITEMCODE AND YEAR(T1.DOCDATE) = YEAR(GETDATE())) AS 'JUN QTY',
CAST ((SELECT SUM(T1.QUANTITY) FROM INV1 T1 with (NOLOCK)
WHERE MONTH(T1.DOCDATE) = 7 AND T1.ITEMCODE =
T0.ITEMCODE AND YEAR(T1.DOCDATE) = YEAR(GETDATE())) As INT) AS 'JUL QTY',
CAST ((SELECT SUM(T1.QUANTITY) FROM INV1 T1 with (NOLOCK)
WHERE MONTH(T1.DOCDATE) = 8 AND T1.ITEMCODE =
T0.ITEMCODE AND YEAR(T1.DOCDATE) = YEAR(GETDATE())) AS INT) AS 'AUG QTY',
CAST ((SELECT SUM(T1.QUANTITY) FROM INV1 T1 with (NOLOCK)
WHERE MONTH(T1.DOCDATE) = 9 AND T1.ITEMCODE =
T0.ITEMCODE AND YEAR(T1.DOCDATE) = YEAR(GETDATE())) As INT) AS 'SEP QTY',
(SELECT SUM(T1.QUANTITY) FROM INV1 T1 with (NOLOCK)
WHERE MONTH(T1.DOCDATE) = 10 AND T1.ITEMCODE =
T0.ITEMCODE AND YEAR(T1.DOCDATE) = YEAR(GETDATE())) AS 'OCT QTY',
(SELECT SUM(T1.QUANTITY) FROM INV1 T1 with (NOLOCK)
WHERE MONTH(T1.DOCDATE) = 11 AND T1.ITEMCODE =
T0.ITEMCODE AND YEAR(T1.DOCDATE) = YEAR(GETDATE())) AS 'NOV QTY',
(SELECT SUM(T1.QUANTITY) FROM INV1 T1 with (NOLOCK)
WHERE MONTH(T1.DOCDATE) = 12 AND T1.ITEMCODE =
T0.ITEMCODE AND YEAR(T1.DOCDATE) = YEAR(GETDATE())) AS 'DEC QTY'
FROM dbo.OITM T0
LEFT JOIN dbo.INV1 T1 ON T1.ItemCode = T0.ItemCode
WHERE T0.SellItem = 'Y'
GROUP BY T0.ItemCode, T0.ItemName, T0.OnHand, T0.IsCommited, YEAR(T1.DOCDATE)
HAVING YEAR(T1.DOCDATE) = YEAR(GETDATE())
ORDER BY T0. ITEMCODEHi John,
If you are not using temporary table, you have to rewrite complete formula to get 3 month average. There is no way to reference within the same query.
Thanks,
Gordon -
I want to extend a region LOV based Region - SupplierLovRN, VO
User, tell us your jdev version, please!
Please take your time to rephrase your question. It's hard to understand what you really try to do. The better you describe the use case the better is the help we can give.
Timo
Maybe you are looking for
-
My macbook pro do not conect to my internet
why does my macbook pro dont connect to my wi fi
-
Oracle Collaboration Suite Slow Performance
Hi, I have encountered with problem with OCS 10.1.2 with following information: Hardware : DL-380G5 Operating System : OEL 4 update 5 64 bit Oracle Product : Collaboration Suite 10.1.2 The installation has been done in three server as follow: Server
-
Okay, I built a simple Hello World project with Swing and it worked. Now I've build my own program from scratch and get a "Cannot find the main class" error from the JRE. Below are the contents of the manafest file craeted by Sun Java Studio 8. Manif
-
Building a case structure with four boolean (true/false) inputs.
I currently have two boolean inputs that I would like to use to select cases in a case structure. I would like to have four cases, each one dependent on the true/false condtions of the two booleans. If boolean 1 =true, then case 1 should be used, if
-
TestStand Non blocking deported interface (C#)
Hi, I wish to have a graphic interface (C#) allowing to manage TestStand (Read/Write of the variables FileGlobals, Wait/Set Notifications, ...). This interface must be active during the TestStand activity (graphic indicators of the state of the tests