How to get rid of "the expression you entered exceeds the1,024-character limit for the query design - ms-access
How to get rid of "the expression you entered exceeds the 1,024-character limit for the query design? As I would need to extract the huge data based on the query in the access below.
Please see my query below. Any help is appreciated.
SELECT dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.Year, dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.Month, dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.Day, dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.PERIOD, dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.CONFIGURATIONNAME, dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.CONNECTIONMEMBERNAME, dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.VALUE, dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID, dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.PRICINGRUNGROUPCODE, DateValue([dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING]![STARTDATE]) AS STARTDATE
FROM dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING, Input_BP
WHERE (((dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.PRICINGRUNGROUPCODE)="BP1B") AND ((DateValue([dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING]![STARTDATE])) Between #4/1/2021# And #4/30/2021#))
GROUP BY dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.Year, dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.Month, dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.Day, dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.PERIOD, dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.CONFIGURATIONNAME, dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.CONNECTIONMEMBERNAME, dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.VALUE, dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID, dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.PRICINGRUNGROUPCODE, DateValue([dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING]![STARTDATE])
HAVING (((dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300140218" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300140226" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300140952" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300310001" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300313005" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300352052" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300469013" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300473072" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300482024" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300582096" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300582120" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300602001" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300629020" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300654028" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300670016" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300761021" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300774099" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300786051" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300786069" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300786085" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300843142" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300894517" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300897973" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300897981" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300964005" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300992725" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300992733" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300992741" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301001294" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301001302" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301001310" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301001328" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301003407" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301003464" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301003472" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301010147" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301010154" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301030434" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301030558" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301035045" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301035052" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301035060" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301035144" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301035151" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301035169" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301035276" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301035284" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301035300" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301035318" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301035326" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301035334" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301036027" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301040052" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301047008" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301049269" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301049293" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301049301" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301049319" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301049343" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301064003" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301128006" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301176898" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301218849" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301344538"));
Thanks
Using an alias for the table name and replacing OR col = val1 OR col = val1 OR col2 = val ... with col in (val1, val2, val3, ...) will save you a lot of characters. It will also make the code a lot easier to understand. I also removed the reference to Input_BP, which did not seem to be contributing anything except the possibility of a ton of duplicate results.
SELECT T.Year,
T.Month,
T.Day,
T.PERIOD,
T.CONFIGURATIONNAME,
T.CONNECTIONMEMBERNAME,
T.VALUE,
T.MSSLID,
T.PRICINGRUNGROUPCODE,
DateValue([T]![STARTDATE]) AS STARTDATE
FROM dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING As T
WHERE (((T.PRICINGRUNGROUPCODE)="BP1B")
AND ((DateValue([T]![STARTDATE])) Between #4/1/2021# And #4/30/2021#))
GROUP BY T.Year,
T.Month,
T.Day,
T.PERIOD,
T.CONFIGURATIONNAME,
T.CONNECTIONMEMBERNAME,
T.VALUE,
T.MSSLID,
T.PRICINGRUNGROUPCODE,
DateValue([T]![STARTDATE])
HAVING (T.MSSLID IN ("9300140218", ...));
If you want to be able to access the query design builder, I recommend copying and pasting maybe half of the repeated fields towards the bottom to a note/word file so you can delete some of the fields in SQL? And I'm not sure if this is for a report or a form, but try to use VBA to reduce the query; or maybe use the function Right() for the field MSSLID so you don't have to write the first "9300"?
(This is just a reduced version of your code; I'm not sure if you're making a report or form or if you just wanted to make a query)
SELECT dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.Year, dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.Month, dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.Day, dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.PERIOD, dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.CONFIGURATIONNAME, dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.CONNECTIONMEMBERNAME, dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.VALUE, dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID, dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.PRICINGRUNGROUPCODE, DateValue([dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING]![STARTDATE]) AS STARTDATE FROM dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING, Input_BP WHERE (((dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.PRICINGRUNGROUPCODE)="BP1B") AND ((DateValue([dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING]![STARTDATE])) Between #4/1/2021# And #4/30/2021#)) GROUP BY dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.Year, dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.Month, dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.Day, dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.PERIOD, dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.CONFIGURATIONNAME, dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.CONNECTIONMEMBERNAME, dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.VALUE, dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID, dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.PRICINGRUNGROUPCODE, DateValue([dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING]![STARTDATE]) HAVING (((dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300140218" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300140226" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300140952" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300310001" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300313005" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300352052" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300469013" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300473072" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300482024" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300582096" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300582120" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300602001" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300629020" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300654028" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300670016" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300761021" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300774099" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300786051"));
Let me know if this helps!
Related
KDB type error - whilst using functional select/updates
I have the below two functions - the idea here is that id like to create percentiles of bpsAum for different programmes, i also have another function for bpsTrn, id like the limits table to be one single function that both can share. This however, returns a type error, I think it has to do with me using functional selects and updates within the functions. would appreciate any advice here. Outside of the function, running line by line this works fine, just not when packaged into function. .tca.dailySlippageDistribution4:{[Dte;lookback;thresh] tab:select bpsAum:(sum ArrivalSlippageUSD % first TradingLevel),sum TurnoverUSD by ProgrammeName,date from fullSlipTable where date within(Dte - 1500;Dte); prog: exec distinct ProgrammeName from tab; dtes: exec distinct date from tab; Dte2:Dte - lookback; Dte2:?[Dte2 in dtes;Dte2;?[(Dte2-1) in dtes;Dte2:Dte2-1;?[(Dte2-2) in dtes;Dte2:(Dte2-2);Dte2]]]; Dte:?[Dte in dtes;Dte;?[(Dte-1) in dtes;Dte:Dte-1;?[(Dte-2) in dtes;Dte:(Dte-2);Dte]]]; st:dtes?Dte; ed:dtes?Dte2; distwindow:(st-ed)-1; metric:`bpsAum; maW:0; Limits:(uj/){[tab;thresh;distwindow;metric;maW;x] LimitsTable4[tab;thresh;distwindow;metric;maW;x]}[tab;thresh;distwindow;metric;maW;] each prog; tab:ej[`date`ProgrammeName;tab;Limits]; tab}; LimitsTable4:{[tab;thresh;distwindow;metric;maW;prog] tab: select from tab where ProgrammeName = prog; tab:$[metric ~ `bpsTrn;![tab;();0b;(enlist `maRunRate)!(enlist (mavg;maW;metric))];tab]; dateList: exec date from tab; LList:$[`maRunRate in cols tab;?[tab;();();`maRunRate];?[tab;();();`metric]]; ltk:`float$swin[.dashFn.getPercentile[thresh(1);];distwindow;LList]; utk:`float$swin[.dashFn.getPercentile[thresh(0);];distwindow;LList]; tab:?[metric ~ `bpsAum;([]date:dateList;ProgrammeName:prog;bpsAum:LList;upperLimit:utk;lowerLimit:ltk);([]date:dateList;ProgrammeName:prog;maBpsTrn:LList;upperLimit:utk;lowerLimit:ltk)]; tab:(-1*distwindow)# tab};
MySQL counting results from max()
I have a MySQL table that tracks inventory scans in and out of the location. Example data like this: bar_code_id,scan_type,scan_timestamp 038755,in,"2020-03-19 16:58:18" 038755,out,"2020-03-10 12:51:47" 001548,in,"2020-03-06 14:50:41" 012170,in,"2020-03-06 14:50:28" 020166,in,"2020-03-06 14:49:48" 022908,in,"2020-03-06 14:49:07" 012169,out,"2020-03-02 11:48:43" 020182,out,"2020-03-02 11:48:24" 047270,in,"2020-01-29 16:54:18" 020166,out,"2020-01-20 11:43:44" 012170,out,"2020-01-20 11:43:32" 022908,out,"2020-01-20 11:43:23" 001548,out,"2020-01-20 11:43:16" 016852,in,"2020-01-20 09:42:07" 020182,in,"2020-01-20 09:42:02" 012172,out,"2020-01-14 12:56:00" 012172,in,"2020-01-14 11:06:22" 001548,in,"2020-01-14 10:46:47" 010606,in,"2020-01-14 10:46:46" 012169,in,"2020-01-14 10:46:45" 020166,in,"2020-01-14 10:46:45" 010054,in,"2020-01-13 14:59:50" 012167,in,"2020-01-13 14:58:33" 020175,out,"2020-01-09 12:16:16" 023260,in,"2020-01-07 12:01:12" 038755,in,"2020-01-06 09:15:23" I'm trying to create a query that will show the current number inside the building. I am able to find the latest scan of each asset using the following: select bar_code_id, max(scan_timestamp) from scans group by bar_code_id order by bar_code_id Giving the following results: bar_code_id,timestamp 001548,"2020-03-06 14:50:41" 010054,"2020-01-13 14:59:50" 010606,"2020-01-14 10:46:46" 012167,"2020-01-13 14:58:33" 012169,"2020-03-02 11:48:43" 012170,"2020-03-06 14:50:28" 012172,"2020-01-14 12:56:00" 016852,"2020-01-20 09:42:07" 020166,"2020-03-06 14:49:48" 020175,"2020-01-09 12:16:16" 020182,"2020-03-02 11:48:24" 022908,"2020-03-06 14:49:07" 023260,"2020-01-07 12:01:12" 038755,"2020-03-19 16:58:18" 047270,"2020-01-29 16:54:18" But can't work out how to show what the scan type is too. Ideally I would like to be able to count the number of items where the last scan was 'in' and also the total number of items. Any pointers? edit I'm trying to only look at the most recent scan for each bar_code and then count the number where the scan_type is 'in' So the latest scan for each bar_code is: bar_code_id,scan_type,scan_timestamp 001548,in,2020-03-06 14:50:41 010054,in,2020-01-13 14:59:50 010606,in,2020-01-14 10:46:46 012167,in,2020-01-13 14:58:33 012169,out,2020-03-02 11:48:43 012170,in,2020-03-06 14:50:28 012172,out,2020-01-14 12:56:00 016852,in,2020-01-20 09:42:07 020166,in,2020-03-06 14:49:48 020175,out,2020-01-09 12:16:16 020182,out,2020-03-02 11:48:24 022908,in,2020-03-06 14:49:07 023260,in,2020-01-07 12:01:12 038755,in,2020-03-19 16:58:18 047270,in,2020-01-29 16:54:18 And then the ones that were type 'in' are: bar_code_id,scan_type,scan_timestamp 001548,in,2020-03-06 14:50:41 010054,in,2020-01-13 14:59:50 010606,in,2020-01-14 10:46:46 012167,in,2020-01-13 14:58:33 012170,in,2020-03-06 14:50:28 016852,in,2020-01-20 09:42:07 020166,in,2020-03-06 14:49:48 022908,in,2020-03-06 14:49:07 023260,in,2020-01-07 12:01:12 038755,in,2020-03-19 16:58:18 047270,in,2020-01-29 16:54:18 Giving an answer of 11. My sql query using max(scan_timestamp) does not include the scan_type so this cannot be counted. If i try to include this then the max function shows more than just the latest scan select bar_code_id, max(scan_timestamp) as timestamp, scan_type from scans group by bar_code_id, scan_type bar_code_id,timestamp,scan_type 038755,"2020-03-19 16:58:18",in 020166,"2020-01-20 11:43:44",out 012170,"2020-01-20 11:43:32",out 022908,"2020-01-20 11:43:23",out 001548,"2020-01-20 11:43:16",out 016852,"2020-01-20 09:42:07",in 020182,"2020-01-20 09:42:02",in 012172,"2020-01-14 12:56:00",out 012172,"2020-01-14 11:06:22",in 001548,"2020-03-06 14:50:41",in 010606,"2020-01-14 10:46:46",in 038755,"2020-03-10 12:51:47",out 012169,"2020-01-14 10:46:45",in 020166,"2020-03-06 14:49:48",in 010054,"2020-01-13 14:59:50",in 012167,"2020-01-13 14:58:33",in 020175,"2020-01-09 12:16:16",out 023260,"2020-01-07 12:01:12",in 012170,"2020-03-06 14:50:28",in 022908,"2020-03-06 14:49:07",in 012169,"2020-03-02 11:48:43",out 020182,"2020-03-02 11:48:24",out 047270,"2020-01-29 16:54:18",in
I don't know if this is really what you want, but I have this request: SELECT bar_code_id, MAX(scan_timestamp) as timetamps, scan_type, COUNT(scan_type) as number_of_items, (SELECT COUNT(id) from scans) as total FROM scans WHERE scan_type = 'in' GROUP BY bar_code_id ORDER BY bar_code_id Here is the link where I tested this request: http://sqlfiddle.com/#!9/0da168/19 I'm available if you have a question about my answer.
MySQL referencing, to avoid repeating myself?
I have this snippet: SELECT CASE WHEN AVG(UNIX_TIMESTAMP(tDone)-UNIX_TIMESTAMP(tIPN))/3600 >= 10 THEN ROUND(AVG(UNIX_TIMESTAMP(tDone)-UNIX_TIMESTAMP(tIPN))/3600,0) ELSE ROUND(AVG(UNIX_TIMESTAMP(tDone)-UNIX_TIMESTAMP(tIPN))/3600,1) END FROM ... Can I do anything to remove the duplication from this? Something along these lines, for instance: (Hypothetical code follows): SET var = AVG(UNIX_TIMESTAMP(tDone)-UNIX_TIMESTAMP(tIPN))/3600 SELECT CASE WHEN var > 10 THEN ROUND(var,0) ELSE ROUND(var,1) END FROM ...
With a subquery you can do something like this : SELECT CASE WHEN avgtiPN >= 10 THEN ROUND(avgtiPN,0) ELSE ROUND(avgtiPN,1) END FROM (SELECT AVG(UNIX_TIMESTAMP(tDone)-UNIX_TIMESTAMP(tIPN))/3600 AS avgtiPN FROM ...) AS AVGQuery But I am still uncertain if it is more readable.
Yes, you can, but variable processing order is undefined for user-defined variables. This reference in the MySQL documentation explains when this works and when it doesnt.
Webmatrix if statement issues. any ideas?
I have written a SQL query, which extracts the lowest weekly price for a property stored in my database: var rPropertyId = Request.QueryString["PropertyID"]; var cheapestrate = "SELECT TOP 1 * FROM RateInfo WHERE PropertyID=#0 ORDER BY RateWeekly ASC"; var qcheapestrate = db.QuerySingle (cheapestrate, rPropertyId); I'm pretty confident that this statement is correct. The problem i have, is that not ALL propertys have pricing, so i only want to show this price if they do. I have created the below if statement, but it's telling me i'm missing an ; somewhere? #if(qcheapestrate=!null){ Rates From qcheapestrate.rateweekly per week } So i'm trying to check if the query returns an entry. if it does, i want to show the lowest "rateweekly" value. Hopefully this all makes sense!
Try this... #if(qcheapestrate!=null){ <text>Rates From</text> #qcheapestrate.rateweekly <text>per week</text> }
MySQL - Perl: How to get array of zip codes within submitted "x" miles of submitted "zipcode" in Perl example
I have found many calculations here and some php examples and most are just over my head. I found this example: SELECT b.zip_code, b.state, (3956 * (2 * ASIN(SQRT( POWER(SIN(((a.lat-b.lat)*0.017453293)/2),2) + COS(a.lat*0.017453293) * COS(b.lat*0.017453293) * POWER(SIN(((a.lng-b.lng)*0.017453293)/2),2))))) AS distance FROM zips a, zips b WHERE a.zip_code = '90210' ## I would use the users submitted value GROUP BY distance having distance <= 5; ## I would use the users submitted value But, I am having trouble understanding how to implement the query with my database. It looks like that query has all I need. However, I cannot even find/understand what b.zip_code actually is! (whats the b. and zips a, zips b?) I also do not need the state in the query. My mySQL db structure is like this: ZIP | LAT | LONG 33416 | 26.6654 | -80.0929 I wrote this in attempt to return some kind of results (not based on above query) but, it only kicks out one zip code. ## Just for a test BUT, in reality I desire to SELECT a zip code WHERE ZIP = the users submitted zip code ## not by a submitted lat lon. I left off the $connect var, assume it's there. my $set1 = (26.6654 - 0.20); my $set2 = (26.6654 + 0.20); my $set3 = (-80.0929 - 0.143); my $set4 = (-80.0929 + 0.143); my $test123 = $connect->prepare(qq{SELECT `ZIP` FROM `POSTAL` WHERE `LAT` >= ? AND `LAT` <= ? AND `LONG` >= ? AND `LONG` <= ?}) or die "$DBI::errstr"; $test123->execute("$set1","$set2","$set3","$set4") or die "$DBI::errstr"; my $cntr; while(#zip = $test123->fetchrow_array()) { print qq~$zip[$cntr]~; push(#zips,$zip[$cntr]); $cntr++; } As you can see, I am quite the novice so, I need some hand holding here with verbose explanation. So, in Perl, how can I push zip codes into an array from a USER SUBMITTED ZIP CODE and user submitted DISTANCE in miles. Can be a square instead of a circle, not really that critical of a feature. Faster is better.
I'll tackle the small but crucial part of the question: However, I cannot even find/understand what b.zip_code actually is! (whats the "b." and "zips a, zips b"?) Basically, the query joins two tables. BUT, both tables being joined are in fact the same table - "zips" (in other words, it joins "zips" table to itself"). Of course, since the rest of the query needs to understand when you are referring to the first copy of the "zips" table and when to the second copy of the "zips" table, you are giving a table alias to each copy - to wit, "a" and "b"'. So, "b.xxx" means "column xxx from table zips, from the SECOND instance of that table being joined".
I don't see what's wrong with your first query. You have latitude and longitude in your database (if I'm understanding, you're comparing a single entry to all others). You don't need to submit or return the state that's just part of the example. Make the first query work like this: my $query = "SELECT b.zip_code, (3956 * (2 * ASIN(SQRT( POWER(SIN(((a.lat-b.lat)*0.017453293)/2),2) + COS(a.lat*0.017453293) * COS(b.lat*0.017453293) * POWER(SIN(((a.lng-b.lng)*0.017453293)/2),2))))) AS distance FROM zips a, zips b WHERE a.zip_code = ? GROUP BY distance having distance <= ?"; my $sth = $dbh->prepare($query); $sth->execute( $user_submitted_zip, $user_submitted_distance ); while( my ($zip, $distance) = $sth->fetchrow() ) ) { # do something } This won't be that fast, but if you have a small record set ( less than 30k rows ) it should be fine. If you really want to go faster you should look into a search engine such as Sphinx which will do this for you.
fetchrow_array returns a list of list references, essentially a two-dimensional array, where each row represents a different result from the database query and each column represents a field from the query (in your case, there is only one field, or column, per row). Calling while ($test123->fetchrow_array()) will cause an infinite loop as your program executes the query over and over again. If the query returns results, then the while condition will be satisfied and the loop will repeat. The usual idiom would be to say something more like for my $row ($test123->fetchrow_array()) { ..., which will only execute the query once and then iterate over the results. Each result is a list reference, and the zip code you are interested in is in the first (and only) column, so you could accumulate the results in an array like this: my #zips = (); # for final results for my $row ($test123->fetchrow_array()) { push #zips, $row->[0]; } or even more concisely with Perl's map statement: my #zips = map { $_->[0] } $test123->fetchrow_array() which does the same thing.