I need to add a proper "needCounter" column code to my query in Access.
It should count rows from 1 to "WorksPerWeekCounter" as it is shown in last, yellow column. Or better 0 to "WorksPerWeekCounter-1" :)
I find my code wrong because my "counter" does not correspond to my "counterMin"
Let's say I have a table [WORKS(roboczy)], and first 4 columns are just like below [http://i.stack.imgur.com/mTO53.jpg][picture] shows. (sorry, my reputation is too low to show pictures)
This is the Access query code I use (week2date function is in VBA code):
SELECT
W.Id_work, W.Id_pracownika, W.vecka, W.Rok,
week2date(W.vecka,W.Rok) AS dataWK,
C.countWorksPerWeek as WorksPerWeekCounter,
C.Id_workMin as id_workMin,
(W.Id_work- C.Id_workMin) as AddDays,
DCount([Id_work],"[WORKS(roboczy)]","Id_work<=" & [Id_work]) AS counter,
C.counterMin
FROM [WORKS(roboczy)] W
inner join
( SELECT count(id_work) as countWorksPerWeek, Id_pracownika, vecka, Rok, min(id_work) as Id_workMin, min(DCount([Id_work],"[WORKS(roboczy)]","Id_work<=" & [Id_work])) AS counterMin
FROM [WORKS(roboczy)]
GROUP BY Id_pracownika, vecka, Rok
) C ON W.Id_pracownika = C.Id_pracownika AND W.vecka = C.vecka AND C.Rok = W.Rok
GROUP BY W.Id_pracownika, W.vecka, Id_work, W.Rok, week2date(W.vecka,W.Rok), C.countWorksPerWeek, C.Id_workMin, C.counterMin
You can use a series of subqueries in one step:
SELECT w.Id_work, w.Id_pracownika, w.vecka, w.Rok, w.dataWK,
(SELECT count(w2.Rok) FROM WorkCounter w2
WHERE w2.vecka = w.vecka GROUP BY w2.vecka) As WorksPerWeekCounter,
(SELECT min(w3.Id_work) FROM WorkCounter w3
WHERE w3.vecka = w.vecka GROUP BY w3.vecka) As id_workMin,
w.Id_work -
(SELECT min(w3.Id_work) FROM WorkCounter w3
WHERE w3.vecka = w.vecka GROUP BY w3.vecka) As AddDays,
(SELECT count(*) FROM WorkCounter w4
WHERE w4.Id_work <= w.Id_work and w4.vecka = w.vecka) As Counter
FROM [WORKS(roboczy)] W
Related
I have a question from my sql script in DB2.
SELECT a.fcyfdtaid current_id_book, a.fcyyyymm current_month_book, a.fcyaccsts current_quality_book, a.fcyarrmth current_dpd,
b.fcyfdtaid old_id_book, b.fcyyyymm old_month_book, b.fcyaccsts old_quality_book, b.fcyarrmth old_dpd
FROM crbidnpd.entfcym0 a JOIN crbidnpd.entfcym0 b
ON a.fcyfdtaid = b.fcyfdtaid AND
b.fcyarrmth < a.fcyarrmth AND
a.fcyyyymm='201911' AND a.fcyarrmth>10
where NOT EXISTS
(SELECT b.fcyfdtaid old_id_book FROM crbidnpd.entfcym0 c
WHERE c.fcyfdtaid = a.fcyfdtaid AND c.fcyyyymm = a.fcyyyymm AND c.fcyyyymm = b.fcyyyymm) order by a.fcydtaid;
and the result
images
how to make it being one row that displays in the last 2 years, on the left 2011 and on the right only 2010?
I have 3 columns
ProjektNummer, DokumentNummer, DokumentType
In DokumentType there are 3 possible values: Angebot, Rechnung, Lieferschein).
I want to select only those which have only Angebot and no other values.
Like you see in the picture, I only want the ProjektNummer (17011) where the DokumentType = Angebot and there no other entries for other values of DokumentType.
So it should NOT select ProjektNummmer = 17016 because there are other entries with different values.
I hope you know what I mean.
I already tried if conditions and other stuff but I can't get it done.
Thanks for your help
You can use NOT EXISTS :
SELECT t.*
FROM table t
WHERE NOT EXISTS (SELECT 1
FROM table t1
WHERE t1.ProjektNummer = t.ProjektNummer AND
t1.DokumentType <> t.DokumentType
)
AND t.DokumentType = 'Angebot';
As I read the question, you only want ProjektNummers that meet the criteria -- not the individual rows. If so, then aggregation is a simple solution:
select ProjektNummer
from t
group by ProjektNummer
having min(DokumentType) = max(DokumentType) and
min(DokumentType) = 'Angebot'
Given the following dataset in the temporary table #RawAlarms:
I need to return the first instance of each AlarmState='UNACK_ALM', inline with the very next instance of the AlarmState 'ACK_ALM' OR 'ACK_RTN' but NOT BOTH.
The query I am running looks like:
SELECT RA1.TagName, RA1.DateTime, RA1.Description, RA2.Description, MIN(RA2.DateTime) as AckDT
FROM #RawAlarms RA1
INNER JOIN #RawAlarms RA2 ON RA1.TagName = RA2.TagName AND RA2.DateTime > RA1.DateTime
AND RA1.LotStartDT = RA2.LotStartDT and RA1.LotEndDT = RA2.LotEndDT
WHERE RA1.AlarmState = 'UNACK_ALM' AND RA2.AlarmState IN('ACK_RTN','ACK_ALM')
GROUP BY RA1.TagName, RA1.DateTime, RA1.Description, RA2.Description
ORDER BY RA1.TagName, RA1.DateTime
These are the results I'm getting; highlighted in Red are an example of the results I need:
HELP
I think the APPLY operator can help here:
SELECT RA1.TagName, RA1.DateTime, RA1.Description, NextAlarmState.Description, NextAlarmState.DateTime AS AckDT
FROM #RawAlarms RA1
CROSS APPLY (SELECT TOP 1 RA2.Description, RA2.DateTime
FROM #RawAlarms RA2
WHERE RA2.LotStartDT = RA1.LotStartDT
AND RA2.LotEndDT = RA1.LotEndDT
AND RA2.DateTime > RA1.DateTime
AND RA2.AlarmState IN ('ACK_RTN', 'ACK_ALM')
ORDER BY RA2.DateTime) AS NextAlarmState
WHERE RA1.AlarmState = 'UNACK_ALM'
ORDER BY RA1.TagName, RA1.DateTime
is there anyway to get the following results from query without joining the same table three times (or) without reading the same "wordlocation" table three times (or more if there are more words)? If there are three or more words, it takes about over a minute for the results to be returned.
Currently "wordlocation" table has three rows being ("bookid","wordid","location") and it currently has 917802 rows.
What I am trying to do is
retrieve the "bookid" that contains all the words specified in the query by "wordid".
sum word count of all words (from the query) from each book
minimum values of each word location, e.g. (min(w0.location), min (w1.location)
I have tried commenting out count(w0.wordid) and min(location) calculations to see whether they are affecting the performance but this is not the case. Joining the same table multiple time was the case.
(this is the same code as the above image)
select
w0.bookid,
count(w0.wordid) as wcount,
abs(min(w0.location) + min(w1.location) + min(w2.location)) as wordlocation,
(abs(min(w0.location) - min(w1.location)) + abs(min(w1.location) - min(w2.location))) as distance
from
wordlocation as w0
inner join
wordlocation as w1 on w0.bookid = w1.bookid
join
wordlocation as w2 on w1.bookid = w2.bookid
where
w0.wordid =3
and
w1.wordid =52
and
w2.wordid =42
group by w0.bookid
order by wcount desc;
This is the result that I am looking for, and which I got from running the above query, but it takes too long if I specify more than 3 words, e.g. (w0 = 3, w1 = 52 , w2 = 42, w3 = 71)
Try this query
SELECT bookid,
ABS(L3+L52+L42) as wordlocation,
ABS(L3-L52)+ABS(L52-L42) as distance
FROM
(SELECT bookid, wordid, CASE WHEN wordid=3 THEN min(location) ELSE 0 END L3,
CASE WHEN wordid=52 THEN min(location) ELSE 0 END L52,
CASE WHEN wordid=42 THEN min(location) ELSE 0 END L42
FROM wordlocation WL
WHERE wordid in (3,52,42)
GROUP BY bookid, wordid) T
GROUP BY bookid
You may also need to create index on wordid
In sqlanywhere 12 I wrote the following query which returns two rows of data:
SELECT "eDatabase"."Vendor"."VEN_CompanyName", "eDatabase"."OrderingInfo"."ORD_Timestamp"
FROM "eDatabase"."OrderingInfo"
JOIN "eDatabase"."Vendor"
ON "eDatabase"."OrderingInfo"."ORD_VEN_FK" = "eDatabase"."Vendor"."VEN_PK"
WHERE ORD_INV_FK='7853' AND ORD_DefaultSupplier = 1
Which returns:
'**United Natural Foods IN','2018-02-07 15:05:15.513'
'Flora ','2018-02-07 14:40:07.491'
I would like to only return the row with the maximum timestamp in the column "ORD_Timestamp". After simply trying to select by MAX("eDatabase"."OrderingInfo"."ORD_Timestamp") I found a number of posts describing how that method doesn't work and to use a subquery to obtain the results.
I'm having difficulty creating the subquery in a way that works and with the following query I'm getting a syntax error on my last "ON":
SELECT "eDatabase"."Vendor"."VEN_CompanyName", "eDatabase"."OrderingInfo"."ORD_Timestamp"
FROM ( "eDatabase"."OrderingInfo"
JOIN
"eDatabase"."OrderingInfo"
ON "eDatabase"."Vendor"."VEN_PK" = "eDatabase"."OrderingInfo"."ORD_VEN_FK" )
INNER JOIN
(SELECT "eDatabase"."Vendor"."VEN_CompanyName", MAX("eDatabase"."OrderingInfo"."ORD_Timestamp")
FROM "eDatabase"."OrderingInfo")
ON "eDatabase"."Vendor"."VEN_PK" = "eDatabase"."OrderingInfo"."ORD_VEN_FK"
WHERE ORD_INV_FK='7853' AND ORD_DefaultSupplier = 1
Does anyone know how I can adjust this to make the query correctly select only the max ORD_Timestamp row?
try this:
SELECT TOP 1 "eDatabase"."Vendor"."VEN_CompanyName", "eDatabase"."OrderingInfo"."ORD_Timestamp"
FROM "eDatabase"."OrderingInfo"
JOIN "eDatabase"."Vendor"
ON "eDatabase"."OrderingInfo"."ORD_VEN_FK" = "eDatabase"."Vendor"."VEN_PK"
WHERE ORD_INV_FK='7853' AND ORD_DefaultSupplier = 1
order by "ORD_Timestamp" desc
this orders them biggest on to and say only hsow the top row