I have been struggling with this for a while now so maybe someone can shed some insight.
We have a practice query that comes from a TVShow database that for this problem has 4 tables.
This is the query: Sponsors that sponsor all tv shows by ABC
What I have tried so far is this but it doesn't seem to be working:
SELECT DISTINCT RSPONSOR.SPONSOR_NAME
FROM RSPONSOR
WHERE NOT EXISTS (
SELECT *
FROM RTVSHOW
WHERE NOT EXISTS (
SELECT *
FROM RSPONSORBY
WHERE RSPONSOR.SPONSOR_NAME = RSPONSORBY.SPONSOR_NAME
AND RSPONSORBY.SHOW_NUM = RTVSHOW.SHOW_NUM
AND RTVSHOW.NETWORK_ID = 'ABC'
)
);
Would love any help! Thanks in advance.
Here are the tables for reference
--RTVSHOW--
SHOW_NUM NUMBER
SHOW_NAME VARCHAR2(20 BYTE)
START_MONTH NUMBER
START_YEAR NUMBER
END_MONTH NUMBER
END_YEAR NUMBER
NETWORK_ID VARCHAR2(20 BYTE)
DISTR_NAME VARCHAR2(20 BYTE)
--RSPONSOR--
SPONSOR_NAME
PARENT_NAME
--RSPONSORBY--
SHOW_NUM
SPONSOR_NAME
--RNETWORK--
NETWORK_ID
NETWORK_HQ
PARENT_NAME
select rSponsor.sponsor_name, rSponsor.parent_name
from rSponsor
join rSponsorBy
on rSponsor.sponsor_name = rSponsorBy.sponsor_name
join rTVShow
on rSponsorBy.show_num = rTVShow.show_num
join rNetwork
on rTVShow.network_id = rNetwork.network_id
where rNetwork.network_id = 'ABC'
group by rSponsor.sponsor_name, rSponsor.parent_name
having count(distinct rTVShow.show_num) = --ABC shows sponsored by this sponsor
(
select count(distinct rTVShow.show_num) --ABC shows
from rTVShow
join rNetwork
on rTVShow.network_id = rNetwork.network_id
where
rNetwork.network_id = 'ABC'
);
Related
Example tables and desired result:
The result table shown below is the output I actually want.
tried the following query with pivot:
with pivot_data AS
(
select client_id
,ph_type
,Ph_number
from client_table
inner join phone_table
on client_table.phone_id = phone_table.ph_id
)
select *
from pivot_data
pivot (sum(ph_number)
for ph_type in ('c','w','h')
);
Result I got:
Any help would be appreciated.
Answers in sql server would be great but oracle & mysql is also welcome if they can point me in the right direction. :)
Thanks in advance.
Oracle Query:
SELECT *
FROM (
SELECT client_id, priority, phone_number, phone_type
FROM client_table c
LEFT OUTER JOIN
phone_table p
ON ( c.phone_id = p.phone_id )
)
PIVOT ( MAX( phone_type ) AS phonetype, MAX( phone_number ) AS phonenumber
FOR priority IN ( 1 AS Prio1, 2 AS Prio2, 3 AS Prio3 ) );
Output:
CLIENT_ID PRIO1_PHONETYPE PRIO1_PHONENUMBER PRIO2_PHONETYPE PRIO2_PHONENUMBER PRIO3_PHONETYPE PRIO3_PHONENUMBER
---------- --------------- ----------------- --------------- ----------------- --------------- -----------------
1 C 9999999999 H 5555555555 W 7777777777
You really need to do some reading on set based thinking and how what you are asking for will be very detrimental to your maintenance of the SSIS solution moving forwards.
All you need to do is export the data as is. If you absolutely have to have it all in the one CSV file, just join the two tables together and retain a normalised, scalable dataset that won't break if the number of priorities increases:
select c.client_id
,c.phone_id
,c.priority
,p.phone_type
,p.phone_number
from #Client c
join #Phone p
on c.phone_id = p.phone_id
I have a table in SQL which will contain multiple rows for one id, as below
accountid Productname
1 GL
1 IP
1 MI
2 GL
2 IP
2 PA
3 MI
3 CP
3 IP
4 GL
4 CP
4 CI
I want to be able to select all accounts which have certain products but not other. For example all that have IP or GL but not MI, using the sample table above this would return accounts 2 and 4.
SELECT ccx_accountidname
FROM (
SELECT ccx_accountidname, ccx_productname
FROM Filteredccx_leadresearch
WHERE ccx_productname IN ('GL','IP')
AND ccx_accountidname IS NOT NULL
) AS T
WHERE ccx_productname NOT IN ('MI')
ORDER BY ccx_accountidname
and
SELECT DISTINCT LR1.ccx_accountidname
FROM Filteredccx_leadresearch LR1
LEFT JOIN Filteredccx_leadresearch LR2 ON LR1.ccx_accountid = LR2.ccx_accountid
AND LR2.ccx_productname IN ('GL', 'IP')
WHERE LR1.ccx_productname NOT IN ('MI')
AND LR1.ccx_accountidname IS NOT NULL
ORDER BY LR1.ccx_accountidname
Both give basically the same results, is there any way this can be done?
Thanks in advance for any help
Could you try this:
SELECT DISTINCT T1.Accountidname FROM TheTableThatContainsAccountnames as T1
JOIN AccountProductsTable as T2 on T1.AccountId=T2.AccountId
WHERE T2.ProductName = 'ProductYouWant'
AND T2.ProductName = 'AnOtherProductYouWant'
According to your post, all you really need is a simple query with the correct and logic. You want all accounts with Product name GL or IP but not in MI. This will do it without any other joins.
SELECT ccx_accountidname
FROM Filteredccx_leadresearch
WHERE
ccx_productname in ('GL','IP')
and ccx_productname not in ('MI')
EDIT
This will get you the account, though I doubt it will work in your overall solution. It's just hard to tell without seeing your complete dataset. This could be done with parameters too.
IF OBJECT_ID('tempdb..#TempTable') IS NOT NULL
DROP TABLE #TempTable
IF OBJECT_ID('tempdb..#TempTableTwo') IS NOT NULL
DROP TABLE #TempTableTwo
create table #TempTable (accountid int, productname char(2))
insert into #TempTable (accountid,productname) values
(1,'GL'),
(1,'IP'),
(1,'MI'),
(2,'GL'),
(2,'IP'),
(2,'MA')
select distinct
t1.accountid,
1 as T
into #TempTableTwo
from
#TempTable t1
where
productname in ('GL','IP')
union all
select distinct
t1.accountid,
-1 as T
from
#TempTable t1
where
productname in ('MI')
select
accountid
from #TempTableTwo
group by accountid
having sum(T) > 0
I might be late for the game, but this should do the trick, if anyone is trying to solve a similar problem. I renamed your table and it's columns:
Filteredccx_leadresearch -> l_search
ccx_accountidname -> a_name
ccx_productname -> p_name
And here's the SQL:
(SELECT DISTINCT t1.a_name
FROM l_search t1
JOIN l_search t2 ON t1.a_name = t2.a_name
WHERE t1.p_name = 'IP'
OR t2.p_name = 'GL')
MINUS
(SELECT DISTINCT t1.a_name
FROM l_search t1
JOIN l_search t2 ON t1.a_name = t2.a_name
WHERE ((t1.p_name = 'IP'OR t1.p_name = 'GL') AND t2.p_name = 'MI')
OR
(t1.p_name = 'MI' AND (t1.p_name = 'IP' OR t1.p_name = 'GL')));
First set:
cross product of table on itself with same IDs, get account IDs which have a product 'IP' or 'GL'.
Second set:
cross product of table on itself with same IDs, get account IDs which have p_name ('IP' OR 'GL') on first cross property AND 'MI' on second.
Also, get those IDs, which have the same but the other way around: p_name 'MI' on first cross property AND ('IP' OR 'GL') on second.
And finally subtract the second from the first.
Here is a simple way to include the accounts that match either IP or GL and exclude those accounts if they have an record for MI without using a subquery.
This is assuming t1 is a table that has unique account numbers in accountid and t2 is the table you have shown above that has accountid and Productname columns.
SELECT DISTINCT
t1.accountid
FROM t1
LEFT JOIN t2 AS t2_match
ON t1.accountid = t2_match.accountid
AND
(
t2_match.Productname = 'IP'
OR t2_match.Productname = 'GL'
)
LEFT JOIN t2 AS t2_not_match
ON t1.accountid = t2_not_match.accountid
AND t2_not_match.Productname = 'MI'
WHERE
t2_match.accountid IS NOT NULL
AND t2_not_match.accountid IS NULL
This is really late, but it might help some one.
I'll focus only on using the columns we have on the table we are shown (won't combine it with other tables we were not given).
Since the only table in the example is not clearly named, I'll call it some_table
SELECT t.accountidname, t.productname
FROM some_table t
WHERE t.productname IN ('GL','IP')
AND t.accountidname NOT IN (
SELECT accountidname
FROM some_table
WHERE productname = 'MI'
);
The idea here is to:
Select all accountid and productname that have productname either GL or IP (3rd line)
Select all accountid that have a productname MI and remove them from the values we already have (4th line onwards)
With this values, filtering or combining it with other tables should be rather trivial.
You might want to replace the SELECT with SELECT DISTINCT if the combinations of accountid and productname could be repeated in the table.
I have a football database with 10k+ rows. My goal is to select one player by his id and get all other players which are in the same league or club or have the same nationality.
My query so far is the following:
SELECT *
FROM `characteristics`
WHERE `nation` = (SELECT `nation`
FROM `characteristics`
WHERE `id` = "1")
OR `club` = (SELECT `club`
FROM `characteristics`
WHERE `id` = "1")
OR `league` = (SELECT `league`
FROM `characteristics`
WHERE `id` = "1")
I think the query doesn't work as I expected, because it will return 1434 rows. For example: Nation = Argentina, League = BBVA, Club = FC Barcelona will return together something about 1500+ rows.
Maybe you guys can help me with this subquery.
Another problem is the subquery syntax in ZF2. I found something here on stackoverflow, but I can't figure the syntax out where to put something.
EDIT: example of db data
id firstname lastname age height nation club league
1 Lionel Messi 28 170 cm Argentina FC Barcelona Liga BBVA
I can't answer regarding ZF2 as I am not working with this.
For your SQL question, I think you are looking for this :
SELECT C1.`id` as playerid,
C1.firstname as playerfirstname,
C1.lastname as playerlastname,
C2.*
FROM `characteristics` C1
INNER JOIN `characteristics` C2
ON ( C1.`nation` = C2.`nation` OR
C1.`club` = C2.`club` OR
C1.`league` = C2.`league` )
WHERE C1.`id` = "1"
The idea is to retrieve the player from characteristics (alias C1) and then to JOIN the table characteristics again (alias C2) on the columns you specified.
I'm bogged in trying to figure out why query a is returning different records than query b. Both queries have seemingly same purpose yet a is returning 500 and b 3500.
this is query a:
SELECT DISTINCT ODE.OrderBillToID
FROM APTIFY.dbo.vwVwOrderDetailsKGExtended ODE
WHERE ProductID IN (2022, 1393)
AND LTRIM(RTRIM(ODE.OrderStatus)) <> 'Cancelled'
AND LTRIM(RTRIM(ODE.OrderType)) <> 'Cancellation'
AND LTRIM(RTRIM(ODE.cancellationStatus)) <> 'FULLY CANCELLED'
UNION
SELECT DISTINCT ID
FROM APTIFY.dbo.vwPersons WHERE City = 'A'
UNION
SELECT DISTINCT RecordID
FROM APTIFY.dbo.vwTopicCodeLinks WHERE TopicCodeID = 16 AND Value = 'Yes, Please'
query b:
SELECT
APTIFY..vwPersons.ID
FROM
APTIFY..vwPersons
WHERE
( APTIFY..vwPersons.ID IN (
SELECT
vwMeetingRegistrants.ID
FROM
APTIFY.dbo.vwMeetings vwMeetings
INNER JOIN APTIFY.dbo.vwMeetingRegistrants vwMeetingRegistrants
ON vwMeetings.ID=vwMeetingRegistrants.ActualMeetingID WHERE
vwMeetings.ProductID = 2022
)
OR
APTIFY..vwPersons.ID IN (
SELECT
vwMeetingRegistrants.ID
FROM
APTIFY.dbo.vwMeetings vwMeetings
INNER JOIN APTIFY.dbo.vwMeetingRegistrants vwMeetingRegistrants
ON vwMeetings.ID=vwMeetingRegistrants.ActualMeetingID WHERE
vwMeetings.ProductID = 1393
)
OR
APTIFY..vwPersons.City = N'Albany' )
OR
((
APTIFY..vwPersons.ID IN (
SELECT
RecordID
FROM
APTIFY.dbo.vwTopicCodeLinks vwTopicCodeLinks
WHERE
vwTopicCodeLinks.TopicCodeID = 16
)
AND
APTIFY..vwPersons.ID IN (
SELECT
RecordID
FROM
APTIFY.dbo.vwTopicCodeLinks vwTopicCodeLinks
WHERE
vwTopicCodeLinks.Value = N'Yes, Please'
) )
)
vwMeetingsRegistrants from the b query are producing the same records as orderkgdetailsextended from query. I cannot see ANY difference in those queries - which perhaps shows my lack of understanding the query behaviour.
BIG Thanks for any points guys! :)
As it came out, incorrectly structured query is a result of badly configured application, Aptify.
Have tables: person,person_ip
Both tables have pid column as a primary key, in table person there is column state_id, in table person_ip there is column ip.
Want to discover if specified IP address is assigned to person with state_id is not equal to 2. But always got result 1, even if state_id is 0, 1 or 2. Always got 0 only if ip address is not listed at all. What am I doing wrong?
SELECT EXISTS (
SELECT person_ip.PID
FROM person_ip,person
WHERE person.PID=person_ip.PID
AND person.state_id NOT IN (2)
AND person_ip.ip='10.11.12.13'
)
this seems like a simple join.. unless i'm missing something
select person.*
from person
inner join person_ip
on person.pid = person_ip.pid
where person.state_id <> 2
and person_ip.ip_address = '10.0.0.1'
If you want to exclude the ip_address if it has been assigned to any user with state = 2, even if it has also been assigned to a user without state = 2, then try:
select max(i)
from (
select *
from (
select 1 as i
from dual
where not exists (
select 1
from person p
inner join person_ip pi
on p.pid = pi.pid
where p.state_id = 2
and pi.ip_address = '10.0.0.1'
)
) q
union
select 0
) qq
(dual is a system table that can be used as a sort of stub table)
here's a fiddle showing both versions
update after some actual sleep
Okay, so the above query is a little.. out there. Back in the real world, this one is probably more appropriate:
select count(case when p1.state_id = 2 then 1 end)
from person p1
inner join person_ip pi1
on p1.pid = pi1.pid
where pi1.ip_address = '10.0.0.1'
group by pi1.ip_address;
This will return 1 or more if your ip_address has been used by someone with a state_id of 2, and 0 if it has never been used by someone with a state_id of 2.
It will return nothing if the ip has never been used.
this fiddle has all three of the above queries.
SELECT IF(COUNT(*)>0,1,0)
FROM person
INNER JOIN person_ip
ON person.pid = person_ip.pid
AND person_ip.ip_address = '10.0.0.1'
WHERE person.state_id <> 2