need to get group by data using join/subQuery on multiple tables - sql

I have table lookup with columns trackingId and string
trackingId
string
1
QQ
3
we
2
QQ
4
rt
.
..
.
..
.
..
select trackingId from lookup where string = 'QQ'
I need to use output of this query in table - content_item. content_item follow below
content_id
tracking_id
approval_status
date
101
1
AP
2014
102
1
PN
2016
103
3
PN
2015
104
4
AP
2018
105
2
RJ
2019
106
4
PN
2019
107
5
PN
2019
108
6
RJ
2019
...
.
..
....
I want to group this data by date
my output should be like
year
count
2014
1
2019
3
....
.
means in each group tracing ids should be unique.
(if one tracking id is counted in one group then it should not be counted in any other group)
Also data of tracking id 3 and 4 should not be counted (b'z I only want data for string QQ).
Please let me know if this is doable. any help will be appreciated...

JOIN the tables to get the 'QQ' content_items. Then GROUP BY and COUNT() the rows:
select c.date, count(*)
from content_item c
join lookup l on c.tracking_id = l.tracking_id
where l.string = 'QQ'
group by c.date

Related

Update a column in the same query as it is to be made in?

In SQL Server, I am trying to create a temp table then join based on the unique index key criteria set forth, organizing them based on their system numbers, it looks a little something like this:
SELECT DISTINCT
t1.[SysNr] AS [oldSysNr], t1.[SysNr] AS [newSysNr],
t1.[Year], t1.[WorkTm], t1.[AMSTC],
t1.[AJCC], t1.[ShifNum], t1.[FMN]
INTO
#TEMPTABLE
FROM
AppData.SCHED AS t1
JOIN
AppData.SCHED AS tS ON t1.ShifNum = tS.ShifNum
AND t1.WorkTM = tS.WorkTM
AND t1.FMN = tS.FMN
WHERE
t1.Year = 2019;
This will essentially give me the old records needed by the oldSysNr column as the year is the param I am looking at. However, I need to update the newSysNr column in the same query, no separate query to update. Is there any way that this can be achieved? I only know of doing update as a separate query.
Here is the data that is produced from the query:
oldSysNr NewSysNr Year WorkTm AMSTC AJJC SHIFNUM FMN
-----------------------------------------------------------
24 24 2019 3 WVB GH 9 YUBMS
25 25 2019 3 BMS MJ 8 YUBMS
26 26 2019 3 BMS PP 8 MJUNL
27 27 2019 4 ZMG MJ 5 MJUNL
28 28 2019 5 BR OP 3 COLHP
Here are the results that are desired:
oldSysNr NewSysNr Year WorkTm AMSTC AJJC SHIFNUM FMN
-----------------------------------------------------------
24 57 2019 3 WVB GH 9 YUBMS
25 58 2019 3 BMS MJ 8 YUBMS
26 59 2019 3 BMS PP 8 MJUNL
27 60 2019 4 ZMG MJ 5 MJUNL
28 61 2019 5 BR OP 3 COLHP
As mentioned, the param it is looking at is the year, and given that, the entire idea is that it would update NewSysNr provided that it was given the same join statement but it would be another year like 2020. Help in any given way on this issue is greatly appreciated.
You should be able to nest a subquery in the FROM clause. In other words, instead of selecting columns x,y,z from the table itself, you can replace the table name with a query which uses an updated WHERE clause.
So in this case it would look something like this: (Note: you may need to adjust the aliasing in the subquery...I'm just lazy. Also, you don't need to select all the columns in the subquery. The query in parenthesis is the subquery)
SELECT DISTINCT
tS.[SysNr] AS [newSysNr], t1.[oldSysNr]
t1.[Year], t1.[WorkTm], t1.[AMSTC],
t1.[AJCC], t1.[ShifNum], t1.[FMN]
INTO
#TEMPTABLE
FROM
(SELECT DISTINCT
t1.[SysNr] AS [oldSysNr]
t1.[Year], t1.[WorkTm], t1.[AMSTC],
t1.[AJCC], t1.[ShifNum], t1.[FMN]
FROM
AppData.SCHED AS t1
JOIN
AppData.SCHED AS tS ON t1.ShifNum = tS.ShifNum
AND t1.WorkTM = tS.WorkTM
AND t1.FMN = tS.FMN
WHERE
t1.Year = 2018)
AS t1
JOIN
AppData.SCHED AS tS ON t1.ShifNum = tS.ShifNum
AND t1.WorkTM = tS.WorkTM
AND t1.FMN = tS.FMN
WHERE
t1.Year = 2019;

to join the same table to match records from last year

I want to create a join that joins records of last year to the same period of the current year. All the data is in the same table.
Input Table:
A | B | C | D
a 2017 1 10
a 2017 2 20
a 2017 3 5
a 2016 1 100
a 2016 2 50
a 2016 3 1
Output Table:
A | B | C | D | E
a 2017 1 10 100
a 2017 2 20 50
a 2017 3 5 1
a 2016 1 100 NULL
a 2016 2 50 NULL
a 2016 3 1 NULL
There are several ways of doing this. One is a left join:
select t.*, tprev.d as e
from t left join
t tprev
on t.a = tprev.a and t.c = tprev.c and tprev.b = t.b - 1;
Another way is to use window functions:
select t.*, lag(t.d) over (partition by t.a, t.c order by b) as e
from t;
These work on the same on your data. But they are subtly different. The first looks specifically at the data one year before. The second looks at the most recent year before where there is data.

Search for a value of a column in another column across the rows in SQL using analytic functions?

Here's how my data looks like:
ID month desired_month val
1 6 4 101
1 4 6 102
2 12 4 103
2 4 3 104
2 3 12 105
I want look across all rows for the same ID and find the row which has month equal to the desired_month of the row in question and then return the val from the row that matched.
Resultant dataset would look like this:
ID month desired_month val val_from_row_that_matched_desired_month
1 6 4 101 102
1 4 6 102 101
2 12 4 103 104
2 4 3 104 105
2 3 12 105 103
I would like to achieve this using analytic functions in Oracle SQL
you can get desire output by using self join into same table like this.let suppose your table name is tab.
select t1.*,t2.val from tab t1
inner join tab t2
on(t1.desired_month=t2.month);
Assuming there is just one and only one row with desired month matching each (ID, month) combination in the data
select
a.id, a.month,
a.desired_month,
a.val, b.val as val_from_desired_month
from input a, input b
where
a.id = b.id
and a.desired_month = b.month
MAY BE ANSWER SHOULD BE LIKE THIS:-
SELECT E.ID,E.MONTH,M.DESIRED_MONTH, E.VAL,M.VAL AS "val_matched_desired_month"
FROM ID_MONTH_DESIRED_MON_VAL E,ID_MONTH_DESIRED_MON_VAL M
WHERE E.ID =M.DESIRED_MONTH
ORDER BY ID

Find missing dates from multi table query

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'

Get 1 row from multiple columns

I have 2 tables
table#1: Order
orderid unitid active
1 aa 1
2 bb 0
3 cc 1
4 dd 1
table#2:Details
orderid month
1 6
1 7
1 12
2 1
2 6
3 1
3 2
3 3
3 4
3 6
Output desired:
orderid unitid jan feb mar apr may jun ......... dec
1 aa yes yes
3 cc yes yes yes yes
For all orders where ACTIVE is 1 and all unitids.
I tried using case statement, i get multiple rows for a single orderid, which is not how i want.
I see a lot of examples for pivot with one table, how to do this using 2 tables? I am using SQL Server 2012.
Maybe a Select within a SELECT as argument
Something like this;
Select orderid, unitid, (SELECT month
From Table2
WHERE ...)
From table1
Where ...
I am referencing with this answer this Issue:
A select query selecting a select statement