How to index this couchbase query - couchbase

I am try to index this query but I don't how to do so
const query = `SELECT r.*, r.itemName as name, r.createdBy.timestamp AS modifiedOn
FROM report r
LET groupReportIds = (SELECT rg.reportGuid
FROM report rg
WHERE rg.eventType = 'GROUP_SHARING' AND LOWER(rg.userEmail) = LOWER("${email}"))
WHERE (ANY u in groupReportIds SATISFIES u.reportGuid = r.details.guid END)`;

CREATE INDEX ix1 ON report(LOWER(userEmail), reportGuid) WHERE eventType = "GROUP_SHARING";
CREATE INDEX ix2 ON report(details.guid);
WITH groupReportIds AS (SELECT RAW rg.reportGuid
FROM report rg
WHERE rg.eventType = "GROUP_SHARING"
AND LOWER(rg.userEmail) = LOWER("${email}"))
SELECT r.*, r.itemName as name, r.createdBy.timestamp AS modifiedOn
FROM report r
WHERE r.details.guid IN groupReportIds;

Related

SQL query to select a value based on certain criteria

I have the following data in my Database:
Id MachineName CategoryName CounterName InstanceName RawValue
11180 SERVER64 Process ID Process w3wp#2 2068
11180 SERVER64 Process Working Set w3wp#2 9310208
Now I want to achieve that if I find the value '2068' for the "ID Process" Countername then I want to retrieve the Working Set RawValue. So based on the value of ID Process I now the [InstanceName] = w3wp#2 and therefore I want the value to retrieve = 9310208
Now I tried different SQL queries:
SELECT *
FROM [dbo].[LoadTest]
WHERE [LoadTestRunId] = '11180' and [CategoryName] = 'Process' and [InstanceName] like 'w3wp%'
But I need a filter. Can anyone guide me into the right direction?
This here will help you. I used variable because you need to find a specific ID
SQL Code
declare #myt table (id int,MachineName nvarchar(50),CategoryName nvarchar(50),CounterName nvarchar(50),InstanceName nvarchar(50),RawValue int)
insert into #myt
values
(11180 ,'SERVER64','Process','ID Process','w3wp#2',2068),
(11180 ,'SERVER64','Process','Working Set','w3wp#2',9310208)
declare #FindID int
Set #FindID = 2068;
with IdProcess as (
Select * from #myt
where RawValue = #FindID and CounterName = 'ID Process'
)
Select a.ID,a.MachineName,a.CategoryName,b.CounterName,a.InstanceName,b.RawValue from IdProcess a
inner join #myt b on a.InstanceName = b.InstanceName and b.CounterName='Working Set'
SQL Code without variable based on ID and InstanceName
with IdProcess as (
Select * from #myt
where CounterName = 'ID Process'
)
Select a.ID,a.MachineName,a.CategoryName,b.CounterName,a.InstanceName,b.RawValue from IdProcess a
inner join #myt b on a.id = b.id and a.InstanceName = b.InstanceName and b.CounterName='Working Set'
SQL Code with CategoryName filter
with IdProcess as (
Select * from #myt
where CounterName = 'ID Process' and CategoryName = 'Process'
)
Select a.ID,a.MachineName,a.CategoryName,b.CounterName,a.InstanceName,b.RawValue from IdProcess a
inner join #myt b on a.id = b.id and a.InstanceName = b.InstanceName and b.CounterName='Working Set'
where b.CategoryName = 'Process'
Result
This will execute.
Select * from (SELECT ROW_NUMBER() OVER(PARTITION BY LoadTestRunId ORDER BY LoadTestRunId DESC) as row,*
FROM [dbo].[LoadTest]) t1 where row=1
with your where clause
Select * from (SELECT ROW_NUMBER() OVER(PARTITION BY LoadTestRunId ORDER BY LoadTestRunId DESC),*
FROM [dbo].[LoadTest]) t1 where t1=1
and [LoadTestRunId] = '11180' and [CategoryName] = 'Process' and [InstanceName] like 'w3wp%'

mysql select query , trying to select multiple values but the query shows 1 value

i have MYSQL database with WORDPRESS that have many tables and trying to do a select query in order to get the requested data.
tables are :
site_locaion:
siteID(PRIMARY KEY)
locationName
lat
long
employee:
employeeID(PRIMARY KEY)
employeeName
inspection_info:
inspectionID(PRIMARY KEY)
inspectionDate
employeeID
inspection_site:
inspectionID(PRIMARY KEY)
siteID(PRIMARY KEY)
i am trying to get the below fields where the employeeName has several values.
SQL qyery
$sql = $wpdb->prepare("select
l.locationName
, n.inspectionDate
, e.employeeName
from army_site_location l
LEFT
JOIN inspection_site s
on l.siteID = s.siteID
LEFT
JOIN inspection_info n
on n.inspectionID = s.inspectionID
LEFT
JOIN employeeName e
on n.employeeID=e.employeeID
where
l.locationName = %s
AND
e.employeeID = %d
AND
n.inspectionID = %d
",$site_name,$soldier_name, $inspection_date);
$query_submit =$wpdb->get_results($sql, OBJECT);
where the result must 2 employee in the same date and the same location but the system just fetch 1 employee

Is there a way to get results based on the results of a sub query?

I am trying to generate a list of projects that members of my website are connected to via tasks they are assigned to. So for example my MySQL query looks like:
$result = $con->query("SELECT ProjectID, ProjectName, StartDate, EndDate, Bio, AStartDate, AEndDate, POwnerID
FROM projects
WHERE ProjectID = (SELECT ProjectID
FROM tasks
INNER JOIN usertasks ON tasks.TaskID = usertasks.TaskID
WHERE UserID = '$User')
ORDER BY StartDate");
So the nested query is returning the two expected values (9,10), but the main query returns boolean false.
Is it possible to use MySQL query to gain the results I am looking for?
when your nested query return more than 1 value you must use in
WHERE ProjectID IN (SELECT ProjectID
to be:
$result = $con->query("SELECT ProjectID, ProjectName, StartDate, EndDate, Bio,
AStartDate, AEndDate, POwnerID FROM projects WHERE ProjectID
IN (SELECT ProjectID FROM tasks INNER JOIN usertasks ON
tasks.TaskID = usertasks.TaskID WHERE UserID = '$User')
ORDER BY StartDate");

How do I use a function in a view to return only distinct union of rows?

I am trying to do a view to reference on a webpage using RadGrid control. I need to get a subset of all records in my projects table by the last entered Changedate in the Changes table per unique projectid with the Status from the Status table along with the details of the project. The ProjectId & StatusID are in the Changes table as foreign Keys.
This is my view.
CREATE VIEW [dbo].[Project-by_View]
AS
SELECT TOP (100) PERCENT
dbo.Projects.Id,
dbo.Projects.ProjectName,
dbo.Department.Name,
dbo.Designer.FName + ' ' + dbo.Designer.LName AS Designer,
dbo.Changes.ChangeDate,
dbo.Projects.DueDate,
dbo.Projects.Instructions,
dbo.Status.Description
FROM dbo.Projects
INNER JOIN dbo.Department
ON dbo.Department.ID = dbo.Projects.DeptID
INNER JOIN dbo.Designer
ON dbo.Designer.Id = dbo.Projects.DesignerID
INNER JOIN dbo.Changes
ON dbo.Changes.ProjectID = dbo.Projects.ID
INNER JOIN dbo.Status
ON dbo.Changes.StatusID = dbo.Status.Id
WHERE EXIST (SELECT *
FROM GetAllLastChangeDatebyProjectIds(0))
ORDER BY dbo.Projects.ID DESC
GO
and my function
USE [FIDO_DEV]
GO
/****** Object: UserDefinedFunction [dbo].[GetAllLastChangeDatebyProjectIds]
Script Date: 9/14/2015 4:31:22 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Author, Geoffrey Stephens>
-- Create date: <Create Date, 09/14/20115>
-- Description: <Description, table of the Last Change Date for each Project>
-- =============================================
ALTER FUNCTION [dbo].[GetAllLastChangeDatebyProjectIds]
(
#ProjectID int
)
RETURNS TABLE
AS
RETURN
(
Select ProjectID, StatusID, ChangeDate from (
Select
ProjectID, StatusID, ChangeDate,
row_number() over(partition by ProjectID order by ChangeDate desc) as rn
from
Changes
) t
where t.rn = 1)
GO
The function works and the View work separately, but in conjunction I still get the full data set returned.
This chunk looks suspicious:
WHERE EXIST (SELECT *
FROM GetAllLastChangeDatebyProjectIds(0))
Should it not look more like:
WHERE EXIST (SELECT *
FROM GetAllLastChangeDatebyProjectIds(Projects.Id))
I haven't looked any deeper, that just jumped out at me.
Try something like this (reformatted to my preferred style)
SELECT Projects.Id,
dbo.Projects.ProjectName,
dbo.Department.Name,
dbo.Designer.FName + ' ' + dbo.Designer.LName AS Designer,
dbo.Changes.ChangeDate,
dbo.Projects.DueDate,
dbo.Projects.Instructions,
dbo.Status.Description
FROM ( SELECT ProjectID,
StatusID,
ChangeDate
FROM ( FROM ProjectID,
StatusID,
ChangeDate,
row_number() OVER (PARTITION BY ProjectID ORDER BY ChangeDate DESC) as rn
FROM Changes) t
WHERE t.rn = 1)) AS Selection
INNER JOIN dbo.Projects
ON dbo.Projects.ID = Selection.ProjectID
INNER JOIN dbo.Designer
ON dbo.Designer.Id = dbo.Projects.DesignerID
INNER JOIN dbo.Department
ON dbo.Department.ID = dbo.Projects.DeptID
INNER JOIN dbo.Changes
ON dbo.Changes.ProjectID = dbo.Projects.ID
INNER JOIN dbo.Status
ON dbo.Changes.StatusID = dbo.Status.Id
WHERE Selection.StatusID = dbo.Changes.StatusID
ORDER BY dbo.Projects.ID DESC
I reordered the Selects, so that I start with the Distinct project.ID and then do the joins. Then did the where on the Status.ID to get the latest distinct record set.
Select TOP 100 PERCENT Projects.Id, dbo.Projects.ProjectName, dbo.Department.Name, dbo.Designer.FName + ' ' + dbo.Designer.LName AS Designer, dbo.Changes.ChangeDate, dbo.Projects.DueDate, dbo.Projects.Instructions, dbo.Status.Description
From GetAllLastChangeDatebyProjectIds(0) INNER JOIN
dbo.Projects ON dbo.Projects.ID = dbo.GetAllLastChangeDatebyProjectIds.ProjectID INNER JOIN
dbo.Designer ON dbo.Designer.Id = dbo.Projects.DesignerID INNER JOIN
dbo.Department ON dbo.Department.ID = dbo.Projects.DeptID INNER JOIN
dbo.Changes ON dbo.Changes.ProjectID = dbo.Projects.ID INNER JOIN
dbo.Status ON dbo.Changes.StatusID = dbo.Status.Id
Where dbo.GetAllLastChangeDatebyProjectIds.StatusID = dbo.Changes.StatusID
ORDER BY dbo.Projects.ID DESC

Convert a Sql query with group by clause and/or aggregate function into linq to sql?

I have a table Auditing logins, I'd like to just extract the latest login for each user, which the below sql accomplishes.
How would I format the sql query into a linq to sql query?
SELECT * FROM AuditTable adt1
inner join UserTable usr
on adt1.[UserName] = usr.[User_Id]
WHERE [TimeStamp] = ( SELECT MAX([TimeStamp])
FROM AuditTable adt2
WHERE adt1.UserName = adt2.UserName)
Another way of writing the query in sql being;
SELECT adt1.[UserName], usr.First_Name, max([TimeStamp]) as [TimeStamp]
FROM AuditTable adt1
INNER JOIN UserTable usr
on adt1.[UserName] = usr.[User_Id]
GROUP BY adt1.[UserName] ,usr.First_Name
query #2:
from adt1 in dc.AuditTable
join usr in dc.UserTable on adt1.UserName == usr.UserID
group adt1 by adt1.username int ag
select new { UserName = ag.Key, TSMax = ag.Max(ts => adt1.TimeStamp) }
Why the join to the user table? Filter?
I got it working... the answer was to use the let keyword.
as an aside I highly recommend using LinqPad when trying to work these things out.
from adt in AuditTable
join usr in UserTable
on adt.UserName equals usr.User_Id
group adt by
new { adt.UserName, adt.Roles, usr.First_Name, usr.Last_Name }
into g
let LastAccessed = g.Max(a => a.TimeStamp)
select new
{ UserName = g.Key.UserName,
Roles = g.Key.Roles,
FirstName = g.Key.First_Name,
LastName = g.Key.Last_Name,
TimeStamp = LastAccessed
}