I have following SQL:
SELECT
M.*
FROM
(
SELECT MAX(counter) AS FirstUserDate, imdb_id, language, season, aufloesung, episode
FROM autofehlerserie
GROUP BY imdb_id, language
) foo
JOIN
autofehlerserie M ON foo.imdb_id = M.imdb_id AND foo.language = M.language
ORDER BY
foo.FirstUserDate DESC, M.imdb_id, M.aufloesung, cast(M.season as int), cast(M.episode as int)
But I want only SELECT items WHERE marker not = "D"
Where I have to add this on this select query?
Use it like below :
SELECT
M.*
FROM
(
SELECT MAX(counter) AS FirstUserDate, imdb_id, language, season, aufloesung, episode
FROM autofehlerserie
WHERE marker <> 'D'
GROUP BY imdb_id, language, season, aufloesung, episode
) foo
JOIN
autofehlerserie M ON foo.imdb_id = M.imdb_id AND foo.language = M.language and M.marker <> 'D'
ORDER BY
foo.FirstUserDate DESC, M.imdb_id, M.aufloesung, cast(M.season as int), cast(M.episode as int)
I think you might actually want:
select m.*
from autofehlerserie m
where not exists (select 1
from autofehlerserie m2
where m2.imdb_id = m.imdb_id AND m2.language = m.language and
m2.marker = 'D'
);
This returns all rows in your table where there is no other row with the same imdb_id and language whose marker is 'D'.
Related
I have written the code shown below and I got the expected output. but,need the same output without use the same tables in inline view (such as tables join for UserPhoneDetail_JSON). thanks in advance
Code:
BEGIN
DROP TABLE #USERMASTER;
DROP TABLE #USERPHONE;
CREATE TABLE #USERMASTER (ID INT, NAME VARCHAR(100));
CREATE TABLE #USERPHONE (ID INT, PHONENUMBER NUMERIC,PHONETYPE CHAR(1));
INSERT INTO #USERMASTER VALUES(1,'JOHN');
INSERT INTO #USERMASTER VALUES(2,'VICTOR');
INSERT INTO #USERPHONE VALUES(1,1356487965,'W');
INSERT INTO #USERPHONE VALUES(1,9841007493,'M');
INSERT INTO #USERPHONE VALUES(1,7255952105,'O');
INSERT INTO #USERPHONE VALUES(2,9874563212,'M');
WITH E AS (SELECT A.ID,A.NAME,B.PHONENUMBER,B.PHONETYPE,ROW_NUMBER() OVER(PARTITION BY A.ID ORDER BY A.ID) RN
FROM #USERMASTER A JOIN #USERPHONE B ON A.ID=B.ID)
SELECT E.ID,
E.NAME,
E.PHONENUMBER,
E.PHONETYPE,
UserPhoneDetail_JSON = (
SELECT A.ID,B.PHONETYPE,PHONENUMBER,A.NAME FROM #USERMASTER A JOIN #USERPHONE B ON A.ID=B.ID
FOR JSON PATH )
FROM E WHERE RN=1;
END
output:
1 JOHN 1356487965 W [{"ID":1,"PHONETYPE":"W","PHONENUMBER":1356487965,"NAME":"JOHN"},{"ID":1,"PHONETYPE":"M","PHONENUMBER":9841007493,"NAME":"JOHN"},{"ID":1,"PHONETYPE":"O","PHONENUMBER":7255952105,"NAME":"JOHN"},{"ID":2,"PHONETYPE":"M","PHONENUMBER":9874563212,"NAME":"VICTOR"}]
2 VICTOR 9874563212 M [{"ID":1,"PHONETYPE":"W","PHONENUMBER":1356487965,"NAME":"JOHN"},{"ID":1,"PHONETYPE":"M","PHONENUMBER":9841007493,"NAME":"JOHN"},{"ID":1,"PHONETYPE":"O","PHONENUMBER":7255952105,"NAME":"JOHN"},{"ID":2,"PHONETYPE":"M","PHONENUMBER":9874563212,"NAME":"VICTOR"}]
EXPECTED OUTPUT:
1 JOHN 1356487965 W [{"ID":1,"PHONETYPE":"W","PHONENUMBER":1356487965,"NAME":"JOHN"},{"ID":1,"PHONETYPE":"M","PHONENUMBER":9841007493,"NAME":"JOHN"},{"ID":1,"PHONETYPE":"O","PHONENUMBER":7255952105,"NAME":"JOHN"}]
2 VICTOR 9874563212 M [{"ID":2,"PHONETYPE":"M","PHONENUMBER":9874563212,"NAME":"VICTOR"}]
If I understand you correctly and you need to generate JSON content for each first row per ID, this statement is an option:
SELECT
t.ID, t.NAME, t.PHONENUMBER, t.PHONETYPE,
UserPhoneDetail_JSON = (
SELECT t.ID, PHONETYPE, PHONENUMBER, t.NAME
FROM #USERPHONE
WHERE ID = t.ID
FOR JSON PATH
)
FROM (
SELECT
m.ID, m.NAME, p.PHONENUMBER, p.PHONETYPE,
ROW_NUMBER() OVER (PARTITION BY m.ID ORDER BY p.ID) RN
FROM #USERMASTER m
JOIN #USERPHONE p ON m.ID = p.ID
) t
WHERE t.RN = 1
I have the following sql query:
SELECT v.venue_id, s.zip, COUNT( * )
FROM bcs_scans s
JOIN bcs_scanners sc ON s.uuid = sc.uuid
JOIN bcs_venues v ON sc.venue_id = v.venue_id
WHERE v.banlist_id = '625'
AND s.del =0
GROUP BY s.zip
ORDER BY COUNT( * ) DESC
Which returns the count of individual zip codes, their count, and associated venue.
How do I go about selecting the top 5 zip codes for each unique venue id?
I believe I can run a subquery that groups results by venue id with the top 5 zip counts, but I am unsure of where to start
Could be you select the result in this way ... a bit complex ..
using the having for extract the value that match the max count group by venue_id from your original query ..
SELECT v.venue_id as venue_id, s.zip as , COUNT( * ) as num
FROM bcs_scans s
JOIN bcs_scanners sc ON s.uuid = sc.uuid
JOIN bcs_venues v ON sc.venue_id = v.venue_id
WHERE v.banlist_id = '625'
AND s.del =0
GROUP BY s.zip
HAVING ( v.venue_id, COUNT( * )) in
(select venue_id, max(num)
from
(SELECT v.venue_id as venue_id, s.zip as , COUNT( * ) as num
FROM bcs_scans s
JOIN bcs_scanners sc ON s.uuid = sc.uuid
JOIN bcs_venues v ON sc.venue_id = v.venue_id
WHERE v.banlist_id = '625'
AND s.del =0
GROUP BY s.zip
ORDER BY COUNT( * ) DESC ) a t
group by venue_id)
ORDER BY COUNT( * ) limit 5
I have a staging table with new address records and need to copy new cities into a mart table. I want to only have one entry in the mart for each city, state, zip combination, and I want to include latitude and longitude for the city. The address table has lat & long, but they could be anywhere within the city, or could be null.
The query I've got so far gets me the right data, but it's pulling one pair of lat & long arbitrarily. I'd prefer to pull from the ones that are not null.
SELECT a.city
,a.STATE
,a.country
,a.latitude
,a.longitude
FROM (
SELECT city
,STATE
,country
FROM staging2.address_daily s
WHERE NOT EXISTS (
SELECT *
FROM mart.city m
WHERE m.city_name = s.city
AND m.state_code = s.STATE
AND m.country_code = s.country
)
GROUP BY city
,STATE
,country
) sq --This subquery groups by city state and country
JOIN staging2.address_daily a
ON a.ID = (
SELECT ID
FROM staging2.address_daily i
WHERE i.city = sq.city
AND i.STATE = sq.STATE
AND i.country = sq.country LIMIT 1
) --This subquery takes the group, and picks one ID.
--The overall query is still flawed, as we're picking at random, and we should ideally pick a non-null latitude and longitude if they exist.
I'm using MySQL but would prefer to avoid things that are unique to MySQL.
From a SQL perspective this logic would work. For MySQL you might need to do some tweaks to the syntax -
SELECT sq.city
,sq.STATE
,sq.country
,a.latitude
,a.longitude
FROM (
SELECT city
,STATE
,country
FROM staging2.address_daily s
WHERE NOT EXISTS (
SELECT *
FROM mart.city m
WHERE m.city_name = s.city
AND m.state_code = s.STATE
AND m.country_code = s.country
)
GROUP BY city
,STATE
,country
) sq --This subquery groups by city state and country
LEFT JOIN staging2.address_daily a
ON a.ID = (
SELECT ID
FROM staging2.address_daily i
WHERE i.city = sq.city
AND i.STATE = sq.STATE
AND i.country = sq.country
AND NOT latitude IS NULL LIMIT 1
)
can you try this to see if it works for you
SELECT *
FROM (SELECT city,
STATE,
country,
A.latitude,
A.longitude,
ROW_NUMBER ()
OVER (PARTITION BY city, state, country
ORDER BY longitude, latitude DESC)
AS ROWNUM
FROM staging2.address_daily s
WHERE NOT EXISTS
(SELECT *
FROM mart.city M
WHERE M.city_name = s.city
AND M.state_code = s.STATE
AND M.country_code = s.country))
WHERE ROWNUM = 1
I am trying to implement the following code into my own.
Here is the code I'm trying to implement. It's for getting Ranks with ties and the rank is non consecutive after the ties.
SET #rnk=0;
SET #rank=0;
SET #curscore=0;
SELECT score,ID,rank FROM (
SELECT AA.*,BB.ID,
(#rnk:=#rnk+1) rnk,
(#rank:=IF(#curscore=score,#rank,#rnk)) rank,
(#curscore:=score) newscore
FROM (
SELECT * FROM (
SELECT COUNT(1) scorecount,score
FROM scores GROUP BY score
) AAA
ORDER BY score DESC
) AA LEFT JOIN scores BB USING (score)
) A;
Here is the link to where I got the code from. It's the second answer.
https://dba.stackexchange.com/questions/13703/get-the-rank-of-a-user-in-a-score-table
My code is below. I swapped "score" with "totalPoints". This is as far as I got.
I am having trouble with two parts of the code.
This line, the BB.ID part.
SELECT AA.*, BB.ID,
And this line.
) AA LEFT JOIN scores BB USING (score)
I know what the code is doing I just don't know how to replace those parts with my code and tables to get them to work.
SET #rnk=0;
SET #rank=0;
SET #curscore=0;
SELECT AA.*, BB.ID,
(#rnk:=#rnk+1) rnk,
(#rank:=IF(#curscore=totalPoints,#rank,#rnk)) rank,
(#curscore:=totalPoints) newPoints
FROM (
SELECT * FROM (
SELECT COUNT(1) scorecount, totalPoints FROM (
#----------start of my code
SELECT *, (totalWins+(totalPushs*.5)) AS totalPoints, totalWins+totalLost+totalPushs AS totalBets FROM (
SELECT *, SUM(win) AS totalWins, SUM(lost) AS totalLost, SUM(push) AS totalPushs FROM (
SELECT *, (finalResult = 'Winner') AS win, (finalResult = 'Loser') AS lost, (finalResult = 'Push') AS push FROM (
SELECT nflp_users.userID, userName, avatar, teamColor,
IF (pickID=visitorID, visitorResult, homeResult) AS finalResult
FROM nflp_users
JOIN nflp_picks
ON nflp_users.userID = nflp_picks.userID
JOIN nflp_schedule
ON nflp_picks.gameID = nflp_schedule.gameID
) x
) x
GROUP BY userID
) x
#----------end of my code
) x GROUP BY totalPoints
) AAA
ORDER BY totalPoints DESC
) AA LEFT JOIN scores BB USING (score)
I have the following query:
SELECT SUM( cost )
FROM (
SELECT s.cost
FROM sandwiches AS s
WHERE s.name = "Cheese Steak"
)
UNION (
SELECT p.cost
FROM pizza AS p
WHERE TYPE = "Plain"
AND SIZE = "L"
)
That gives me an error of:
#1248 - Every derived table must have its own alias
You need to alias your temp tables
SELECT SUM( cost )
FROM
(
(
SELECT s.cost
FROM sandwiches AS s
WHERE s.name = "Cheese Steak"
) AS T1
UNION
(
SELECT p.cost
FROM pizza AS p
WHERE TYPE = "Plain"
AND SIZE = "L"
) AS T2
) AS T
Do you want the whole Sum?
SELECT
( SELECT SUM(s.cost)
FROM sandwiches AS s
WHERE s.name = "Cheese Steak"
)
+
( SELECT SUM(p.cost)
FROM pizza AS p
WHERE p.TYPE = "Plain"
AND p.SIZE = "L"
)
The following form should do the job:
SELECT SUM(cost) FROM (
SELECT cost FROM sandwiches WHERE name = "Cheese Steak"
UNION
SELECT cost FROM pizza WHERE TYPE = "Plain" AND SIZE = "L"
) as temp
MySQL only requires a temporary table name for the subselect.