I work on an Oracle database.
I have a table (it is a join table) but this is how it looks:
CustomerID days_attached Startdate enddate team
1 7 01-01-2016 08-01-2016 A
1 2 09-01-2016 10-01-2016 B
1 8 01-02-2016 09-02-2016 A
2 1 01-02-2017 02-02-2016 C
2 8 08-05-2017 16-05-2017 C
I need to know how long a person is attached to a specific team. A person can be attached to a person for a X amount of days. That person could be in a team. For instance in this case, how long is a person attached to team A = 7+8 15 days.
How do I get this in a SQL statement?
Our app only supports SQL not PL/sql .
I expect an output like:
CustomerID days_attached team
1 15 A
1 2 B
2 9 C
select customer, team, sum(dayattached) from table_name group by customer, team
hopefully this will help u
Related
I was wonder if it is possible preferably using a select statement on PL/SQL V11 to get the following results from this table:
Area Store Product
10 1 A
10 1 B
11 1 E
11 1 D
10 2 C
10 2 B
10 2 A
10 3 B
10 3 A
13 1 B
13 1 A
and Return this result, so it groups by Area, and Store and looks for and area and store with the same products. So Area 10 Store 1 has products A and B so it will look at the list for other stores that only have A and B and count them. In this example it counts Area 10 store 1/Area 10 store 3/Area 13 Store 1.
Product Count of groups
AB 3
ABC 1
DE 1
Thanks in advance for the help.
Yes, you can use listagg() and then another group by:
select products, count(*)
from (select listagg(product) within group (order by product) as products
from t
group by area, store
) p
group by products;
I'm looking to write a query that can count missing entries from a table of dates based on skills that a resource has to forecast availability of resource for booking. I'm not sure if it can be done and I'm certainly struggling with the logic!!
Tables
Dates
ID dateFrom StaffID
1 01-06-2014 1
2 02-06-2014 1
3 03-06-2014 1
4 04-06-2014 1
5 05-06-2014 1
6 01-06-2014 2
7 03-06-2014 2
8 04-06-2014 2
9 05-06-2014 2
10 06-06-2014 2
(Free dates on the 6th for staffID 1 and 2nd for staffID 2)
Staff
StaffID Name
1 John
2 Paul
Skills
ID StaffID SkillID
1 1 1
2 1 2
3 1 3
4 2 2
5 2 3
6 2 4
So I want to write a query that says in June, for each of the skills there is X no of days available to book. Is this even possible? looking for records that don't exist to join with a staff table?
I've put together a calendar table that can identify days without bookings but I'm struggling from there on to be honest.
Any help would be greatly appreciated!!
Steve
EDIT: DB is SQL 2005.
Expected output (if possible)
SkillID Number of days available
1 20
2 22
3 14
etc
create a calendar table with all possible dates (booked or not)
select count(distinct ad.calendarDate), s.SkillID
from all_dates ad
cross join skills s
where not exists (
select 1 from
dates where dateFrom = ad.calendarDate
and StaffID = s.StaffID
)
group by s.SkillID
If I understand your problem, your query will be some thing like:
Select sum(temp.nbrDate), temp.SkillID from
(Select s.SkillID, count (d.ID) as nbrDate from Skills s, Dates d
where s.StaffID = d.StaffID
Group by SkillID) temp
group by SkillID
If you want to add a date range, add this in your where close:
and d.DateForm between '01-06-2014' and '30-06-2014'
Following is my table schema.
Channel listing table:
CHAN_NUMBER CHAN_NAME CHAN_TYPE
----------- -----------------------------------
1 MTV Music
2 ESPN Sports
3 TNT Movies
4 Fox Movies
5 Fox Sports
customer survey table:
SURV_ID SURV_DATE SURV_FAV_CHAN CUST_NUMBER
---------- --------- ------------- -----------
1 25-NOV-12 1 2
2 24-NOV-12 2 1
3 24-NOV-12 3 3
4 24-NOV-12 4 4
5 24-NOV-12 5 5
6 24-NOV-12 1 6
7 24-NOV-12 2 7
8 24-NOV-12 3 8
9 24-NOV-12 4 9
10 24-NOV-12 5 10
11 24-NOV-12 1 11
I have these two tables that I need to generate a report that lists every channel and a count of how many customers have selected that channel as their favorite.
On oracle database I got up to the point where I am generating a count of each time a channel was selected as a favorite from the SURVEY table. But I can't figure out how to join them to create a list of channels displaying the channel number, the name and the count of customers who chose it as their favorite.
-- my channel table query
SELECT CHAN_NUMBER, CHAN_NAME FROM CHANNEL;
-- here is how I'm generating the couNt
SELECT COUNT(SURV_FAV_CHAN) FROM SURVEY
GROUP BY SURV_FAV_CHAN HAVING COUNT(SURV_FAV_CHAN) > 1;
ANY HELP WOULD BE AWESOME.
Please check this reference * SQLFIDDLE
You said you want list every channel, and count of how many customers have selected that channel as their favourite.
Let's go from nested to outside. Nested query you count number of customers from Survery table grouping by favourite channel. Every channel means, you need to do a LEFT JOIN on Channels table to get all the records.
Query:
(select c.*, s.ct from
channel c
left join
(select count(cust_number) as ct
, surv_fav_chan from survey
group by surv_fav_chan) as s
on c.chan_number = s.surv_fav_chan
;
Results:
CHAN_NUMBER CHAN_NAME CHAN_TYPE CT
1 MTV Music 3
2 ESPN Sports 2
3 TNT Movies 2
4 Fox Movies 2
5 Fox Sports 2
You seem to treat FOX as two channels offering two different types of programmes. So I have left it as it is. If you want to even count customers by channel type then please clarify.
PS: You may ignore the other old table schema in that SQLFIDDLE table sample. NOTE that it is in MYSQL, however this is an ANSI query - so you may apply it to ORACLE as well.
You can try something like this:
SELECT MAX(CH.CHAN_NUMBER), MAX(CH.CHAN_NAME), COUNT(SRV.SURV_FAV_CHAN) FROM SURVEY SRV
LEFT JOIN CHANNEL CH ON CH.CHAN_NUMBER = SRV.SURV_FAV_CHAN
GROUP BY SRV.SURV_FAV_CHAN HAVING COUNT(SRV.SURV_FAV_CHAN) > 1;
And you may want to use SUM(SRV.SURV_FAV_CHAN) if you really need the total amount of customers if I understand you question correctly
Assuming that SURV_FAV_CHAN and CHAN_NUMBER is the relation, use that for your JOIN, so try this:
SELECT CHAN_NUMBER
, CHAN_NAME
, COUNT(DISTINCT SURVEY.CUST_NUMBER) AS FAV_CHANNEL_CNT
FROM CHANNEL
LEFT JOIN SURVEY
ON SURVEY.SURV_FAV_CHAN = CHANNEL.CHAN_NUMBER
GROUP BY CHAN_NUMBER, CHAN_NAME
I have two tables in a firebird 1.5 database, the tables are client and notes, in the notes table there can be multiple records for each corresponding record in Client table, and sometimes none.
The tables are structured like
Client
Client_id name
-----------------
1 Sam
2 Lee
3 Steve
4 Linda
5 Sue
6 Jill
7 Jack
Notes
Notes_id client_id Note
------------------------------
1 1 New
2 1 do not send
3 2 old
4 2 likes
5 4 do not send
6 5 new
7 5 Cats and Dogs
8 5 drives
I would like to run a select statement that would only return records from the Client table where there is no note named ‘do not send’ linked to the client in the notes table. So with the above examples the select statement would only return the following records from the client table.
Client_id name
-----------------
2 Lee
3 Steve
5 Sue
6 Jill
7 Jack
Is this possible? Any assistance with this would be appreciated.
Regards Alan
Below are three queries that will do the task:
SELECT
c.*
FROM
client c
WHERE
NOT EXISTS(SELECT * FROM notes n WHERE n.client_id = c.client_id
AND n.note = 'do not send')
or
SELECT
c.*, n.client_id
FROM
client.c LEFT JOIN
(SELECT client_id FROM notes WHERE note = 'do not send') n
ON c.client_id = n.client_id
WHERE
n.client_id IS NULL
or
SELECT
c.*
FROM
client c
WHERE
NOT c.client_id IN (SELECT client_id FROM notes n
WHERE n.note = 'do not send')
I have a scenario as follows:
ManufacturerID Name
1 XXX
2 YYY
DeptID Name ManufacturerID
1 abc 1
2 bcd 1
3 efg 2
ProductID name deptid
1 dfdfg 1
2 dfdg 2
3 sdfsd 2
PartsID name productid
1 sdfs 1
2 sfdfs 2
3 sdd 1
I want the above table structure to be made as hierarchical using levels. How do I design the table?
I cannot understand what hierarchy you need to make, but if you're using sql server 2008 please take a look at hierachyId datatype.
And please provide more desdcription about you process. Now it looks straightforward:
Manufacturer has many departments, department has many products, product has many parts.