This request might be asked many times but I have done a search last night to figure out but I came up with nothing.
I have three tables
Table A
ID
City
1
LA
2
NY
3
LV
Table B
ID
Job
11
Programmer
22
Engineer
33
Database Administrator
44
Cyber Security Analyst
Table C
ID
Job level
111
Junior
222
Associate
333
Senior
444
Director
Final table
ID
EmployeeName
City_ID
Job_ID
Level_ID
1000
Susie
1
11
333
1001
Nora
2
11
222
1002
Jackie
2
22
111
1003
Mackey
1
11
444
1004
Noah
1
11
111
I’d like to have a crosstab query using Microsoft Access that returns the following result ( based on city )
LA Table
Jobs
Junior
Associate
Senior
Director
Programmer
1
-
1
1
Engineer
-
-
-
-
Database Administrator
-
-
-
-
Cyber Security Analyst
-
-
-
-
How can I do it?
The best approach for this is always:
Create a "base" query that joins the base tables and returns all data columns that you will need for the crosstab query.
Run the crosstab query wizard using the "base" query as input.
Related
I'm ranking race data for series of cycling events. Racers win various amounts of points for their position in races. I want to retain the discrete event scoring, but also rank the racer in the series. For example, considering a sub-query that returns this:
License #
Rider Name
Total Points
Race Points
Race ID
123
Joe
25
5
567
123
Joe
25
12
234
123
Joe
25
8
987
456
Ahmed
20
12
567
456
Ahmed
20
8
234
You can see Joe has 25 points, as he won 5, 12, and 8 points in three races. Ahmed has 20 points, as he won 12 and 8 points in two races.
Now for the ranking, what I'd like is:
Place
License #
Rider Name
Total Points
Race Points
Race ID
1
123
Joe
25
5
567
1
123
Joe
25
12
234
1
123
Joe
25
8
987
2
456
Ahmed
20
12
567
2
456
Ahmed
20
8
234
But if I use rank() and order by "Total Points", I get:
Place
License #
Rider Name
Total Points
Race Points
Race ID
1
123
Joe
25
5
567
1
123
Joe
25
12
234
1
123
Joe
25
8
987
4
456
Ahmed
20
12
567
4
456
Ahmed
20
8
234
Which makes sense, since there are three "ties" at 25 points.
dense_rank() solves this problem, but if there are legitimate ties across different racers, I want there to be gaps in the rank (e.g if Joe and Ahmed both had 25 points, the next racer would be in third place, not second).
The easiest way to solve this I think would be to issue two queries, one with the "duplicate" racers eliminated, and then a second one where I can retain the individual race data, which I need for the points break down display.
I can also probably, given enough effort, think of a way to do this in a single query, but I'm wondering if I'm not just missing something really obvious that could accomplish this in a single, relatively simple query.
Any suggestions?
You have to break this into steps to get what you want, but that can be done in a single query with common table expressions:
with riders as ( -- get individual riders
select distinct license, rider, total_points
from racists
), places as ( -- calculate non-dense rankings
select license, rider, rank() over (order by total_points desc) as place
from riders
)
select p.place, r.* -- join rankings into main table
from places p
join racists r on (r.license, r.rider) = (p.license, p.rider);
db<>fiddle here
I'm working with a companies CRM database which contains clients that the company works with. Employees can create new client entries through the CRM and when someone creates a new entry and checks multiple services that the client provides, the service codes are all stored in 1 field. Example below:
clientid CompanyName Email Tel Services
1 Randomname1 XXX#... 33333 ;14;294;448
2 Randomname2 yyy#... 44444 ;448
3 Randomname3 zzz#... 55555 ;58;448;14;65;24
So my questions is, what is the best way of storing the services column values? There are around 60 different services that you can choose from.
The classic approach is:
Clients Table:
clientid CompanyName Email Tel
1 Randomname1 XXX#... 33333
2 Randomname2 yyy#... 44444
3 Randomname3 zzz#... 55555
Client_Services Table:
client_id service_id
1 14
1 294
1 448
2 448
3 58
3 448
3 14
3 65
3 24
I have a table with an ID and multiple informative columns. Sometimes however, I can have multiple data for an ID, so I added a column called "Sequence". Here is a shortened example:
ID Sequence Name Tel Date Amount
124 1 Bob 873-4356 2001-02-03 10
124 2 Bob 873-4356 2002-03-12 7
124 3 Bob 873-4351 2006-07-08 24
125 1 John 983-4568 2007-02-01 3
125 2 John 983-4568 2008-02-08 13
126 1 Eric 345-9845 2010-01-01 18
So, I would like to obtain only these lines:
124 3 Bob 873-4351 2006-07-08 24
125 2 John 983-4568 2008-02-08 13
126 1 Eric 345-9845 2010-01-01 18
Anyone could give me a hand on how I could build a SQL query to do this ?
Thanks !
You can calculate the maximum sequence using group by. Then you can use join to get only the maximum in the original data.
Assuming your table is called t:
select t.*
from t join
(select id, MAX(sequence) as maxs
from t
group by id
) tmax
on t.id = tmax.id and
t.sequence = tmax.maxs
I'm doing a class assignment and seem to be having some trouble with one question. The question requires me two join 3 tables and count the overall number of occurrences for each row.
This is the join command I have so far (need to figure out where I can put a nested count(distinct):
SELECT litwork.btitle,
bookcopy.copy_num,
request.rdate
FROM litwork,
bookcopy,
request
WHERE litwork.lit_id = bookcopy.lit_id
AND bookcopy.persidh = request.persid
ORDER BY btitle;
The join gives me this table as result:
BTITLE COPY_NUM RDATE
--------------- -------------- -------
Bankers 2 18-oct-2012
Bankers 2 30-oct-2012
Blue Ridge 1 20-oct-2012
Linux 1 18-oct-2012
Linux 1 30-oct-2012
My Life 3 31-oct-2012
O-O Design 1 30-oct-2012
O-O Design 3 25-oct-2012
O-O Design 3 18-oct-2012
So how would I make a count(distinct) for all three column within that one command? I'm using standard SQL
Edit to add question prompt (copied and paste):
Show how many requests and how many copies exist for each book that is requested. Show title, number of requests, number of copies of the book. Show in order by title. The SQL query for this question mixes a join with count. To understand such a query, be aware that the join must be processed before the count function .
Be also aware that the join may create duplicate data, so that you should use the count(distinct ..) form of count where needed. (Suggestions: if you have trouble with this question, look at the results of the join without count and group, and figure out how you would perform the required count if these results were data of an existing table. Please do not show this extra query in your report.).
Assumption: a given customer may not have more than one request of the same book.. Note that when joining the table REQUEST with the table BOOK_COPY, the number of rows where a given book appears is the product of the number of requests for that book and of the number of copies.
UPDATE: It's still not 100% correct..
Additional Tables used for the Join (First one is LITWORK table, second one is REQUEST table, and last one is BOOKCOPY table)
LIT_ID BTITLE YEAR
---------- --------------- ----------
1001 The Trojans 2000
1002 My Life 2001
1003 Nature 1998
1004 Blue Ridge 1996
1005 True Java 2012
1006 CPlus 2004
1007 Streaming 2000
1008 MyApps 1998
1009 O-O Design 2012
1010 Camping 1978
1011 Bankers 1970
1012 Linux 1962
LIT_ID PERSID BNAME RDATE RTIME
---------- ---------- -------- ----------- ----------
1001 11 College 18-oct-2012
1001 7 College 25-oct-2012
1003 8 La Jolla 20-oct-2012
1005 7 Pacific 18-oct-2012
1008 11 Atlantic 30-oct-2012
1008 1 College 30-oct-2012
1012 4 La Jolla 31-oct-2012
LIT_ID COPY_NUM PERSID_OUT DATE_OUT DATE_DUE PERSIDH HDATE BNAMEP BNAMEC
---------- ---------- ---------- ----------- ----------- ---------- ----------- -------- --------
1001 1 4 13-sep-2012 27-nov-2012 La Jolla
1002 1 Pacific Atlantic
1002 2 1 02-sep-2012 12-oct-2012 Pacific
1002 3 4 15-nov-2012 La Jolla La Jolla
1003 1 9 10-dec-2012 30-dec-2012 Pacific
1003 2 1 13-dec-2012 22-jan-2013 La Jolla
1003 3 Atlantic Atlantic
1004 1 8 19-nov-2012 Pacific College
1004 2 4 04-dec-2012 17-feb-2013 Pacific
1004 3 10 11-oct-2012 31-oct-2012 Atlantic
1005 1 4 27-oct-2012 10-jan-2013 La Jolla
1005 2 1 19-sep-2012 29-oct-2012 Pacific
1006 1 7 29-jan-2012 09-mar-2012 Pacific
1006 2 1 07-jan-2012 16-feb-2012 College
1006 3 Pacific College
1007 1 1 26-oct-2012 05-dec-2012 Pacific
1007 2 Pacific College
1007 3 6 15-oct-2012 04-nov-2012 La Jolla
1008 1 4 23-oct-2012 06-jan-2013 College
1008 2 3 15-oct-2012 24-nov-2012 Pacific
1009 1 1 20-nov-2012 Pacific Pacific
1009 2 11 28-sep-2012 12-dec-2012 Pacific
1009 3 7 22-nov-2012 La Jolla College
1010 1 1 01-sep-2012 11-oct-2012 Pacific
1011 1 4 31-jan-2012 15-apr-2012 La Jolla
1011 2 11 20-nov-2012 College La Jolla
1012 1 11 19-nov-2012 Pacific Atlantic
1012 2 3 29-oct-2012 08-dec-2012 Pacific
This is my current SQL command (not the one I got from Gordon Linoff's comment--that one gave me 2-3 extra rows)
select litwork.btitle,
count(distinct bookcopy.copy_num),
count(distinct request.rdate)
from litwork,
bookcopy,
request
where litwork.lit_id=request.lit_id and
bookcopy.persidh=request.persid
group by btitle;
And that gives me this table (which you can see I have all the correct amount of rows [when you take out the duplicates] but the count numbers are wrong)
BTITLE COUNT(DISTINCTBOOKCOPY.COPY_NUM) COUNT(DISTINCTREQUEST.RDATE)
--------------- -------------------------------- ----------------------------
Linux 1 1
MyApps 2 1
Nature 1 1
The Trojans 3 2
True Java 1 1
The following should work with Oracle's SQL *Plus:
select litwork.btitle, count(distinct bookcopy.copy_num), count(distinct request.rdate)
from litwork, bookcopy, request
where litwork.lit_id = request.lit_id and request.lit_id = bookcopy.lit_id(+)
group by litwork.btitle;
For more info on joins see Oracle SQL*Plus Pocket Reference, 2nd Edition.
Possible solutions to count issue:
Change count(distinct request.rdate) to count(distinct request.PERSID).
Change count(distinct request.rdate) to count(distinct request.*).
Change count(distinct request.rdate) to sum(case when request.rdate is null then 0 else 1 end)
Instead of joining the request table you could join (select lit_id, count(*) as requestCount from request group by lit_id) then select requestCount.
This is probably the best I can do since I do not have the detailed definition of the fields and how they relate. It would also help if I had the expected results and SQL *Plus to test it with.
Say I have an employee table, with a record for each employee in my company, and a column for supervisor (as seen below). I would like to prepare a report, which lists the names and title for each step in a supervision line. eg for dick robbins, 1d #15, i'd like a list of each supervisor in his "chain of command," all the way to the president, big cheese. I'd like to avoid using cursors, but if that's the only way to do this then that's ok.
id fname lname title supervisorid
1 big cheese president 1
2 jim william vice president 1
3 sally carr vice president 1
4 ryan allan senior manager 2
5 mike miller manager 4
6 bill bryan manager 4
7 cathy maddy foreman 5
8 sean johnson senior mechanic 7
9 andrew koll senior mechanic 7
10 sarah ryans mechanic 8
11 dana bond mechanic 9
12 chris mcall technician 10
13 hannah ryans technician 10
14 matthew miller technician 11
15 dick robbins technician 11
The real data probably won't be more than 10 levels deep...but I'd rather not just do 10 outside joins...I was hoping there was something better than that, and less involved than cursors.
Thanks for any help.
This is basically a port of the accepted answer on my question that I linked to in the OP comments.
you can use common-table expressions
WITH Family As
(
SELECT e.id, e.supervisorid, 0 as Depth
FROM Employee e
WHERE id = #SupervisorID
UNION All
SELECT e2.ID, e2.supervisorid, Depth + 1
FROM Employee e2
JOIN Family
On Family.id = e2.supervisorid
)
SELECT*
FROM Family
For more:
Recursive Queries Using Common Table Expressions
You might be interested in the "Materialized Path" solution, which does slightly de-normalize the table but can be used on any type of SQL database and prevents you from having to do recursive queries. In fact, it can even be used on no-SQL databases.
You just need to add a column which holds the entire ancestry of the object. For example, the table below includes a column named tree_path:
+----+-----------+----------+----------+
| id | value | parent | tree_path|
+----+-----------+----------+----------+
| 1 | Some Text | 0 | |
| 2 | Some Text | 0 | |
| 3 | Some Text | 2 | -2-|
| 4 | Some Text | 2 | -2-|
| 5 | Some Text | 3 | -2-3-|
| 6 | Some Text | 3 | -2-3-|
| 7 | Some Text | 1 | -1-|
+----+-----------+----------+----------+
Selecting all the descendants of the record with id=2 looks like this:
SELECT * FROM comment_table WHERE tree_path LIKE '-2-%' ORDER BY tree_path ASC
To build a tree, you can sort by tree_path to get an array that's fairly easy to convert to a tree.
You can also index tree_path and the index can be used when the wildcard is not at the beginning.
For example, tree_path LIKE '-2-%' can use the index, but tree_path LIKE '%-2-' cannot.
Some recursive function which either return the supervisor (if any) or null. Could be a SP which invokes itself as well, and using UNION.
SQL is a language for performing set operations and recursion is not one of them. Further, many database systems have limitations on recursion using stored procedures as a safety measure to prevent rogue code from running away with precious server resources.
So, when working with SQL always think 'flat', not 'hierarchical'. So I would highly recommend the 'tree_path' method that has been suggested. I have used the same approach and it works wonderfully and crucially, very robustly.