When i execute the below sql with error, who knows how to resolve it? Thanks a lot.
error message:
class io.confluent.ksql.execution.expression.tree.LogicalBinaryExpression cannot be cast to class io.confluent.ksql.execution.expression.tree.ComparisonExpression (io.confluent.ksql.execution.expression.tree.LogicalBinaryExpression and io.confluent.ksql.execution.expression.tree.ComparisonExpression are in unnamed module of loader 'app')
select
a.mo_order_id,
a.mo_lot_no,
b.tenant_id,
a.line_id,
substring((TIMESTAMPTOSTRING(a.creation_time, 'yyyy-MM-dd HH:mm:ss', 'GMT')),1,10) as produce_date,
substring(substring((TIMESTAMPTOSTRING(a.creation_time, 'yyyy-MM-dd HH:mm:ss', 'GMT')),12,12),1,2) as produce_hour,
(
case
when a.result_code =1 then 1
when a.result_code =3 then 1
when a.result_code =8 then 1
when a.result_code =2 then 2
else 0
end) cause_type,
SUM((
case
when ((a.result_code =1) and is_reset = false) then sn_count
when ((a.result_code =3) and is_reset = false) then sn_count
when ((a.result_code =8) and is_reset = false) then sn_count
when ((a.result_code =1) and is_reset = true) then -1*sn_count
when (a.result_code = 2) then sn_count else 0
end)) stats
from
STREAM_ORI_SACMES_INSPECTION a
inner join KSQL_TABLE_GP_MO_ORDER b on
(a.mo_order_id = b.id and (a.result_code =1 or a.result_code =3 or a.result_code =8))
group by
a.mo_order_id,
a.mo_lot_no,
b.tenant_id,
a.line_id,
substring((TIMESTAMPTOSTRING(creation_time, 'yyyy-MM-dd HH:mm:ss', 'GMT')),1,10),
substring(substring((TIMESTAMPTOSTRING(creation_time, 'yyyy-MM-dd HH:mm:ss', 'GMT')),12,12),1,2),
(
case
when a.result_code =1 then 1
when a.result_code =3 then 1
when a.result_code =8 then 1
when a.result_code =2 then 2
else 0
end)
emit changes;
Humm... looks like a bug!
The issue is that your join criteria is invalid: (a.mo_order_id = b.id and (a.result_code =1 or a.result_code =3 or a.result_code =8)).
Looks like your join criteria should be a.mo_order_id = b.id, and the other bits should be moved to a WHERE clause.
a.mo_order_id,
a.mo_lot_no,
b.tenant_id,
a.line_id,
substring((TIMESTAMPTOSTRING(a.creation_time, 'yyyy-MM-dd HH:mm:ss', 'GMT')),1,10) as produce_date,
substring(substring((TIMESTAMPTOSTRING(a.creation_time, 'yyyy-MM-dd HH:mm:ss', 'GMT')),12,12),1,2) as produce_hour,
(
case
when a.result_code =1 then 1
when a.result_code =3 then 1
when a.result_code =8 then 1
when a.result_code =2 then 2
else 0
end) cause_type,
SUM((
case
when ((a.result_code =1) and is_reset = false) then sn_count
when ((a.result_code =3) and is_reset = false) then sn_count
when ((a.result_code =8) and is_reset = false) then sn_count
when ((a.result_code =1) and is_reset = true) then -1*sn_count
when (a.result_code = 2) then sn_count else 0
end)) stats
from
STREAM_ORI_SACMES_INSPECTION a
inner join KSQL_TABLE_GP_MO_ORDER b on
a.mo_order_id = b.id
where
a.result_code =1 or a.result_code =3 or a.result_code =8
group by
a.mo_order_id,
a.mo_lot_no,
b.tenant_id,
a.line_id,
substring((TIMESTAMPTOSTRING(creation_time, 'yyyy-MM-dd HH:mm:ss', 'GMT')),1,10),
substring(substring((TIMESTAMPTOSTRING(creation_time, 'yyyy-MM-dd HH:mm:ss', 'GMT')),12,12),1,2),
(
case
when a.result_code =1 then 1
when a.result_code =3 then 1
when a.result_code =8 then 1
when a.result_code =2 then 2
else 0
end)
emit changes;
join ksqldb
Related
I cant for the life of me figure out why the below active record relation returns a different result if you run count on the relation directly as opposed to mapping over the relation and then counting the result.
Shouldn't they be the same? Does anyone know what is going on?
ActiveRecord::Base.connection.query_cache.clear
# => {}
Panel.connection.schema_cache.clear!
# => nil
Panel.reset_column_information
# => nil
#filtered_skew_panels[agglo_code].map(&:panel_id).count
# => 57
#filtered_skew_panels[agglo_code].all.count
(13.1ms) SELECT COUNT(count_column) FROM (SELECT 1 AS count_column FROM "panels" WHERE "panels"."agglo_code_id" = $1 AND "panels"."environment_id" = $2 AND "panels"."product_id" = $3 AND "panels"."alcohol_friendly" = $4 AND "panels"."suburb" = 'Marrickville' AND (NOT EXISTS(SELECT 1 FROM campaign_search_panels WHERE campaign_search_panels.panel_id = panels.panel_id AND campaign_search_panels.campaign_id = 52)) AND (NOT EXISTS(SELECT 1 FROM "AIDAAU_Avails" WHERE "AIDAAU_Avails"."PanelID" = panels.panel_uid AND "AIDAAU_Avails"."TillDate" >= '2017-08-21 00:00:00.000000' AND "AIDAAU_Avails"."FromDate" <= '2017-09-03 00:00:00.000000')) LIMIT 100) subquery_for_count [["agglo_code_id", 4], ["environment_id", 2], ["product_id", 1], ["alcohol_friendly", "t"]]
# => 0
Shouldn't #filtered_skew_panels[agglo_code].map(&:panel_id).count be equal to #filtered_skew_panels[agglo_code].count?
The relation itself seems to actually contain all the records:
[3] pry(#<PanelSearch>)> #filtered_skew_panels[agglo_code]
=> [#<Panel:0x007fe4ef779990
location_type: "Street",
move_id: "27779",
panel_id: "11441A1",
address: "Victoria Rd N/O Sydenham Rd E/S",
postal_code: "2204",
suburb: "Marrickville",
latitude: #<BigDecimal:7fe4ee0b14b0,'-0.33909329E2',18(27
[5] pry(#<PanelSearch>)> #filtered_skew_panels[agglo_code].class
=> Panel::ActiveRecord_Relation
[6] pry(#<PanelSearch>)> #filtered_skew_panels[agglo_code].to_sql
=> "SELECT \"panels\".* FROM \"panels\" WHERE \"panels\".\"agglo_code_id\" = 4 AND \"panels\".\"environment_id\" = 2 AND \"panels\".\"product_id\" = 1 AND \"panels\".\"alcohol_friendly\" = 't' AND \"panels\".\"suburb\" = 'Marrickville' AND (NOT EXISTS(SELECT 1 FROM campaign_search_panels WHERE campaign_search_panels.panel_id = panels.panel_id AND campaign_search_panels.campaign_id = 52)) AND (NOT EXISTS(SELECT 1 FROM \"AIDAAU_Avails\" WHERE \"AIDAAU_Avails\".\"PanelID\" = panels.panel_uid AND \"AIDAAU_Avails\".\"TillDate\" >= '2017-08-21 00:00:00.000000' AND \"AIDAAU_Avails\".\"FromDate\" <= '2017-09-03 00:00:00.000000')) LIMIT 500"
[7] pry(#<PanelSearch>)> #filtered_skew_panels[agglo_code].all.to_sql
=> "SELECT \"panels\".* FROM \"panels\" WHERE \"panels\".\"agglo_code_id\" = 4 AND \"panels\".\"environment_id\" = 2 AND \"panels\".\"product_id\" = 1 AND \"panels\".\"alcohol_friendly\" = 't' AND \"panels\".\"suburb\" = 'Marrickville' AND (NOT EXISTS(SELECT 1 FROM campaign_search_panels WHERE campaign_search_panels.panel_id = panels.panel_id AND campaign_search_panels.campaign_id = 52)) AND (NOT EXISTS(SELECT 1 FROM \"AIDAAU_Avails\" WHERE \"AIDAAU_Avails\".\"PanelID\" = panels.panel_uid AND \"AIDAAU_Avails\".\"TillDate\" >= '2017-08-21 00:00:00.000000' AND \"AIDAAU_Avails\".\"FromDate\" <= '2017-09-03 00:00:00.000000')) LIMIT 500"
I have a SQL Server database and I am trying to pull specific data. I need a count of all the non-null columns in each row, a subtraction of one column from another, and data from other table columns (joins).
This is where I am, could someone please look at the code and tell me what I am doing wrong (ignore the hard-coded dates, they are there solely for testing)?
SELECT
((CASE WHEN TC.Time0 IS NOT NULL THEN 1 ELSE 0 END)
+ (CASE WHEN TC.Time1 IS NOT NULL THEN 1 ELSE 0 END)
+ (CASE WHEN TC.Time2 IS NOT NULL THEN 1 ELSE 0 END)
+ (CASE WHEN TC.Time3 IS NOT NULL THEN 1 ELSE 0 END)
+ (CASE WHEN TC.Time4 IS NOT NULL THEN 1 ELSE 0 END)
+ (CASE WHEN TC.Time5 IS NOT NULL THEN 1 ELSE 0 END)
+ (CASE WHEN TC.Time6 IS NOT NULL THEN 1 ELSE 0 END)
+ (CASE WHEN TC.Time7 IS NOT NULL THEN 1 ELSE 0 END)
+ (CASE WHEN TC.Time8 IS NOT NULL THEN 1 ELSE 0 END)
+ (CASE WHEN TC.Time9 IS NOT NULL THEN 1 ELSE 0 END)) AS [Time Punches]
,SUM(CASE WHEN TC.Odometer0 IS NOT NULL THEN 1 ELSE 0 END) AS MileageStart
,SUM(CASE WHEN TC.Odometer1 IS NOT NULL THEN 1 ELSE 0 END) AS MileageEnd
,SUM(CASE WHEN MileageEnd >= 0 THEN 1 ELSE 0 END) -
SUM(CASE WHEN MileageStart < 0 THEN 1 ELSE 0 END) AS [Total Miles]
,D.DriverID AS [Driver ID]
,W.FirstName +' '+W.LastName AS [Driver Name]
,TC.PunchDate AS [DATE]
FROM tblTimeClock TC WITH (NOLOCK)
INNER JOIN tblDrivers D WITH (NOLOCK)
ON D.DriverID = TC.PunchID
INNER JOIN tblWorker W WITH (NOLOCK)
ON W.WorkerID = D.DriverID
WHERE TC.PunchID IS NOT NULL
AND TC.PunchDate BETWEEN '2017-05-01' AND '2017-06-01'
ORDER BY TC.PunchDate
With the above I am getting this error:
> Column 'tblTimeClock.Time0' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
But I do not know how to include this in a GROUP BY clause - every time I try it causes other errors to pop up (different, depending on where I place the clause).
The reason I asked if someone can "tell me what I am doing wrong" is so that once I fix what is broken, I don't have to come back and say "help" again and again for each subsequent issue. I know the code is bad, this is why I need help.
I figured it out on my own:
SELECT
TC.PunchDate AS [Date]
,D.DriverID AS [Driver ID]
,W.FirstName +' '+W.LastName AS [Driver Name]
,((CASE WHEN TC.Time0 IS NOT NULL THEN 1 ELSE 0 END)
+ (CASE WHEN TC.Time1 IS NOT NULL THEN 1 ELSE 0 END)
+ (CASE WHEN TC.Time2 IS NOT NULL THEN 1 ELSE 0 END)
+ (CASE WHEN TC.Time3 IS NOT NULL THEN 1 ELSE 0 END)
+ (CASE WHEN TC.Time4 IS NOT NULL THEN 1 ELSE 0 END)
+ (CASE WHEN TC.Time5 IS NOT NULL THEN 1 ELSE 0 END)
+ (CASE WHEN TC.Time6 IS NOT NULL THEN 1 ELSE 0 END)
+ (CASE WHEN TC.Time7 IS NOT NULL THEN 1 ELSE 0 END)
+ (CASE WHEN TC.Time8 IS NOT NULL THEN 1 ELSE 0 END)
+ (CASE WHEN TC.Time9 IS NOT NULL THEN 1 ELSE 0 END)) AS [Time Punches]
,TC.Odometer0 AS [Starting Mileage]
,TC.Odometer1 AS [Ending Mileage]
,SUM(CASE WHEN TC.Odometer1 IS NOT NULL AND TC.Odometer1 >= 0 THEN TC.Odometer1 ELSE 0 END) -
SUM(CASE WHEN TC.Odometer0 IS NOT NULL AND TC.Odometer0 >= 0 THEN TC.Odometer0 ELSE 0 END) AS [Total Miles]
FROM tblTimeClock TC WITH (NOLOCK)
INNER JOIN tblDrivers D WITH (NOLOCK)
ON CAST(D.DriverID AS VARCHAR(50)) = TC.PunchID
INNER JOIN tblWorker W WITH (NOLOCK)
ON W.WorkerID = D.DriverID
WHERE TC.PunchID IS NOT NULL
AND TC.PunchDate BETWEEN #StartDate AND #EndDate
GROUP BY TC.Time0, TC.Time1, TC.Time2, TC.Time3, TC.Time4, TC.Time5, TC.Time6, TC.Time7, TC.Time8, TC.Time9, TC.Odometer0,TC.Odometer1, D.DriverID, W.FirstName, W.LastName, TC.PunchDate
ORDER BY TC.PunchDate
create view "informix".v_prod_cost (ship_no,dc_id,whse_id,cust_id,prod_id,
unit_cost_excl,unit_cost_incl,vat_perc,is_catch_wght,tot_cost_excl,tot_cost_incl) as
SELECT x3.ship_no, x1.dc_id, x1.whse_id, x4.cust_id, x1.prod_id,
x5.case_cost, round(((x5.case_cost * (x7.cvat_desc ::smallint
+ 100. ) ) / 100.0000000000000000 ) , 2 ), x7.cvat_desc,
CASE WHEN ((x4.catch_wgt_ctl = 'Y' ) AND (x5.catch_wgt_cntl
= 'Y' ) ) THEN 'Y' WHEN ((x4.catch_wgt_ctl = 'N' ) AND (x5.catch_wgt_cntl
= 'Y' ) ) THEN 'N' ELSE 'N' END,
CASE WHEN ((x4.catch_wgt_ctl
= 'Y' ) AND (x5.catch_wgt_cntl = 'Y' ) ) THEN round(sum((select
(sum(x9.catch_wgt ) * x5.case_cost ) from "informix".icwma
x8 ,"informix".icwmad x9 where (((((((((((x8.cwma_id = x9.cwma_id
) AND (x8.dc_id = x1.dc_id ) ) AND (x8.whse_id = x1.whse_id
) ) AND (x8.curr_pal_no = x1.curr_pal_no ) ) AND (x8.ord_id
= x1.ord_id ) ) AND (x8.prod_id = x1.prod_id ) ) AND (x8.invc_id
= x1.invc_id ) ) AND (x8.sgmt_id = x1.sgmt_id ) ) AND (x8.unit_ship_cse
= x1.unit_ship_cse ) ) AND (x8.assg_id = x1.assg_id ) ) AND
(x8.ckpt_id = x1.ckpt_id ) ) ) ) , 2 ) WHEN ((x4.catch_wgt_ctl
= 'N' ) AND (x5.catch_wgt_cntl = 'Y' ) ) THEN round(sum((((x1.prod_qty
/ x1.unit_ship_cse ) * x1.prod_wgt ) * x5.case_cost ) ) ,
2 ) ELSE round(sum((x1.prod_qty * x5.case_cost ) ) , 2
) END,
CASE WHEN ((x4.catch_wgt_ctl = 'Y' ) AND (x5.catch_wgt_cntl
= 'Y' ) ) THEN round(sum((select (((sum(x11.catch_wgt ) *
x5.case_cost ) * (x7.cvat_desc ::smallint + 100. ) ) / 100.0000000000000000
) from "informix".icwma x10 ,"informix".icwmad x11 where (((((((((((x10.cwma_id
= x11.cwma_id ) AND (x10.dc_id = x1.dc_id ) ) AND (x10.whse_id
= x1.whse_id ) ) AND (x10.curr_pal_no = x1.curr_pal_no )
) AND (x10.ord_id = x1.ord_id ) ) AND (x10.prod_id = x1.prod_id
) ) AND (x10.invc_id = x1.invc_id ) ) AND (x10.sgmt_id =
x1.sgmt_id ) ) AND (x10.unit_ship_cse = x1.unit_ship_cse
) ) AND (x10.assg_id = x1.assg_id ) ) AND (x10.ckpt_id =
x1.ckpt_id ) ) ) ) , 2 ) WHEN ((x4.catch_wgt_ctl = 'N' )
AND (x5.catch_wgt_cntl = 'Y' ) ) THEN round(sum(((((x1.prod_qty
/ x1.unit_ship_cse ) * x1.prod_wgt ) * x5.case_cost ) * (x7.cvat_desc
::smallint + 100. ) ) ) , 2 ) ELSE round(sum((((x1.prod_qty
* x5.case_cost ) * (x7.cvat_desc ::smallint + 100. ) ) /
100.0000000000000000 ) ) , 2 ) END
FROM "informix".ishs x0,
"informix".ishd x1, "informix".irtst x2, "informix".crtst x3,
"informix".icust x4, "informix".iprod x5, "informix".cprod x6,
"informix".cvat x7
WHERE ((((((((((((x0.shs_id = x1.shs_id
) AND (x0.dc_id = x1.dc_id ) ) AND (x0.whse_id = x1.dc_id
) ) AND (x0.rtst_id = x2.rtst_id ) ) AND (x0.dc_id = x2.dc_id
) ) AND (x1.whse_id = x2.dc_id ) ) AND (x2.rtst_id = x3.rtst_id
) ) AND (x1.cust_id = x4.cust_id ) ) AND (x1.dc_id = x5.dc_id
) ) AND (x1.prod_id = x5.prod_id ) ) AND (x5.prod_id = x6.prod_id
) ) AND (x6.vat_ind = x7.vat_ind ) )
GROUP BY x3.ship_no, x1.dc_id, x1.whse_id, x4.cust_id, x1.prod_id,
x5.case_cost, 7, x7.cvat_desc, 9, x4.catch_wgt_ctl, x5.catch_wgt_cntl
;
I have the view above, which was created to perform the necessary calculations. The problem I am facing it takes over 5 minutes to run.
This is my sql trace output:
IBM Informix Dynamic Server Version 12.10.FC4W1WE -- On-Line -- Up 27 days 20:42:31 -- 2211344 Kbytes
Statement history:
Trace Level High
Trace Mode User
Number of traces 1000
Current Stmt ID 1
Trace Buffer size 4056
Duration of buffer 1481025737 Seconds
Trace Flags 0x0000FF21
Control Block 700000015dc1028
Statement # 1: # 700000015dc1048
Database: system
Statement text:
select * from v_prod_cost
SELECT using tables [ ishs ishd irtst crtst icust iprod cprod cvat ]
Iterator/Explain
================
ID Left Right Sender Next Est Cost Est Rows Num Rows Partnum Type
9 0 0 0 0 10054 47203 47203 7342066 Seq Scan
10 0 0 0 0 1 40977 1 7340184 Index Scan
8 9 10 0 0 35212 40977 40715 0 Nested Join
11 0 0 0 0 2 325474 6 7343766 Index Scan
7 8 11 0 0 154765 278608 303217 0 Nested Join
12 0 0 0 0 1 1280461 1 7343819 Index Scan
6 7 12 0 0 550996 1111035 1225518 0 Nested Join
13 0 0 0 0 1 152406 1 7340237 Index Scan
5 6 13 0 0 1337728 1111035 1225518 0 Nested Join
14 0 0 0 0 2 2 2 7340235 Seq Scan
4 5 14 0 0 1544660 1111032 1225518 0 Hash Join
15 0 0 0 0 1 6941 1 7343796 Index Scan
3 4 15 0 0 2084251 1111036 1225518 0 Nested Join
16 0 0 0 0 1 152627 1 7341873 Index Scan
2 3 16 0 0 3736185 1109418 1225518 0 Nested Join
1 2 0 0 0 2892742 1108253 27 0 Group
Statement information:
Sess_id User_id Stmt Type Finish Time Run Time TX Stamp PDQ
207705 55121 SELECT 14:02:16 99.8398 cd5d6ee8 0
Statement Statistics:
Page Buffer Read Buffer Page Buffer Write
Read Read % Cache IDX Read Write Write % Cache
0 18720195 100.00 0 87400 704 0.00
Lock Lock LK Wait Log Num Disk Memory
Requests Waits Time (S) Space Sorts Sorts Sorts
10561863 0 0.0000 0.000 B 0 0 0
Total Total Avg Max Avg I/O Wait Avg Rows
Executions Time (S) Time (S) Time (S) IO Wait Time (S) Per Sec
1 99.8398 99.8398 99.8398 0.000000 0.000000 0.2704
Estimated Estimated Actual SQL ISAM Isolation SQL
Cost Rows Rows Error Error Level Memory
6628928 1108253 27 0 0 CR 331184
The below is my SET EXPLAIN output:
QUERY: (OPTIMIZATION TIMESTAMP: 12-06-2016 14:12:43)
------
select * from v_prod_cost
Estimated Cost: 6628928
Estimated # of Rows Returned: 1108253
Temporary Files Required For: Group By
1) informix.irtst: SEQUENTIAL SCAN
2) informix.crtst: INDEX PATH
(1) Index Name: informix. 1565_316639
Index Keys: rtst_id (Serial, fragments: ALL)
Lower Index Filter: informix.irtst.rtst_id = informix.crtst.rtst_id
NESTED LOOP JOIN
3) informix.ishs: INDEX PATH
Filters: (informix.ishs.dc_id = informix.irtst.dc_id AND informix.ishs.dc_id = informix.ishs.whse_id )
(1) Index Name: informix.ishs_rtst
Index Keys: rtst_id (Serial, fragments: ALL)
Lower Index Filter: informix.ishs.rtst_id = informix.crtst.rtst_id
NESTED LOOP JOIN
4) informix.ishd: INDEX PATH
Filters: (informix.ishs.whse_id = informix.ishd.dc_id AND informix.ishd.dc_id = informix.ishd.whse_id )
(1) Index Name: informix.ishdd1
Index Keys: shs_id (Serial, fragments: ALL)
Lower Index Filter: informix.ishs.shs_id = informix.ishd.shs_id
NESTED LOOP JOIN
5) informix.cprod: INDEX PATH
(1) Index Name: informix.cprodu1
Index Keys: prod_id (Serial, fragments: ALL)
Lower Index Filter: informix.ishd.prod_id = informix.cprod.prod_id
NESTED LOOP JOIN
6) informix.cvat: SEQUENTIAL SCAN
DYNAMIC HASH JOIN
Dynamic Hash Filters: informix.cprod.vat_ind = informix.cvat.vat_ind
7) informix.icust: INDEX PATH
(1) Index Name: informix.icustu1
Index Keys: cust_id (Serial, fragments: ALL)
Lower Index Filter: informix.ishd.cust_id = informix.icust.cust_id
NESTED LOOP JOIN
8) informix.iprod: INDEX PATH
(1) Index Name: informix. 588_2002
Index Keys: prod_id dc_id (Serial, fragments: ALL)
Lower Index Filter: (informix.iprod.prod_id = informix.cprod.prod_id AND informix.ishs.whse_id = informix.iprod.dc_id )
NESTED LOOP JOIN
Query statistics:
-----------------
Table map :
----------------------------
Internal name Table name
----------------------------
t1 irtst
t2 crtst
t3 ishs
t4 ishd
t5 cprod
t6 cvat
t7 icust
t8 iprod
type table rows_prod est_rows rows_scan time est_cost
-------------------------------------------------------------------
scan t1 47203 47203 47203 00:00.10 10054
type table rows_prod est_rows rows_scan time est_cost
-------------------------------------------------------------------
scan t2 40715 40977 40715 00:00.41 1
type rows_prod est_rows time est_cost
-------------------------------------------------
nljoin 40715 40978 00:00.53 35213
type table rows_prod est_rows rows_scan time est_cost
-------------------------------------------------------------------
scan t3 303217 325474 303217 00:01.35 3
type rows_prod est_rows time est_cost
-------------------------------------------------
nljoin 303217 278609 00:01.94 154765
type table rows_prod est_rows rows_scan time est_cost
-------------------------------------------------------------------
scan t4 1225518 1280461 1225518 00:07.02 1
type rows_prod est_rows time est_cost
-------------------------------------------------
nljoin 1225518 1111035 00:09.22 550996
type table rows_prod est_rows rows_scan time est_cost
-------------------------------------------------------------------
scan t5 1225518 152406 1225518 00:11.75 1
type rows_prod est_rows time est_cost
-------------------------------------------------
nljoin 1225518 1111036 00:21.40 1337728
type table rows_prod est_rows rows_scan time est_cost
-------------------------------------------------------------------
scan t6 2 2 2 00:00.00 2
type rows_prod est_rows rows_bld rows_prb novrflo time est_cost
------------------------------------------------------------------------------
hjoin 1225518 1111033 2 1225518 0 00:25.94 1544661
type table rows_prod est_rows rows_scan time est_cost
-------------------------------------------------------------------
scan t7 1225518 6941 1225518 00:09.50 0
type rows_prod est_rows time est_cost
-------------------------------------------------
nljoin 1225518 1111037 00:35.99 2084251
type table rows_prod est_rows rows_scan time est_cost
-------------------------------------------------------------------
scan t8 1225518 152627 1225518 00:12.24 1
type rows_prod est_rows time est_cost
-------------------------------------------------
nljoin 1225518 1109418 00:48.97 3736186
type rows_prod est_rows rows_cons time est_cost
------------------------------------------------------------
group 27 1108253 1225518 01:42.95 2892742
What can I do to reduce the cost of nested loops and dynamics hash joins?
declare #ProductDetails as table(ProductName nvarchar(200),ProductDescription nvarchar(200),
Brand nvarchar(200),
Categry nvarchar(200),
Grop nvarchar(200),
MRP decimal,SalesRate decimal,CurrentQuantity decimal,AvailableQty decimal)
declare #AvailableQty table(prcode nvarchar(100),Aqty decimal)
declare #CloseStock table(pcode nvarchar(100),
Cqty decimal)
insert into #CloseStock
select PCODE ,
0.0
from producttable
insert into #AvailableQty
select PCODE ,
0.0
from producttable
--Current Qty
--OpenQty
update #CloseStock set Cqty=((OOQTY+QTY+SRRQTY+PYQTY)-(STQTY+PRRQTY))
from
(
select PC.PCODE as PRODUCTCODE,
--Opening
(select case when SUM(PU.Quantity)is null then 0 else SUM(PU.Quantity) end as Q from ProductOpeningYearEnd PU
where PC.PCODE=PU.ProductName) as OOQTY,
--Purchase
(select case when SUM(PU.quantity)is null then 0 else SUM(PU.quantity) end as Q from purchase PU
where PC.PCODE=PU.prdcode ) as QTY,
--Sales
(select case when SUM(ST.QUANTITY)is null then 0 else SUM(ST.QUANTITY)end as Q2 from salestable ST
where PC.PCODE=ST.PRODUCTCODE and ST.status!='cancel' )as STQTY,
--Physical Stock
(select case when SUM(PS.Adjustment)is null then 0 else SUM(PS.Adjustment)end as Q3 from physicalstock PS
where PC.PCODE=PS.PCODE )as PYQTY,
--Sales Return
(select case when SUM(SR.quantity)is null then 0 else SUM(SR.quantity)end as Q3 from salesreturn SR
where PC.PCODE=SR.prdcode )as SRRQTY,
--Purchase Return
(select case when SUM(PR.quantity)is null then 0 else SUM(PR.quantity)end as Q3 from purchasereturn PR
where PC.PCODE=PR.prdcode )as PRRQTY
from producttable PC
group by PC.PCODE
)t
where PCODE=t.PRODUCTCODE
--Available
update #AvailableQty set Aqty=((CCqty-GIQty)+(GOQty))
--((OOQTY+QTY+SRRQTY+PYQTY)-(STQTY+PRRQTY))
from
(
select PC.PCODE as PRODUCTCODE,
--GoodsIn
(select case when SUM(GI.quantity)is null then 0 else SUM(GI.quantity) end as Q from goodsin GI
where PC.PCODE=GI.productcode) as GIQty,
--GoodsOut
(select case when SUM(GUT.quantity)is null then 0 else SUM(GUT.quantity) end as Q from goodsout GUT
where PC.PCODE=GUT.productcode ) as GOQty,
--Current Stock
(select CS.Cqty as Q from #CloseStock CS
where PC.PCODE=CS.pcode ) as CCqty
from producttable PC
group by PC.PCODE
)t
where prcode=t.PRODUCTCODE
insert into #ProductDetails
select PCODE,[DESCRIPTION],BRAND,CATEGORY,DEPARTMENT,MRP,SALERATE,0,0
from producttable
update #ProductDetails set CurrentQuantity=pcqty,AvailableQty=acqty
from
(
select pt.ProductName as pn,cs.Cqty as pcqty,ac.Aqty as acqty from #ProductDetails pt
inner join #CloseStock cs on pt.ProductName=cs.pcode
inner join #AvailableQty ac on pt.ProductName=ac.prcode
)t
where ProductName=t.pn
select * from #ProductDetails
end
This not working when productable in pcode field add ant (-.&) this kind of symbol i want to even allow in pcode field,
please help me how i can allow any symbol in query
(problem with this code)
update #AvailableQty set Aqty=((CCqty-GIQty)+(GOQty))
from
(
select PC.PCODE as PRODUCTCODE,
--GoodsIn
(select case when SUM(GI.quantity)is null then 0 else SUM(GI.quantity) end as Q from goodsin GI
where PC.PCODE=GI.productcode) as GIQty,
--GoodsOut
(select case when SUM(GUT.quantity)is null then 0 else SUM(GUT.quantity) end as Q from goodsout GUT
where PC.PCODE=GUT.productcode ) as GOQty,
--Current Stock
(select CS.Cqty as Q from #CloseStock CS
where PC.PCODE=CS.pcode ) as CCqty
from producttable PC
group by PC.PCODE
)t
where prcode=t.PRODUCTCODE
The problem here isn't with the symbols you're using, it's that you are assigning the value of a subquery to a single column in the result set. For example:
(select case when SUM(PR.quantity)is null then 0 else SUM(PR.quantity)end as Q3 from purchasereturn PR
where PC.PCODE=PR.prdcode )as PRRQTY
Note that this is allowed only if the subquery returns only a single value; otherwise, we don't know which of the values should be assigned to the column.
If you expect your subqueries to return multiple values and you just want an arbitrary one, use TOP 1 in the subquery to only return 1 value. Otherwise, you'll have to debug each subquery to figure out which returns multiple results and is causing the issue.
Given the following select:
SELECT
COUNT(*) AS total,
SUM(CASE approved WHEN 't' THEN 1 ELSE 0 END) AS num_approved,
SUM(CASE soft_delete WHEN 't' THEN 1 ELSE 0 END) AS num_deleted
FROM model_name;
How might I translate this into something in my class ModelName definition, so that I can retrieve the values of total, num_approved, and num_deleted in my Rails application? I'm happy with any output (array, hash, accessors on a ModelName object) that puts the numbers at the Ruby level.
result = ModelName.select("
COUNT(*) AS total,
SUM(CASE approved WHEN 't' THEN 1 ELSE 0 END) AS num_approved,
SUM(CASE soft_delete WHEN 't' THEN 1 ELSE 0 END) AS num_deleted
").first
irb(main):278:0> result.total
=> 3041
irb(main):279:0> result.num_approved
=> 199464763
irb(main):280:0> result.num_deleted
=> #<BigDecimal:7fa0b7d79fd8,'0.19365329E8',9(18)>