query doesn't return proper value - cakephp

I have a query as given below:
SELECT DISTINCT(FldStrategyID), FldTradeServerName,FldBaseDir,FldBinaryStartTime
FROM TblOrders Ord,
(SELECT CONCAT(FldStrategyID,'.',FldTradeServerName,'.',FldBaseDir,'.',FldBinaryStartTime) strid
FROM TblOrders
GROUP BY FldStrategyID) as OrderView
WHERE Ord.FldStrategyID !=''
AND OrderView.strid NOT IN (SELECT FldStrategyID FROM TblStrategyMasters WHERE 1)
The problem is that this part is not working:
OrderView.strid NOT IN (SELECT FldStrategyID FROM TblStrategyMasters WHERE 1)

I think that the quotes(') are missing somewhere.

Related

Display of online users on the system

I don't know exactly where I'm wrong, but I need a list of all the workers who are currently at work (for the current day), this is my sql query:
SELECT
zp.ID,
zp.USER_ID,
zp.Arrive,
zp.Deppart,
zp.DATUM
FROM time_recording as zp
INNER JOIN personal AS a on zp.USER_ID, = zp.USER_ID,
WHERE zp.Arrive IS NOT NULL
AND zp.Deppart IS NULL
AND zp.DATUM = convert(date, getdate())
ORDER BY zp.ID DESC
this is what the data looks like with my query:
For me the question is, how can I correct my query so that I only get the last Arrive time for the current day for each user?
In this case to get only these values:
Try this below script using ROW_NUMBER as below-
SELECT * FROM
(
SELECT zp.ID, zp.USER_ID, zp.Arrive, zp.Deppart, zp.DATUM,
ROW_NMBER() OVER(PARTITION BY zp.User_id ORDER BY zp.Arrive DESC) RN
FROM time_recording as zp
INNER JOIN personal AS a
on zp.USER_ID = zp.USER_ID
-- You need to adjust above join relation as both goes to same table
-- In addition, as you are selecting nothing from table personal, you can drop the total JOIN part
WHERE zp.Arrive IS NOT NULL
AND zp.Deppart IS NULL
AND zp.DATUM = convert(date, getdate())
)A
WHERE RN =1
you can try this:
SELECT DISTINCT
USER_ID,
LAR.LastArrive
FROM time_recording as tr
CROSS APPLY (
SELECT
MAX(Arrive) as LastArrive
FROM time_recording as ta
WHERE
tr.USER_ID = ta.USER_ID AND
ta.Arrive IS NOT NULL
) as LAR

SQL Server : auto-increment column in function of two columns values

I want to produce a similar table that this table below.
In the first time, this table is create by another program but I must insert a new rows when an images is added.
For this, I have try this request but the select is done upstream of the insert and the Max function is useless.
#idart contains a table with multiple CODE and ID_IMG but without ORDER number.
INSERT INTO [NOMENC_ARTICLES_IMAGES]
SELECT
I.CODE, I.ID As ID_IMG,
CASE
WHEN (SELECT MAX(AI.ORDRE) FROM [NOMENC_ARTICLES_IMAGES] AS AI
WHERE AI.CODE = A.CODE) IS NULL
THEN 0
ELSE (SELECT MAX(AI.ORDRE) FROM [NOMENC_ARTICLES_IMAGES] AS AI
WHERE AI.CODE = A.CODE) + 1
END AS ORDER
FROM
#idart AS I
LEFT JOIN
[NOMENC_ARTICLES_IMAGES] AS A ON A.CODE = I.CODE
Could you help me to increment the ORDER column in terms of CODE and ID_IMG?
EDIT :
In [NOMENC_ARTICLES_IMAGES] I have :
And I want to add 1, 2, x value contains in #idart.
For exemple :
#idart :
The expected result after insert :
I hope that with this example, you will better understand my need
I think I've find the solution based on the answer of casenonsensitive :
INSERT INTO [NOMENC_ARTICLES_IMAGES]
SELECT I.CODE, I.ID As ID_IMG,
row_number() over (partition by I.CODE order by I.ID) +
CASE WHEN A.ORDER IS NULL THEN -1 ELSE A.ORDER END As ORDER
FROM
#idart AS I
LEFT JOIN
(SELECT AI.CODE, MAX(AI.ORDER) As ORDER FROM [NOMENC_ARTICLES_IMAGES] As AI GROUP BY AI.CODE) AS A ON A.CODE = I.CODE
Thanks for all !
Use an analytical function like this:
INSERT INTO [NOMENC_ARTICLES_IMAGES]
select I.CODE, I.ID As ID_IMG,
row_number() over (partition by I.CODE order by I.ID)
+ (select isnull(max(order), 0) from [NOMENC_ARTICLES_IMAGES] AS A ON A.CODE = I.CODE)
As ORDER
FROM
#idart AS I
But pay attention, that you're always getting the highest order for the highest I.ID per I.CODE. In some cases that works, in others I.ID would not have to be strictly growing. Meaning the newest images wouldn't get the biggest ORDER's

how to optimize the inner query?

SELECT mark.student_id,
.
.
.
MAX(mark.SAVE_TIME) AS SAVE_TIME,
(SELECT tamil FROM mark WHERE SAVE_TIME = (MAX(mark.SAVE_TIME))) AS tamilmark,
(SELECT english FROM mark WHERE SAVE_TIME = (MAX(mark.SAVE_TIME))) AS englishmark
FROM
(.......
)
Above query will return the my expected data. but the problem is two times I'm fetching the data from same table. How can i achieve with single select statement?
Here is one way that could be rewritten:
SELECT MySubTable.*, mark.tamil, mark.english
(
SELECT student_id,
MAX(mark.SAVE_TIME) AS SAVE_TIME
FROM Table
GROUP BY student_id
) MySubTable
INNER JOIN mark
ON mark.SAVE_TIME = MySubTable.SAVE_TIME
You can use one select statement to get all values, but you also will get NULLs (in my example I gave them 'n/a' value). To get rid of them you will have to select this subquery:
SELECT *
FROM (
SELECT DISTINCT mark.student_id,
MAX(MARK.SAVE_TIME) OVER() AS SAVE_TIME,
CASE WHEN SAVE_TIME = (MAX(mark.SAVE_TIME) OVER()) THEN tamil ELSE 'n/a' END AS tamilmark,
CASE WHEN SAVE_TIME = (MAX(mark.SAVE_TIME) OVER()) THEN english ELSE 'n/a' END AS englishmark
FROM MARK
) AS t
WHERE tamilmark != 'n/a'

Clean up SQL Server script

SELECT pp.pat_key, MAX(pp.PROV_NPI) [Provider_ID], CONCAT(pp.LAST_NM,' ',pp.FIRST_NM) [Provider_Name]
INTO pat_primary_provider
FROM TRDW.dbo.PATIENT_PROVIDER pp
WHERE IS_PCP=1
AND pat_key IN (SELECT Consumer_ID FROM CareWire0521)
GROUP BY pp.PAT_KEY, pp.last_nm, pp.FIRST_NM;
SELECT ppp.*
INTO ppp1
FROM (SELECT PAT_KEY, MAX(provider_ID) AS maxprov FROM pat_primary_provider GROUP BY PAT_KEY) AS x
INNER JOIN pat_primary_provider AS ppp ON ppp.PAT_KEY = x.PAT_KEY AND ppp.Provider_ID = x.maxprov;
I need to get the results of ppp1 only using one query (no INTO statements) in SQL Server. Please help.
Simply put the first query into a CTE (without the INTO clause). Then select from that.
;WITH pat_primary_provider AS
(
-- The first query goes here
)
-- The second query goes here
But something like below might also return the PAT_KEY's with the maximum PROV_NPI:
SELECT TOP 1 WITH TIES
PAT_KEY,
MAX(PROV_NPI) AS [Max_Provider_ID],
CONCAT(LAST_NM,' ',FIRST_NM) AS [Patient_Provider_Full_Name]
FROM TRDW.dbo.PATIENT_PROVIDER pp
WHERE IS_PCP = 1
AND PAT_KEY IN (SELECT Consumer_ID FROM CareWire0521)
GROUP BY PAT_KEY, LAST_NM, FIRST_NM
ORDER BY row_number() over (order by MAX(PROV_NPI) desc);
Whats wrong with just inserting the first query as subqueries into the second?
SELECT ppp.*
FROM (SELECT PAT_KEY, MAX(provider_ID) AS maxprov FROM (SELECT pp.pat_key, MAX(pp.PROV_NPI) [Provider_ID], CONCAT(pp.LAST_NM,' ',pp.FIRST_NM) [Provider_Name]
FROM TRDW.dbo.PATIENT_PROVIDER pp
WHERE IS_PCP=1
AND pat_key IN (SELECT Consumer_ID FROM CareWire0521)
GROUP BY pp.PAT_KEY, pp.last_nm, pp.FIRST_NM) GROUP BY PAT_KEY) AS x
INNER JOIN (SELECT pp.pat_key, MAX(pp.PROV_NPI) [Provider_ID], CONCAT(pp.LAST_NM,' ',pp.FIRST_NM) [Provider_Name]
FROM TRDW.dbo.PATIENT_PROVIDER pp
WHERE IS_PCP=1
AND pat_key IN (SELECT Consumer_ID FROM CareWire0521)
GROUP BY pp.PAT_KEY, pp.last_nm, pp.FIRST_NM) AS ppp ON ppp.PAT_KEY = x.PAT_KEY AND ppp.Provider_ID = x.maxprov;

Create View - Declare a variable

I am creating a view that is using that STUFF function. I want to put the result of STUFF in a variable for my view. The problem I am having is declaring my variable. It gives me the message "Incorrect Syntax near 'DECLARE'. Expecting '(' or SELECT." I already have the '(' in there. I have tried putting a BEGIN before it. I have tried putting it after the SELECT word. But nothing seems to work and I cannot find a solution in my search. I am using SQL Server 2012
CREATE VIEW [AQB_OB].[GISREQUESTEDBURNS]
AS
(DECLARE #CONDITIONS AS varchar(20)
SET #CONDITIONS = (SELECT DISTINCT BD.[RequestedBurnsID]
,[ConditionsReasonsID] = STUFF((SELECT ', ' + CONVERT(VARCHAR (20),[ConditionsReasonsID]) FROM [AQB_OB].[BurnDecisions] WHERE [RequestedBurnsID]= BD.[RequestedBurnsID] ORDER BY [RequestedBurnsID] ASC
FOR XML PATH ('')) , 1 , 1, '') FROM
[AQB_OB].[BurnDecisions] BD)
SELECT RB.[RequestedBurnsID] AS REQUESTEDBURNID
,BUY.[BurnYear] AS BURNYEAR
,CY.[CurrentYear] AS CURRENTYEAR
,RB.[BurnSitesID] AS BURNSITESID
,[BurnerID] AS BURNERID
,[Contact] AS CONTACT
,[BurnDecision] AS BURNDECISION
,RB.[Comment] AS COMMENT
,#CONDITIONS AS CONDITIONS
FROM [AQB_MON].[AQB_OB].[RequestedBurns] RB
LEFT join AQB_MON.[AQB_OB].[PileDryness] PD on RB.[PileDrynessID] = PD.[PileDrynessID]
inner join AQB_MON.[AQB_OB].[BurnYear] BUY on BUY.BurnYearID = BP.BurnYearID
inner join AQB_MON.[AQB_OB].[CurrentYear] CY on CY.CurrentYearID = BUY.CurrentYearID
GO
You can't declare variables in a view. Could you make it into a function or stored procedure?
Edit - you might also be able to put something into a CTE (Common Table Expression) and keep it as a view.
e.g.
WITH conditions as
(
... do the STUFF here
)
SELECT blah
FROM blah
INNER JOIN conditions
(or CROSS JOIN conditions if its just one row, I can't quite decipher what your data is like)
Here is a sample query that uses a CTE (Common Table Expression) to nicely emulate internal variable construction, as described by James Casey. You can test-run it in your version of SQL Server.
CREATE VIEW vwImportant_Users AS
WITH params AS (
SELECT
varType='%Admin%',
varMinStatus=1)
SELECT status, name
FROM sys.sysusers, params
WHERE status > varMinStatus OR name LIKE varType
SELECT * FROM vwImportant_Users
yielding output:
status name
12 dbo
0 db_accessadmin
0 db_securityadmin
0 db_ddladmin
also via JOIN
WITH params AS ( SELECT varType='%Admin%', varMinStatus=1)
SELECT status, name
FROM sys.sysusers INNER JOIN params ON 1=1
WHERE status > varMinStatus OR name LIKE varType
also via CROSS APPLY
WITH params AS ( SELECT varType='%Admin%', varMinStatus=1)
SELECT status, name
FROM sys.sysusers CROSS APPLY params
WHERE status > varMinStatus OR name LIKE varType
Or use a CTE (common table expression) as subselect like:
WITH CTE_Time(Clock)
AS(
SELECT 11 AS [Clock] -- set var
)
SELECT
DATEPART(HOUR, GETDATE()) AS 'actual hour',
CASE
WHEN DATEPART(HOUR, GETDATE()) >= (SELECT [Clock] FROM CTE_Time) THEN 'after'
ELSE 'before'
END AS [Data]
Try put the condition subquery directly inside the the view select statement. you may CAST the XML to VARCHAR(20).
CREATE VIEW [AQB_OB].[GISREQUESTEDBURNS]
AS
SELECT RB.[RequestedBurnsID] AS REQUESTEDBURNID
,BUY.[BurnYear] AS BURNYEAR
,CY.[CurrentYear] AS CURRENTYEAR
,RB.[BurnSitesID] AS BURNSITESID
,[BurnerID] AS BURNERID
,[Contact] AS CONTACT
,[BurnDecision] AS BURNDECISION
,RB.[Comment] AS COMMENT,
(
SELECT DISTINCT BD.[RequestedBurnsID],
[ConditionsReasonsID] = STUFF((SELECT ', ' + CONVERT(VARCHAR (20), [ConditionsReasonsID]) FROM [AQB_OB].[BurnDecisions]
WHERE [RequestedBurnsID]= BD.[RequestedBurnsID] ORDER BY [RequestedBurnsID] ASC
FOR XML PATH ('')) , 1 , 1, '') FROM
[AQB_OB].[BurnDecisions] BD
) AS CONDITIONS
FROM [AQB_MON].[AQB_OB].[RequestedBurns] RB
LEFT join AQB_MON.[AQB_OB].[PileDryness] PD on RB.[PileDrynessID] = PD.[PileDrynessID]
inner join AQB_MON.[AQB_OB].[BurnYear] BUY on BUY.BurnYearID = BP.BurnYearID
inner join AQB_MON.[AQB_OB].[CurrentYear] CY on CY.CurrentYearID = BUY.CurrentYearID

Resources