I just want to get the latest record from my table based on my updated timstamp data, because I want to run this query in my crontab. How can I always get the latest updated data from my query? This is my query:
select
x.updated as updated,
x.data_a->>'custom1' as mch_group,
x.data_a->>'custom2' as mch_name,
x.data_a->>'custom3' as mch_id,
x.data_a->'simAuxFieldsDTO'->>'custom4' as terminal_id,
x.data_a->>'msisdn' as msisdn,
x.data_a->>'simId' as sim_id,
x.data_a->>'acctName' as acct_name,
x.data_a->>'activationDate' as activation_date,
x.data_a->>'inSession' as in_session,
x.data_a->>'sessionType' as session_type,
x.data_a->>'statusNameDisplay' as status_name_display,
x.data_a->>'monthToDateDataUsageMB' as month_todate_data_usage,
x.data_a->>'linePayStatus' as line_pay_status,
x.data_a->>'iccid' as iccid,
x.data_a->>'monthToDateSmsUsage' as month_todate_sms_usage,
x.data_a->>'monthToDateVoiceUsage' as month_todate_voice_usage,
x.data_a->>'overageLimitReached' as overage_limit_reached,
x.data_a->>'termStartDate' as term_startdate,
x.data_a->>'termEndDate' as term_enddate,
x.data_a->>'migratedSim' as migrated_sim
from (
select jsonb_array_elements(a.data_a) as data_a, a.updated as updated
from (
select updated, json_data -> 'data' as data_a
from tcash_edc_json
where updated > TO_TIMESTAMP('2018-09-21 03:02:00','YYYY-MM-DD HH:MI:SS')
)a
) x;
try replacing the where conditions with order by and limit:
instead of this:
where updated > TO_TIMESTAMP('2018-09-21 03:02:00','YYYY-MM-DD HH:MI:SS')
this:
order by updated desc limit 1
You can try to get max(updated) and then do the select where updated = max (updated)
I got a few problems when trying to fetch only specific data. First I don't know how to create a sql query (current sql query I can grab only one user) so I can grab the data like this.
Second I want to grab 1 year data until current date. Below is my sql query done so far (I need to do it manual one by one).
SELECT type, COUNT(*) FROM (
TABLE_DATE_RANGE([githubarchive:day.events_],
TIMESTAMP('2013-1-01'),
TIMESTAMP('2015-08-28')
)) AS events
WHERE type IN ("CommitCommentEvent","CreateEvent","DeleteEvent","DeploymentEvent","DeploymentStatusEvent","DownloadEvent","FollowEvent",
"ForkEvent","ForkApplyEvent","GistEvent","GollumEvent","IssueCommentEvent","IssuesEvent","MemberEvent","MembershipEvent","PageBuildEvent",
"PublicEvent","PullRequestEvent","PullRequestReviewCommentEvent","PushEvent","ReleaseEvent","RepositoryEvent","StatusEvent","TeamAddEvent",
"WatchEvent") AND actor.login = "datomnurdin"
GROUP BY type;
Reference:
https://www.githubarchive.org/
https://github.com/igrigorik/githubarchive.org
Here is how to properly pivot the data:
SELECT actor.login,
ifnull(sum(if(type='CommitCommentEvent',1,null)),0) as CommitCommentEvent,
ifnull(sum(if(type='CreateEvent',1,null)),0) as CreateEvent,
ifnull(sum(if(type='DeleteEvent',1,null)),0) as DeleteEvent,
ifnull(sum(if(type='DeploymentEvent',1,null)),0) as DeploymentEvent,
ifnull(sum(if(type='DeploymentStatusEvent',1,null)),0) as DeploymentStatusEvent,
ifnull(sum(if(type='DownloadEvent',1,null)),0) as DownloadEvent,
ifnull(sum(if(type='FollowEvent',1,null)),0) as FollowEvent,
ifnull(sum(if(type='ForkEvent',1,null)),0) as ForkEvent,
ifnull(sum(if(type='ForkApplyEvent',1,null)),0) as ForkApplyEvent,
ifnull(sum(if(type='GistEvent',1,null)),0) as GistEvent,
ifnull(sum(if(type='GollumEvent',1,null)),0) as GollumEvent,
ifnull(sum(if(type='IssueCommentEvent',1,null)),0) as IssueCommentEvent,
ifnull(sum(if(type='IssuesEvent',1,null)),0) as IssuesEvent,
ifnull(sum(if(type='MemberEvent',1,null)),0) as MemberEvent,
ifnull(sum(if(type='MembershipEvent',1,null)),0) as MembershipEvent,
ifnull(sum(if(type='PageBuildEvent',1,null)),0) as PageBuildEvent,
ifnull(sum(if(type='PublicEvent',1,null)),0) as PublicEvent,
ifnull(sum(if(type='PullRequestEvent',1,null)),0) as PullRequestEvent,
ifnull(sum(if(type='PullRequestReviewCommentEvent',1,null)),0) as PullRequestReviewCommentEvent,
ifnull(sum(if(type='PushEvent',1,null)),0) as PushEvent,
ifnull(sum(if(type='ReleaseEvent',1,null)),0) as ReleaseEvent,
ifnull(sum(if(type='RepositoryEvent',1,null)),0) as RepositoryEvent,
ifnull(sum(if(type='StatusEvent',1,null)),0) as StatusEvent,
ifnull(sum(if(type='TeamAddEvent',1,null)),0) as TeamAddEvent,
ifnull(sum(if(type='WatchEvent',1,null)),0) as WatchEvent,
FROM (
TABLE_DATE_RANGE([githubarchive:day.events_],
DATE_ADD(CURRENT_TIMESTAMP(), -1, "YEAR"),
CURRENT_TIMESTAMP()
)) AS events
WHERE type IN ("CommitCommentEvent","CreateEvent","DeleteEvent","DeploymentEvent","DeploymentStatusEvent","DownloadEvent","FollowEvent",
"ForkEvent","ForkApplyEvent","GistEvent","GollumEvent","IssueCommentEvent","IssuesEvent","MemberEvent","MembershipEvent","PageBuildEvent",
"PublicEvent","PullRequestEvent","PullRequestReviewCommentEvent","PushEvent","ReleaseEvent","RepositoryEvent","StatusEvent","TeamAddEvent",
"WatchEvent")
GROUP BY 1
limit 100
I have the following SQL query that I need to convert into LINQ with VB.NET
SELECT *
FROM (SELECT Id
,LocationCode
,LocationName
,ContactName
,ContactEmail
,Comments
,SBUName
,CreatedBy
,CreatedDtm
,ModifiedBy
,ModifiedDtm
,ROW_NUMBER() OVER (PARTITION BY LocationCode ORDER BY ID) AS RowNumber
FROM testDB ) as rows
WHERE ROWNUMBER = 1
There are many duplicates of location code so I only want to display one record of each and the user will be able to edit the information. Once they edit I will save the info for all records that are for that specific location code.
I couldn't use DISTINCT here, it would still bring back all of the data since the CreatedBy/ModifiedBy are different.
By using the following LINQ query to select all of the data, is there a way I can get the DISTINCT records for LocationCode out of it?
queryLocMaint = From MR In objcontextGSC.TestDB
Select MR.Id,
MR.LocationCode,
MR.LocationName,
MR.SBUName,
MR.ContactName,
MR.ContactEmail,
MR.Comments,
MR.CreatedBy,
MR.CreatedDtm,
MR.ModifiedBy,
MR.ModifiedDtm()
ROW_NUMBER is not supported in LINQ, maybe you can use this GROUP BY approach:
Dim q = From mr In objcontextGSC.TestDB
Group mr By mr.LocationCode Into LocationCodeGroup = Group
Select LocationCodeGroup.OrderBy(Function(mr) mr.Id).First()
This takes the first row of each LocationCode-group ordered by id.
I have a requirement to create a Sales report and I have a sql query:
SELECT --top 1
t.branch_no as TBranchNo,
t.workstation_no as TWorkstation,
t.tender_ref_no as TSaleRefNo,
t.tender_line_no as TLineNo,
t.tender_code as TCode,
T.contribution as TContribution,
l.sale_line_no as SaleLineNo
FROM TENDER_LINES t
LEFT JOIN SALES_TX_LINES l
on t.branch_no = l.branch_no and t.workstation_no = l.workstation_no and t.tender_ref_no = l.sale_tx_no
where l.sale_tx_no = 2000293 OR l.sale_tx_no = 1005246 --OR sale_tx_no = 1005261
order by t.tender_ref_no asc,
l.sale_line_no desc
The results of the query look like the following:
The results I am trying to achieve is:
With only 1 line for transaction 2 either SaleLineNo 1 or 2, while still have=ing both lines for transaction 1 because the TCode is different.
Thanks
I am using SSQL2012.
Not exactly sure on what data you have, but you might want to try
GROUP BY TlineNo, TCode ...
But you have to keep a look on not to group by something that would result in duplicate contribution values.
You can use the ROW_NUMBER function that allows to partition the rows in groups, and number the lines inside each group starting by one. If you choose the right columns to define the partition, and keep only the rows with "row_number = 1`, you have solved the first part of your problem, i.e. discarding the lines that don't have to appear in the report. (See the sample sin the linked documentation, they're quite clear).
Once you have solved this problem, you simply have to repeat what you're doing, but on the result of this data, instead of the original data. You can use a view, a CTE, or a subselect to achieve your result, i.e.
With view:
CREATE VIEW FilteredData AS -- here the rank function query, then selct from the view
SELECT --here your current query --
FROM FilteredData
With CTE
WITH -- here the rank function query
SELECT -- your current querym, from the CTE
With subselect
SELECT -- your current query
FROM (SELECT FROM -- here the rank function query -- )
Appreciate your assistance with my query. After playing around, I have found a solution that works just as I want. It is as below: I did a Group by as hinted by #Yogesh86 on a few fields.
SELECT
MAX(t.branch_no) as TBranchNo,
Max(t.workstation_no) as TWorkstation,
t.tender_ref_no as TSaleRefNo,
Max(t.tender_line_no) as TLineNo,
t.tender_code as TCode,
MAx(T.contribution) as TContribution,
MAX(l.sale_line_no) as SaleLineNo
FROM TENDER_LINES t
LEFT JOIN SALES_TX_LINES l
on t.branch_no = l.branch_no and t.workstation_no = l.workstation_no and t.tender_ref_no = l.sale_tx_no
where l.sale_tx_no = 2000293 OR l.sale_tx_no = 1005246 --OR sale_tx_no = 1005261
GROUP BY
t.tender_ref_no,
t.tender_line_no,
t.tender_code
I have this query:
SELECT *
FROM GUITARS.FENDER
WHERE FENDER.GUITARTYPE IN (
SELECT GUITARTYPE,Count(*)
FROM GUITARS.GUITAR_TYPE
WHERE GuitarColor = 'RED'
Group By GUITARTYPE
Having Count(*) = 1)
Basically I want to make sure I am only checking the Guitartypes that don't have duplicates with a count. The issue is the IN is only checking for 1 column, but i need the count(*)in there for instances of more than one guitar type. Is there a way to make this query work, or possible another way around doing the count.
You don't need to have the count() returned in the select statement, having the group by and the count() is sufficient.
SELECT *
FROM GUITARS.FENDER
WHERE FENDER.GUITARTYPE IN (
SELECT GUITARTYPE
FROM GUITARS.GUITAR_TYPE
WHERE GuitarColor = 'RED'
Group By GUITARTYPE
Having Count(*) = 1)
Adding the code so it looks right.