This question already has answers here:
Concat groups in SQL Server [duplicate]
(5 answers)
SQL in (#Variable) query
(3 answers)
Closed 9 years ago.
I am using SQL Server 2008 and inherited a database that did not use many to many. They instead used a comma-separated column. I have found how to link the comma-separated values to the name of the program. But I need a list of the programs and the offices they belong to, like this
OFFICE table:
ID Name
--- ------
1 HQ
2 PA
3 CEO
PRG table:
ID Name Office Affected
-- ---- ---------------
A PRG1 1,3
B PRG2 2
C PRG3 2,3
D PRG4 1,2
Output that I need :
Name Programs
---- ---------
HQ PRG1, PRG4
PA PRG2, PRG3, PRG4
CEO PRG1, PRG3
You can manage to do this. However, because storing lists in strings is a bad idea, I don't want to compound that by putting them back in a comma-delimited list. Instead, the following query produces the data in a more normalized form, with one row per office name and program:
select o.name, p.name as program_name
from prg p join
office o
on ','+p.OfficeAffected+',' like '%,'+cast(o.id as varchar(255)) + ',%';
Related
This question already has answers here:
Q: How to exclude persons name from a table
(2 answers)
Closed 1 year ago.
I am trying to use bigquery to extract data about 10 most mentioned personalities in the leading newspapers in Israel using this code:
SELECT
person,
COUNT(1) AS count_mentions,
COUNT(DISTINCT url) AS count_distinct_urls
FROM
`composed-hold-309910.dataset_1.israel_media_person`
GROUP BY
person
ORDER BY
count_mentions DESC
LIMIT
10;
Unfortunately, some of the results iv'e gotten were not actual people but some "buzzwords" like 'Maccabi Haifa' and 'Gaza Gaza'
person
count_mentions
count_distinct_urls
Benjamin Netanyahu
32965
20660
------------------
----------------
--------------------
Maccabi Haifa
16528
5947
------------------
----------------
--------------------
Gaza Gaza
13267
7623
I would be delighted to find a way to eliminate these false results.
Matan
You can filter out these buzzwords by using where person not in ([list_buzz_word])
This question already has answers here:
Simulating group_concat MySQL function in Microsoft SQL Server 2005?
(12 answers)
Closed 4 years ago.
I have looked at what was marked as the duplicate of this and it is not. I'm pulling from two tables, not one.
First, allow me to say I had nothing to do with the design of this database.
I have two tables that must be joined, and then an unknown amount of rows where the data must be concatenated into one giant string. They are joined by the Record ID.
Item table:
Item RecordID
---------------------
Car A 123
Car B 456
Car C 789
Yes, the words literally cut off in the middle. There should be nothing added between the values, and I also need to keep the commas and other special characters.
Details table:
RecordID Details
--------------------------------
123 black pain
123 t, radials
123 , green le
123 ather, spo
123 rt steerin
123 g wheel, b
123 uilt-in GP
123 S
456 standard
789 black leat
789 her, teles
789 coping ste
789 ering whee
789 l, seven c
789 up holders
789 , heavy du
789 ty mudflap
789 s
What I want to end up with is this:
ItemID RecordID Details
----------------------------------------------------------------------------
Car A 123 black paint, radials, green leather, sport steering wheel, built-in GPS
Car B 456 standard
Car C 789 black leather, telescoping steering wheel, seven cup holders, heavy duty mudflaps
I've looked at all the XML ones and can't figure out how to do this.
Thanks in advance.
There is no guarantee that your STUFF/ FOR XML PATH will produce the results you ask for unless you have an IDENTITY field in your Details table or some other value that you can sort by that will force the order of the Details text.
Usually you could use the STUFF command with an ORDER BY statement
SELECT
Item.Item AS ItemID,
Item.RecordID,
STUFF( (
SELECT
'' + Details
FROM
Details
WHERE
Details.RecordID = Item.RecordID
-- ORDER BY SomeLineIndicator
FOR XML PATH ('')
), 1, 0, '' ) AS Details
FROM
Item
I tried this on my box without the ORDER BY and just so happened to get the result you're asking for, but you really can't rely on these results without a field you can use to force the order.
Please read this post and the linked articles for more information about why you'd need a field for this and why you can't depend on an undetermined internal "index" to take care of it for you: Default row order in SELECT query - SQL Server 2008 vs SQL 2012
I am using MS Access and I have a rather complex situation.
I have Respondents who are linked to varying numbers of different Companies via 2 connecting tables. I want to be able to create a list of distinct customers which excludes any customer associated with Company X.
Here is a pic of the relationships that are involved with the query.
And here is an example of what I'm trying to achieve.
RespondentRef | Respondent Name
8 Joe Bloggs
.
RespondentRef | GroupRef
8 2
.
GroupRef | CompanyRef
2 10
.
CompanyRef | CompanyName
10 Ball of String
I want a query where I enter in 'Ball of String' for the company name, and then it produces a list of all the Respondents (taken from Tbl_Respondent) which completely excludes Respondent 8 (as he is linked to CompanyName: Ball of String).
Tbl_Respondent
RespondentRef | Respondent Name
... ...
7 Bob Carlyle
9 Anton Boyle
I have tried many combinations of subqueries with <> and NOT EXISTS and NOT IN and nothing seems to work. I suspect the way these tables are linked may have something to do with it.
Any help you could offer would be very much appreciated. If you have any questions let me know. (I have made best efforts, but please accept my apologies for any formatting conventions or etiquette faux-pas I may have committed.)
Thank you very much.
EDIT:
My formatted version of Frazz's code is still turning resulting in a syntax error. Any help would be appreciated.
SELECT *
FROM Tbl_Respondent
WHERE RespondentRef NOT IN (
SELECT tbl_Group_Details_Respondents.RespondentRef
FROM tbl_Group_Details_Respondents
JOIN tbl_Group_Details ON tbl_Group_Details.GroupReference = tbl_Group_Details_Respondents.GroupReference
JOIN tbl_Company_Details ON tbl_Company_Details.CompanyReference = tbl_Group_Details.CompanyReference
WHERE tbl_Company_Details.CompanyName = "Ball of String"
)
This should do what you need:
SELECT *
FROM Tbl_Respondent
WHERE RespondentRef NOT IN (
SELECT gdr.RespondentRef
FROM Tbl_Group_Details_Respondent gdr
JOIN Tbl_Group_Details gd ON gd.GroupRef=gdr.GroupRef
JOIN Tbl_Company_Details cd ON cd.CompanyRef=gd.CompanyRef
WHERE cd.CompanyName='Ball of String'
)
This question already has answers here:
Concatenate multiple rows in one field in Access? [duplicate]
(4 answers)
Closed 8 years ago.
How can I group multiple rows of data?
My data structure is similar to:
ID NAME PhoneNo
1 Jon 8798765
2 Jon 3134684
3 Adams 7968434
4 Phil 3435435
5 Thomas 6734354
6 Jon 2343545
7 Jeff 3435424
8 Adams 3434354
I need to use SQL to group the information so I get something like this:
ID NAME PhoneNo
1 Jon 8798765,3134684,2343545
3 Adams 7968434,3434354
4 Phil 3435435
5 Thomas 6734354
7 Jeff 3435424
See what I did there? I de-duplicated and added all the phone numbers on the same field, comparing the names: Same name= same person, so put all the names on the same cell.
I'm currently using MS access, but I guess any other variant could work (I can find the equivalency)
What you are looking for is mySQL function GROUP_CONCAT
If you are in Access, just use some kind of Macro inspired by this: is there a group_concat function in ms-access? or for MSSQL Emulating MySQL’s GROUP_CONCAT() Function in SQL Server 2005
There is a Question, what are you really need.
This question already has answers here:
How to retrieve two columns data in A,B format in Oracle
(4 answers)
Closed 9 years ago.
I have been asked to create a report using PL/SQL to get names associated with a given city. While this is not in the least bit difficult, I find that the way in which the data is to be presented is something I have not seen from SQL. The report needs to be formatted such that the city name appears first and all subsequent people associated with that city are to be listed after that - in a single line.
TEMPE: Rich Allen, Jerry Black, et al..
TUSCON: Bob Adams, Frank Bruce, et al..
I can't recall ever seeing output like this and am a bit stuck on how to present this data.
Any suggestions would be appreciated.
Select city, wmConcat(namefield)
from tablename
group by City
WM_CONCAT for versions 10g and prior
LISTAGG for newer versions.