DECLARE #items table (
pfid varchar(8),
timestart datetime,
timeend datetime
)
insert INTO #items(pfid,timestart,timeend)
VALUES('123456','12:00 AM','3:00 AM')
,('987654', '2:00 AM', '4:00 PM')
,('492384', '3:00 PM', '9:00 PM')
SELECT * FROM #items a
INNER JOIN #items b
ON a.timestart < b.timeend
AND b.timestart < a.timeend
AND a.pfid != b.pfid
I need the select statement above converted to LINQ. In my code, I am working with a DataTable, named 'dt'. My table has three columns just like in the example above, exact same names and populated with this data.
I am struggling to create a LINQ query that will query my DataTable in the same fashion my SQL query is working with the temp table above. Please assist. Thanks in advance!
something in the line of...i didn't test this but you may have to do Datetime.Parse etc if needed...
(from r1 in dt.Rows.OfType<DataRow>()
from r2 in dt.Rows.OfType<DataRow>()
where r1["timestart"] < r2["timeend"] && r2["timestart"] < r1["timeend"] && r1["pfid"] != r2["pfid"]
select new {R1=r1, R2=r2})
Related
I'm trying to edit this code to be dynamic as I'm going to schedule it to run.
Normally I would input the date in the where statement as 'YYYY-MM-DD' and so to make it dynamic I changed it to DATE(). I'm not erroring out, but I'm also not pulling data. I just need help with format and my google searching isn't helping.
PROC SQL;
CONNECT TO Hadoop (server=disregard this top part);
CREATE TABLE raw_daily_fcast AS SELECT * FROM connection to Hadoop(
SELECT DISTINCT
a.RUN_DATE,
a.SCHEDSHIPDATE,
a.SOURCE,
a.DEST ,
a.ITEM,
b.U_OPSTUDY,
a.QTY,
c.case_pack_qty
FROM CSO.RECSHIP a
LEFT JOIN CSO.UDT_ITEMPARAM b
ON a.ITEM = b.ITEM
LEFT JOIN SCM.DIM_PROD_PLN c
ON a.ITEM = c.PLN_NBR
WHERE a.RUN_DATE = DATE()
AND a.SOURCE IN ('88001', '88003', '88004', '88006', '88008', '88010', '88011', '88012',
'88017', '88018', '88024', '88035', '88040', '88041', '88042', '88047')
);
DISCONNECT FROM Hadoop;
QUIT;
When RUN_DATE is a string you can generate the current date string in-line on the SAS side
WHERE a.RUN_DATE = %str(%')%sysfunc(date(),yymmdd10.)%str(%')
AND ...
or
WHERE a.RUN_DATE = %sysfunc(quote(%sysfunc(date(),yymmdd10.),%str(%')))
AND ...
For the case of RUN_DATE being a string containing DATE9 formatted values, change the yymmdd10. to date9.
change:
WHERE a.RUN_DATE = DATE()
to:
WHERE a.RUN_DATE = PUT(date(), YYMMDD10.) AS date
I have two Hive tables, one holds a date value with Timestamp datatype. If I query on one specific record using the key it shows the date value correctly.
select acct_key, account_open_date from Table_1 where acct_key=1234;
acct_id account_open_date
1234 1963-03-01 00:00:00
However when joining this table with another table, the returned timestamp value changed to some value in year 2031
select a.acct_key, b.account_open_date
from Table_2 a left outer join on Table_1 b
on a.acct_key=b.acct_key;
acct_id account_open_date
1234 2031-03-19 00:00:00
Seems this issues only occurs for date value before Unix epoch time(1970). Any suggestion? Thanks
Two issues here, first thing join not working with timestamp and epoch timestamp. With your last line, I assume that the join is returning correct timestamp for other timestamp. Correct me if I am wrong. So if that is resolved, you can look into here to handle epoch time
I was unable to reproduce what you are seeing but nonetheless, you could try casting account_open_date to string.
select a.acct_id
, b.new
, other_columns
from db.table1
left outer join (
select *
, cast(account_open_date as string) new
from db.table2 ) b
on a.acct_id=b.acct_id
I tried. Cast timestamp as String in a nested query worked as below. I also tried without nested query, but that does not work. Anyone knows why?
Not worked version:
SELECT
a.acct_id
,CAST(b.account_open_date AS STRING) new
,other_columns
FROM
db.table1 a,
LEFT OUTER JOIN db.table2 b ON (a.acct_id = b.acct_id)
;
worked version:
SELECT
a.acct_id
,b.new
,other_columns
FROM
db.table1 a
LEFT OUTER JOIN (
SELECT
*
,CAST(account_open_date AS STRING) new
FROM
db.table2
) b
ON (a.acct_id=b.acct_id)
I have the following query in oracle. I want to convert it to PostgreSQL form. Could someone help me out in this,
SELECT user_id, user_name, reports_to, position
FROM pr_operators
START WITH reports_to = 'dpercival'
CONNECT BY PRIOR user_id = reports_to;
A something like this should work for you (SQL Fiddle):
WITH RECURSIVE q AS (
SELECT po.user_id,po.user_name,po.reports_to,po.position
FROM pr_operators po
WHERE po.reports_to = 'dpercival'
UNION ALL
SELECT po.user_id,po.user_name,po.reports_to,po.position
FROM pr_operators po
JOIN q ON q.user_id=po.reports_to
)
SELECT * FROM q;
You can read more on recursive CTE's in the docs.
Note: your design looks strange -- reports_to contains string literals, yet it is being comapred with user_id which typicaly is of type integer.
This question might have answer ... But not for openbravo with postgresql database.
I have openbravo 3.0 framework. In my window i have two date fields namely fromdate and todate. The requirement is i have to write a hql where clause to filter the records on the basis of current date.The date field is of timestamp without timezone.
Means fromdate < currentdate
and todate > currentdate .
I went through this link and wrote the hql where clause as
e.id in(select s.id from Tablename as s where s.fromdate < current_Date and s.todate>current_date)
when i open this window i get this error as
Exception when creating query select e from Tablename as e
where ( e.id in(select s.Tablename_ID from Tablename as s where s.fromdate < (current_date) and s.todate < (current_date)
however if i remove the current date conditions as
e.id in(select s.id from Tablename as s).. It is working fine.
Is it because of current_Date function ? .I tried even with now function .. but i get the same error.
!!! Got The Error.
There is a problem in the query i wrote , in the where clause i am selecting the id's which is not correct,hence when i gave the below query it was running correctly.
(Tablename.fromdate < currrent_date and TableName.todate>current_date) There was no problem with the current_date function.
I thought may be it would help some one!!!
Tip: If you want to write hql query correctly in openbravo , pls install the Hql query tool module that is available freely for community edition of openbravo.
Happy Coding
i have a Stored Procedure which returns output as a ref cursor. I would like to store the output in another table using the MERGE statement. I'm having problems however mixing all the statements together (WITH, USING, MERGE etc..).
Can somebody assist? Thanks!
This is the table i want the output in (STOP_TIME is left out on purpose):
TABLE: **USER_ALLOCATION**
START_TIME date NOT NULL Primary Key
USER_ID number NOT NULL Primary Key
TASK_ID number NULL
This is the SP:
create or replace
PROCEDURE REPORT_PLAN_AV_USER
(
from_dt IN date,
to_dt IN date,
sysur_key IN number,
v_reservations OUT INTRANET_PKG.CURSOR_TYPE
)
IS
BEGIN
OPEN v_reservations FOR
with
MONTHS as (select FROM_DT + ((ROWNUM-1) / (24*2)) as DT from DUAL connect by ROWNUM <= ((TO_DT - FROM_DT) * 24*2) + 1),
TIMES as (select DT as START_TIME,(DT + 1/48) as STOP_TIME from MONTHS where TO_NUMBER(TO_CHAR(DT,'HH24')) between 8 and 15 and TO_NUMBER(TO_CHAR(DT,'D')) not in (1,7))
select
TIMES.START_TIME,
TIMES.STOP_TIME,
T.TASK_ID,
sysur_key USER_ID,
from
TIMES
left outer join (ALLOCATED_USER u INNER JOIN REQUIRED_RESOURCE r ON u.AU_ID = r.RR_ID INNER JOIN TASK t ON r.TASK_ID = t.TASK_ID)
ON u.USER_ID = sysur_key AND t.PLAN_TYPE = 3 AND TIMES.start_time >= TRUNC30(t.START_DATE) AND TIMES.start_time < TRUNC30(t.FINISH_DATE)
where u.USER_ID is null OR u.USER_ID = sysur_key
order by START_TIME ASC;
END;
I don't think you can resuse the cursor unless you write a lot of procedural code.
Can't you write a single merge statement and drop procedure REPORT_PLAN_AV_USER?
If you still need procedure REPORT_PLAN_AV_USE you can create a view that you use in procedure REPORT_PLAN_AV_USER and in your merge statement (to prevent code duplication).