This question already has answers here:
Combine values from related rows into a single concatenated string value
(1 answer)
SQL Query to Group By and Concat rows
(1 answer)
Closed 2 years ago.
I've been asked to take an MSAccess query with results like this:
Name Age Skills
Jim 42 Access
Jim 42 Excel
Jane 38 Access
And turn it into this:
Name Age Skills
Jim 42 Access, Excel
Jane 38 Access
I have no idea where to start with something like this, or if it can be done. Doesn't matter if it's a query or if I have to write a function, so long as the results are the same. Can anyone help?
Related
This question already has answers here:
How to use count and group by at the same select statement
(11 answers)
Closed 6 months ago.
I want to count a certain value in a column and output it as a number.
Here is an example:
id
job
1
police
2
police
3
ambulance
Now I want to count the value "police" in the "job" column and make the result in a number, so because there are two entries with "police" in the column it would be as output the number two. With the value "ambulance" it is only one entry so the result would be 1.
Can anyone tell me how to write this as code?
I have now searched a lot on the Internet and tried myself but I have found nothing that worked.
You're saying you want to count how many of each type of job there is, right?
SELECT COUNT(*), job
FROM tablename
GROUP BY job
This question already has an answer here:
Case sensitive WHERE in Access 2010
(1 answer)
Closed 6 months ago.
when using sql in access database, for string criteria, is it case sensitive? e.g. where PersonName like "LINDA", is equal to: where PersonName like "linda"?
You could use the StrComp function in an Access query
SELECT tblSource.ID, tblSource.Test
FROM tblSource
WHERE (((StrComp([tblSource].[Test],"LINDA",0))=0));
This will return only the rows with LINDA, it will not return Linda or LInda etc.
This question already has answers here:
What is the best way to create and populate a numbers table?
(12 answers)
Closed 4 years ago.
I am wondering if it is possible to get a query that will take a range of numbers, in this case 8 to 17, compare it against a field in a table and remove the ones that do appear in the table and return the rest?
I assume the peusdo code would look something like
Select nums from range(8-17) where nums not in (select column from table)
Is this possible at all?
Edit
To clarify my question.
In table I might have the following:
Intnumber
9
10
16
I would like to have the numbers between 8-17 that do not appear in this table, so 8,11,12,13,14,15,17
Kind regards
Matt
select nums from table where nums not between 8 and 17;
This question already has answers here:
Efficiently convert rows to columns in sql server
(5 answers)
Closed 6 years ago.
I have this table:
Value | Name
300 | moshe
400 | yoni
500 | niv
And i would like to convert it into this:
nameColumn: moshe yoni niv
value: 300 400 500
The value is float type and name is nchar(20).
anyone?
thanks
Most databases have a PIVOT relational operator (link for SQL Server) to turn the unique values of a specified column from multiple rows into multiple column values in the output (cross-tab), effectively rotating a table.
This question already has answers here:
Simple way to transpose columns and rows in SQL?
(9 answers)
Pivot Dynamic Columns, no Aggregation
(1 answer)
Closed 7 years ago.
I have a table Dev with the data below
YYYMMDD Atest BTest CTest
20150525 100 200 300
20150526 110 210 310
20150527 120 220 320
I need output like below
xyz 20150525 201050526 20150527
Atest 100 110 120
BTest 200 210 220
CTest 300 310 320
How can i achieve above result set. My table Dev will grow and i need the result set table to build columns dynamically and display the data as required.
Any help is appreciated. If you suggest pivot, may i know what field should i use for aggregation and how to use it. Thanks.
First you will need to unpivot your table and after that pivot again. This is the key idea:
select * from TableName
unpivot(v for xyz in([Atest],[Btest],[Ctest]))u
pivot(max(v) for yyyymmdd in([20150525],[20150526],[20150527]))p
Fiddle http://sqlfiddle.com/#!3/a675c/1
As for dynamic sql, you can find many example of how you can use STUFF function to concatenate distinct dates in one string and construct dynamic query. For instance T-SQL dynamic pivot