SQL getting only one max id field on a LEFT OUTER JOIN - sql

how to compare the value of the user table with only one maximum value of the notifications of the table?
SELECT "vkId" AS "id"
FROM "user" AS "user"
LEFT OUTER JOIN "notification" ON "notification"."userId" = "user"."vkId"
WHERE (
"user"."vipEnd" < 1469714507
AND "user"."vipEnd" > 1469710907
) AND (
(
"notification"."type" = 4
AND
"notification"."expiresVipDate" < 1469710907
)
OR "notification"."id" IS NULL
)
LIMIT '100';

You may use DISTINCT ON for that, sorting by expiresVipDate DESC:
SELECT DISTINCT ON (u."vkId") u."vkId" AS id, n.*
FROM "user" AS u
LEFT JOIN notification n ON (n."userId" = u."vkId" AND
n.type = 4 AND
n."expiresVipDate" < 1469710907)
WHERE u."vipEnd" < 1469714507 AND u."vipEnd" > 1469710907
ORDER BY u."vkId", n."expiresVipDate" DESC
LIMIT 100;

Related

SQL > Server > How to query for additional fields

I have the following query and it works perfectly and gives me 200 rows. However, I wanted to retrieve additional fields from ExecutionLogStorage table. When I added ExecutionLogStorage.TimeStart, ExecutionLogStorage.TimeDataRetrieval with group by the result is 8,000+ rows. How can I retrieve the latest date (Max of the date) and still keep 200 rows of data.
Select * from (
SELECT ExecutionLogStorage.ReportID, COUNT(*) AS HitCount, Catalog.Name, ExecutionLogStorage.UserName
FROM [SP_RPT_SVC].[dbo].ExecutionLogStorage INNER JOIN
Catalog ON [SP_RPT_SVC].[dbo].ExecutionLogStorage.ReportID = Catalog.ItemID
where Catalog.[Type] = 2
GROUP BY ExecutionLogStorage.ReportID, Catalog.Name, ExecutionLogStorage.UserName) X
LEFT Join
(SELECT [Id]
,[DirName]
,[LeafName]
FROM [SP_BI].[dbo].[AllDocs]) Y
on
Y.ID = X.ReportID
LEFT Join
(SELECT [NTName],[PreferredName]
FROM [SP_ProfileDB].[dbo].[UserProfile_Full]) Z
ON
X.UserName = Z.NTName
How can I retrieve the latest date (Max of the date) and still keep 200 rows of data.
No need to modify the GROUP BY clause: just add more aggregate functions to your inner query:
SELECT *
FROM (
SELECT
ExecutionLogStorage.ReportID,
COUNT(*) AS HitCount,
Catalog.Name,
ExecutionLogStorage.UserName,
MAX(ExecutionLogStorage.TimeStart) MaxTimeStart --> here,
MAX(ExecutionLogStorage.TimeDataRetrieval) MaxTimeDataRetrieval --> and here
FROM [SP_RPT_SVC].[dbo].ExecutionLogStorage
INNER JOIN Catalog
ON [SP_RPT_SVC].[dbo].ExecutionLogStorage.ReportID = Catalog.ItemID
WHERE Catalog.[Type] = 2
GROUP BY
ExecutionLogStorage.ReportID,
Catalog.Name,
ExecutionLogStorage.UserName
) X
LEFT JOIN ...

Limit join to one row

I have the following query:
SELECT sum((select count(*) as itemCount) * "SalesOrderItems"."price") as amount, 'rma' as
"creditType", "Clients"."company" as "client", "Clients".id as "ClientId", "Rmas".*
FROM "Rmas" JOIN "EsnsRmas" on("EsnsRmas"."RmaId" = "Rmas"."id")
JOIN "Esns" on ("Esns".id = "EsnsRmas"."EsnId")
JOIN "EsnsSalesOrderItems" on("EsnsSalesOrderItems"."EsnId" = "Esns"."id" )
JOIN "SalesOrderItems" on("SalesOrderItems"."id" = "EsnsSalesOrderItems"."SalesOrderItemId")
JOIN "Clients" on("Clients"."id" = "Rmas"."ClientId" )
WHERE "Rmas"."credited"=false AND "Rmas"."verifyStatus" IS NOT null
GROUP BY "Clients".id, "Rmas".id;
The problem is that the table "EsnsSalesOrderItems" can have the same EsnId in different entries. I want to restrict the query to only pull the last entry in "EsnsSalesOrderItems" that has the same "EsnId".
By "last" entry I mean the following:
The one that appears last in the table "EsnsSalesOrderItems". So for example if "EsnsSalesOrderItems" has two entries with "EsnId" = 6 and "createdAt" = '2012-06-19' and '2012-07-19' respectively it should only give me the entry from '2012-07-19'.
SELECT (count(*) * sum(s."price")) AS amount
, 'rma' AS "creditType"
, c."company" AS "client"
, c.id AS "ClientId"
, r.*
FROM "Rmas" r
JOIN "EsnsRmas" er ON er."RmaId" = r."id"
JOIN "Esns" e ON e.id = er."EsnId"
JOIN (
SELECT DISTINCT ON ("EsnId") *
FROM "EsnsSalesOrderItems"
ORDER BY "EsnId", "createdAt" DESC
) es ON es."EsnId" = e."id"
JOIN "SalesOrderItems" s ON s."id" = es."SalesOrderItemId"
JOIN "Clients" c ON c."id" = r."ClientId"
WHERE r."credited" = FALSE
AND r."verifyStatus" IS NOT NULL
GROUP BY c.id, r.id;
Your query in the question has an illegal aggregate over another aggregate:
sum((select count(*) as itemCount) * "SalesOrderItems"."price") as amount
Simplified and converted to legal syntax:
(count(*) * sum(s."price")) AS amount
But do you really want to multiply with the count per group?
I retrieve the the single row per group in "EsnsSalesOrderItems" with DISTINCT ON. Detailed explanation:
Select first row in each GROUP BY group?
I also added table aliases and formatting to make the query easier to parse for human eyes. If you could avoid camel case you could get rid of all the double quotes clouding the view.
Something like:
join (
select "EsnId",
row_number() over (partition by "EsnId" order by "createdAt" desc) as rn
from "EsnsSalesOrderItems"
) t ON t."EsnId" = "Esns"."id" and rn = 1
this will select the latest "EsnId" from "EsnsSalesOrderItems" based on the column creation_date. As you didn't post the structure of your tables, I had to "invent" a column name. You can use any column that allows you to define an order on the rows that suits you.
But remember the concept of the "last row" is only valid if you specifiy an order or the rows. A table as such is not ordered, nor is the result of a query unless you specify an order by
Necromancing because the answers are outdated.
Take advantage of the LATERAL keyword introduced in PG 9.3
left | right | inner JOIN LATERAL
I'll explain with an example:
Assuming you have a table "Contacts".
Now contacts have organisational units.
They can have one OU at a point in time, but N OUs at N points in time.
Now, if you have to query contacts and OU in a time period (not a reporting date, but a date range), you could N-fold increase the record count if you just did a left join.
So, to display the OU, you need to just join the first OU for each contact (where what shall be first is an arbitrary criterion - when taking the last value, for example, that is just another way of saying the first value when sorted by descending date order).
In SQL-server, you would use cross-apply (or rather OUTER APPLY since we need a left join), which will invoke a table-valued function on each row it has to join.
SELECT * FROM T_Contacts
--LEFT JOIN T_MAP_Contacts_Ref_OrganisationalUnit ON MAP_CTCOU_CT_UID = T_Contacts.CT_UID AND MAP_CTCOU_SoftDeleteStatus = 1
--WHERE T_MAP_Contacts_Ref_OrganisationalUnit.MAP_CTCOU_UID IS NULL -- 989
-- CROSS APPLY -- = INNER JOIN
OUTER APPLY -- = LEFT JOIN
(
SELECT TOP 1
--MAP_CTCOU_UID
MAP_CTCOU_CT_UID
,MAP_CTCOU_COU_UID
,MAP_CTCOU_DateFrom
,MAP_CTCOU_DateTo
FROM T_MAP_Contacts_Ref_OrganisationalUnit
WHERE MAP_CTCOU_SoftDeleteStatus = 1
AND MAP_CTCOU_CT_UID = T_Contacts.CT_UID
/*
AND
(
(#in_DateFrom <= T_MAP_Contacts_Ref_OrganisationalUnit.MAP_KTKOE_DateTo)
AND
(#in_DateTo >= T_MAP_Contacts_Ref_OrganisationalUnit.MAP_KTKOE_DateFrom)
)
*/
ORDER BY MAP_CTCOU_DateFrom
) AS FirstOE
In PostgreSQL, starting from version 9.3, you can do that, too - just use the LATERAL keyword to achieve the same:
SELECT * FROM T_Contacts
--LEFT JOIN T_MAP_Contacts_Ref_OrganisationalUnit ON MAP_CTCOU_CT_UID = T_Contacts.CT_UID AND MAP_CTCOU_SoftDeleteStatus = 1
--WHERE T_MAP_Contacts_Ref_OrganisationalUnit.MAP_CTCOU_UID IS NULL -- 989
LEFT JOIN LATERAL
(
SELECT
--MAP_CTCOU_UID
MAP_CTCOU_CT_UID
,MAP_CTCOU_COU_UID
,MAP_CTCOU_DateFrom
,MAP_CTCOU_DateTo
FROM T_MAP_Contacts_Ref_OrganisationalUnit
WHERE MAP_CTCOU_SoftDeleteStatus = 1
AND MAP_CTCOU_CT_UID = T_Contacts.CT_UID
/*
AND
(
(__in_DateFrom <= T_MAP_Contacts_Ref_OrganisationalUnit.MAP_KTKOE_DateTo)
AND
(__in_DateTo >= T_MAP_Contacts_Ref_OrganisationalUnit.MAP_KTKOE_DateFrom)
)
*/
ORDER BY MAP_CTCOU_DateFrom
LIMIT 1
) AS FirstOE
Try using a subquery in your ON clause. An abstract example:
SELECT
*
FROM table1
JOIN table2 ON table2.id = (
SELECT id FROM table2 WHERE table2.table1_id = table1.id LIMIT 1
)
WHERE
...

SELECT Data from multiple tables?

I have 3 tables, with 3 fields all the same. I basically want to select information from each table
For example:
userid = 1
I want to select data from all 3 tables, where userid = 1
I am currently using:
SELECT r.*,
p.*,
l.*
FROM random r
LEFT JOIN pandom p ON r.userid = p.userid
LEFT JOIN landom l ON l.userid = r.userid
WHERE r.userid = '1'
LIMIT 0, 30
But it doesn't seem to work.
with 3 fields all the same
So you mean that you want the same 3 fields from all 3 tables?
SELECT r.col1, r.col2, r.col3
FROM random r
WHERE r.userid = '1'
LIMIT 0, 30
UNION ALL
SELECT p.pcol1, p.pcol_2, p.p3
FROM pandom p
WHERE p.userid = '1'
LIMIT 0, 30
UNION ALL
SELECT l.l1, l.l2, l.l3
FROM landom l
WHERE l.userid = '1'
LIMIT 0, 30
The fields don't have to be named the same, but the same types need to line up in position 1, 2 and 3.
The way the limits work is:
it will attempt to get 30 from random.
If it has 30 already, it won't even look at the other 2 tables
if it has less than 30 from random, it will try to fill up to 30 from pandom and only finally landom
SELECT t1.*, t2.*, t3.*
FROM `random` as t1, `pandom` as t2, `landom` as t3
WHERE t1.`userid`='1' AND t2.`userid`='1' AND t3.`userid`='1'
SELECT * FROM `random`
JOIN `pandom` USING (`userid`)
JOIN `landom` USING (`userid`)
WHERE `userid`='1'

Query not working as expected

Goal
Select distinct ids from blog_news where
active = 1
title is not empty
has at least one picture unless picture is logo, or at least one video
The statement so far
select distinct n.id from blog_news n
left join blog_pics p ON n.id = p.blogid and active = '1' and trim(n.title) != ''
left join blog_vdos v ON n.id = v.blogid
where (p.islogo = '0' and p.id is not null) OR (v.id is not null)
order by `newsdate` desc, `createdate` desc
The issue
selects blog_news ids that have pictures, unless they're logos [correct]
selects blog_news ids that have both videos and pictures [correct]
does not select blog_news ids that have only videos [wrong]
How about this:
SELECT DISTINCT n.id
FROM blog_news n
WHERE n.active = '1'
AND trim(n.title) != ''
AND (EXISTS (SELECT 1
FROM blog_pics p
WHERE p.blogid = n.id
AND p.islogo = 0)
OR EXISTS (SELECT 1
FROM blog_vdos v
WHERE v.blogid = n.id)
)
ORDER BY n.newsdate desc, n.createdate desc
Where you are just interested in the existence (or not) of child rows then it is often clearer and easier to use EXISTS.
I can't see any problem in your query.
I expect active is column in blog_news table, you should call it n.active. If this column is in blog_pics table, then this is the problem.
I would add the condition (n.active, n.title) to WHERE, as it's not related to left join (blog_pics) - but that's just for better readability, the result would be the same.
You can write the query using sub selects as well:
SELECT n.id FROM blog_news n
WHERE n.active = 1 AND TRIM(n.title) != '' AND n.id IN (
SELECT DISTINCT p.blogid FROM blog_pics p WHERE p.islogo = 0 UNION
SELECT DISTINCT v.blogid FROM blog_vdos
);

Querying for Consecutive Rows with Certain Characteristics

I've got a table with the following columns:
id int(10)
user int(10)
winner int(10)
profit double
created datetime
The winner column can be either 0 or 1. I'd like to create a query that returns the maximum number of consecutive winners as ordered by the created datetime column along with the first and last created date as well as the sum of the profit column from that period of consecutive winners.
Here's a possible solution that looks at winning streaks per userid.
select head.userid, head.id, sum(profit), count(*)
from #bingo b
inner join (
select cur.userid, cur.id
from #bingo cur
left join #bingo prev
on cur.userid = prev.userid
and prev.id < cur.id
and not exists(
select *
from #bingo inbetween
where prev.userid = inbetween.userid
and prev.id < inbetween.id
and inbetween.id < cur.id)
where cur.winner = 1
and IsNull(prev.winner,0) = 0
) head
on head.userid = b.userid
and head.id <= b.id
left join (
select cur.userid, cur.id
from #bingo cur
left join #bingo prev
on cur.userid = prev.userid
and prev.id < cur.id
and not exists(
select *
from #bingo inbetween
where prev.userid = inbetween.userid
and prev.id < inbetween.id
and inbetween.id < cur.id)
where cur.winner = 1
and IsNull(prev.winner,0) = 0
) nexthead
on nexthead.userid = b.userid
and head.id < nexthead.id
and nexthead.id <= b.id
where nexthead.id is null
and b.winner = 1
group by head.userid, head.id
The two "heads" subqueries are identical, you could put them in a view or a WITH where those are supported. The "heads" subquery searches for each head of a winning streak; that is, the first win or a win that's preceeded by a loss. I'm assuming your id's increase over time, so I'm not using the Created column.
The query below that searches the corresponding head for every row. A head's id must be smaller or equal to the current row's id, and there must be no other head in between.
After that it's a simple matter of grouping on the head, and summing the profits and counting the rows.
I haven't tested it but maybe this will work.
select first_winner.created, last_winner.created, sum(mid_winner.profit)
from T first_winner
join T last_winner
on first_winner.created <= last_winner.created
and first_winner.winner = 1
and last_winner.winner = 1
and not exists -- no losers in between first_winner and last_winner
(
select * from T loser
where loser.winner = 0
and first_winner.created <= loser.created
and loser.created <= last_winner.created
)
join T mid_winner
on first_winner.created <= mid_winner.created
and mid_winner.created <= last_winner.created
and mid_winner.winner = 1
left join T bef_first_winner -- winner before first winner with no losers in between
on bef_first_winner.winner = 1
and bef_first_winner.created < first_winner.created
and not exists
(
select * from T b_loser
where b_loser.winner = 0
and bef_first_winner.created <= b_loser.created
and b_loser.created <= first_winner.created
)
left join T after_last_winner -- winner after last winner with no losers in between
on after_last_winner.winner = 1
and last_winner.created < after_last_winner.created
and not exists
(
select * from T a_loser
where a_loser.winner = 0
and last_winner.created <= a_loser.created
and a_loser.created <= after_last_winner.created
)
where bef_first_winner.id is null
and after_last_winner.id is null
group by first_winner.created, last_winner.created