SQL Exists returning errors - sql
So I have the following query:
select *
from ft, pn
where exists (ft.vendnm like '%rocha%' and pn.fno = ft.fno);
Where I only want the results that respect the conditions inside parenthesis.
What SQL Server tells me is:
Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.
And I'm not getting what's wrong with this.
The whole code for context:
select
x.Num,
x.Cliente,
x.Zona,
sum(x.valor_1) as Valor_1,
sum(x.valor_2) as Valor_2,
x.Vendnm,
'2021-01-01 00:00:00.000' as Data1,
'2021-12-31 00:00:00.000' as Data2,
'2022-01-01 00:00:00.000' as Data3,
'2022-12-31 00:00:00.000' as Data4,
(select sum(pn.etiliquido) from pn where pn.vendedor in (3) and (pn.fdata BETWEEN '2021-01-01 00:00:00.000' AND '2021-12-31 00:00:00.000')) as VT1,
(select sum(pn.etiliquido) from pn where pn.vendedor in (3) and (pn.fdata BETWEEN '2022-01-01 00:00:00.000' AND '2022-12-31 00:00:00.000')) as VT2,
isnull(
round((((select sum(pn.etiliquido) from pn where pn.no in (select no from cl where cl.vendedor = 3) and (pn.fdata BETWEEN '2022-01-01 00:00:00.000' AND '2022-12-31 00:00:00.000') AND PN.REF NOT IN ('DESC. FINANCEIRO','PORTES','DESP.BANCARIAS','COL. COM.','VENDA VIAT CI','RESP.TRANSPORTE','RECIGLAGEM','MARK. CONT. 2013','RAPPEL13','JUROS','VENDA VIAT SI','CREDITO SRCM','RAPPEL14','FEIRAS','MARK. CONT. 2014','MARK. CONT. 2015','RAPPEL15','RAPPEL16','MARK. CONT. 2016','RAPPEL17','MARK. CONT. 2017','RAPPEL18','MARK. CONT. 2018','RAPPEL19','MARK. CONT. 2019','MARK. CONT. 2020','RAPPEL20','MARK. CONT. 2021','RAPPEL21','MARK. CONT. 2022','RAPPEL22') and pn.usr1 not in ('12-DAS','13-DIDO','19-GIOTTO','19-GIOTTO BE-BÈ','10-ADIGRAF','27-OMYACOLOR','28-PONGO', '18-FILA', '36-TRATTO','48 - LYRA'))
-
(select sum(pn.etiliquido) from pn where pn.no in (select no from cl where cl.vendedor = 3) and (pn.fdata BETWEEN '2021-01-01 00:00:00.000' AND '2021-12-31 00:00:00.000') AND PN.REF NOT IN ('DESC. FINANCEIRO','PORTES','DESP.BANCARIAS','COL. COM.','VENDA VIAT CI','RESP.TRANSPORTE','RECIGLAGEM','MARK. CONT. 2013','RAPPEL13','JUROS','VENDA VIAT SI','CREDITO SRCM','RAPPEL14','FEIRAS','MARK. CONT. 2014','MARK. CONT. 2015','RAPPEL15','RAPPEL16','MARK. CONT. 2016','RAPPEL17','MARK. CONT. 2017','RAPPEL18','MARK. CONT. 2018','RAPPEL19','MARK. CONT. 2019','MARK. CONT. 2020','RAPPEL20','MARK. CONT. 2021','RAPPEL21','MARK. CONT. 2022','RAPPEL22') and pn.usr1 not in ('12-DAS','13-DIDO','19-GIOTTO','19-GIOTTO BE-BÈ','10-ADIGRAF','27-OMYACOLOR','28-PONGO', '18-FILA', '36-TRATTO','48 - LYRA'))))*100,2),0) as Diftotal,
isnull(
round((((select sum(pn.etiliquido) from pn where pn.no in (select no from cl where cl.vendedor = 3) and (pn.fdata BETWEEN '2022-01-01 00:00:00.000' AND '2022-12-31 00:00:00.000') AND PN.REF NOT IN ('DESC. FINANCEIRO','PORTES','DESP.BANCARIAS','COL. COM.','VENDA VIAT CI','RESP.TRANSPORTE','RECIGLAGEM','MARK. CONT. 2013','RAPPEL13','JUROS','VENDA VIAT SI','CREDITO SRCM','RAPPEL14','FEIRAS','MARK. CONT. 2014','MARK. CONT. 2015','RAPPEL15','RAPPEL16','MARK. CONT. 2016','RAPPEL17','MARK. CONT. 2017','RAPPEL18','MARK. CONT. 2018','RAPPEL19','MARK. CONT. 2019','MARK. CONT. 2020','RAPPEL20','MARK. CONT. 2021','RAPPEL21','MARK. CONT. 2022','RAPPEL22') and pn.usr1 not in ('12-DAS','13-DIDO','19-GIOTTO','19-GIOTTO BE-BÈ','10-ADIGRAF','27-OMYACOLOR','28-PONGO', '18-FILA', '36-TRATTO','48 - LYRA'))
-
(select sum(pn.etiliquido) from pn where pn.no in (select no from cl where cl.vendedor = 3) and (pn.fdata BETWEEN '2021-01-01 00:00:00.000' AND '2021-12-31 00:00:00.000') AND PN.REF NOT IN ('DESC. FINANCEIRO','PORTES','DESP.BANCARIAS','COL. COM.','VENDA VIAT CI','RESP.TRANSPORTE','RECIGLAGEM','MARK. CONT. 2013','RAPPEL13','JUROS','VENDA VIAT SI','CREDITO SRCM','RAPPEL14','FEIRAS','MARK. CONT. 2014','MARK. CONT. 2015','RAPPEL15','RAPPEL16','MARK. CONT. 2016','RAPPEL17','MARK. CONT. 2017','RAPPEL18','MARK. CONT. 2018','RAPPEL19','MARK. CONT. 2019','MARK. CONT. 2020','RAPPEL20','MARK. CONT. 2021','RAPPEL21','MARK. CONT. 2022','RAPPEL22') and pn.usr1 not in ('12-DAS','13-DIDO','19-GIOTTO','19-GIOTTO BE-BÈ','10-ADIGRAF','27-OMYACOLOR','28-PONGO', '18-FILA', '36-TRATTO','48 - LYRA')))/
(select sum(pn.etiliquido) from pn where pn.no in (select no from cl where cl.vendedor = 3) and (pn.fdata BETWEEN '2021-01-01 00:00:00.000' AND '2021-12-31 00:00:00.000') AND PN.REF NOT IN ('DESC. FINANCEIRO','PORTES','DESP.BANCARIAS','COL. COM.','VENDA VIAT CI','RESP.TRANSPORTE','RECIGLAGEM','MARK. CONT. 2013','RAPPEL13','JUROS','VENDA VIAT SI','CREDITO SRCM','RAPPEL14','FEIRAS','MARK. CONT. 2014','MARK. CONT. 2015','RAPPEL15','RAPPEL16','MARK. CONT. 2016','RAPPEL17','MARK. CONT. 2017','RAPPEL18','MARK. CONT. 2018','RAPPEL19','MARK. CONT. 2019','MARK. CONT. 2020','RAPPEL20','MARK. CONT. 2021','RAPPEL21','MARK. CONT. 2022','RAPPEL22') and pn.usr1 not in ('12-DAS','13-DIDO','19-GIOTTO','19-GIOTTO BE-BÈ','10-ADIGRAF','27-OMYACOLOR','28-PONGO', '18-FILA', '36-TRATTO','48 - LYRA')))*100,2),0) as Perc_dif
from
(
select
pn.no as Num,
(select nome from cl where cl.no=pn.no) as Cliente,
(select zona from cl where cl.no=pn.no) as Zona,
sum(pn.etiliquido) as Valor_1,
0 as Valor_2,
(select vendnm from cl where cl.no=pn.no) as Vendnm
from pn
where (pn.usr1 not like '%'+'Portes'+'%')
and (pn.fdata BETWEEN '2021-01-01 00:00:00.000' AND '2021-12-31 00:00:00.000')
and pn.ref<>''
and pn.usr1 not in ('12-DAS','13-DIDO','19-GIOTTO','19-GIOTTO BE-BÈ','10-ADIGRAF','27-OMYACOLOR','28-PONGO', '18-FILA', '36-TRATTO','48 - LYRA')
and pn.REF not IN ('DESC. FINANCEIRO','PORTES','DESP.BANCARIAS','COL. COM.','VENDA VIAT CI','RESP.TRANSPORTE','RECIGLAGEM','MARK. CONT. 2013','RAPPEL13','JUROS','VENDA VIAT SI','CREDITO SRCM','RAPPEL14','FEIRAS','MARK. CONT. 2014','MARK. CONT. 2015','RAPPEL15','RAPPEL16','MARK. CONT. 2016','RAPPEL17','MARK. CONT. 2017','RAPPEL18','MARK. CONT. 2018','RAPPEL19','MARK. CONT. 2019','MARK. CONT. 2020','RAPPEL20','MARK. CONT. 2021','RAPPEL21','MARK. CONT. 2022','RAPPEL22')
group by pn.no
union all
select
pn.no as Num,
(select nome from cl where cl.no=pn.no) as Cliente,
(select zona from cl where cl.no=pn.no) as Zona,
0 as Valor_1,
sum(pn.etiliquido) as Valor_2,
(select vendnm from cl where cl.no=pn.no) as Vendnm
from pn
where (pn.usr1 not like '%'+'Portes'+'%')
and (pn.fdata BETWEEN '2022-01-01 00:00:00.000' AND '2022-12-31 00:00:00.000')
and pn.usr1 not in ('12-DAS','13-DIDO','19-GIOTTO','19-GIOTTO BE-BÈ','10-ADIGRAF','27-OMYACOLOR','28-PONGO', '18-FILA', '36-TRATTO','48 - LYRA')
and pn.ref<>''
and pn.REF not IN ('DESC. FINANCEIRO','PORTES','DESP.BANCARIAS','COL. COM.','VENDA VIAT CI','RESP.TRANSPORTE','RECIGLAGEM','MARK. CONT. 2013','RAPPEL13','JUROS','VENDA VIAT SI','CREDITO SRCM','RAPPEL14','FEIRAS','MARK. CONT. 2014','MARK. CONT. 2015','RAPPEL15','RAPPEL16','MARK. CONT. 2016','RAPPEL17','MARK. CONT. 2017','RAPPEL18','MARK. CONT. 2018','RAPPEL19','MARK. CONT. 2019','MARK. CONT. 2020','RAPPEL20','MARK. CONT. 2021','RAPPEL21','MARK. CONT. 2022','RAPPEL22')
group by pn.no
)
x
where x.Vendnm like '%rocha%'
group by x.Num,x.Zona,x.Cliente,x.Vendnm
order by Valor_2 desc
select ft.*
from ft
join pn on pn.fno = ft.fno
where ft.vendnm like '%rocha%
Related
Translating SQL query to VBA
I have got SQL query running successfully in Oracle, the code is- Select Sam.SAM_ID, sum( case when Aud.AUDIT_COMPLETION between (next_day(trunc(sysdate, 'iw'), 'Friday') - 14) and (next_day(trunc(sysdate, 'iw'), 'Friday') - 7) then 1 else 0 end ) as "Major Defects - 1 week", sum( case when Aud.AUDIT_COMPLETION between (next_day(trunc(sysdate, 'iw'), 'Friday') - 28) and (next_day(trunc(sysdate, 'iw'), 'Friday') - 7) then 1 else 0 end ) as "Major Defect - 4 week", count(Aud.AUDIT_COMPLETION) From CMS.CMS_SAM_ALL_DATA Sam left join CMS.WATSON_AUDIT_INSPECTION_DT1_VW Aud ON Sam.SAM_ID = Aud.SAM_ID Where Aud.DEFECT_SEVERITY = 'Major' AND Aud.AUD_RESULT = 'Defect' And NOT (Aud.AUDIT_OUTCOME = 'SPFR Withdrawn' and Aud.AUDIT_OUTCOME = 'Defect/ Observation Cancelled' and Aud.AUDIT_OUTCOME = 'Rejected by MIMA' and Aud.AUD_RESULT = 'Fixed' and Aud.AUDIT_OUTCOME = 'SPFR response accepted') and Aud.AUDIT_COMPLETION IS NOT NULL Group by Sam.SAM_ID; Now I tried running the above code in macro(VBA) but not able to run macro successfully, Macro code - StrSQL = StrSQL & "Select Sam.SAM_ID," StrSQL = StrSQL & "sum(case when Aud.AUDIT_COMPLETION between (next_day(trunc(sysdate, 'iw'), 'Friday') - 14) and (next_day(trunc(sysdate, 'iw'), 'Friday') - 7) then 1 else 0 end )as ""Major Defects - 1 week""," StrSQL = StrSQL & "sum(case when Aud.AUDIT_COMPLETION between (next_day(trunc(sysdate, 'iw'), 'Friday') - 28) and (next_day(trunc(sysdate, 'iw'), 'Friday') - 7) then 1 else 0 end )as ""Major Defect - 4 week""," StrSQL = StrSQL & "count(Aud.AUDIT_COMPLETION)as ""Total Open Defects"" StrSQL = StrSQL & "From CMS.CMS_SAM_ALL_DATA Sam left join CMS.WATSON_AUDIT_INSPECTION_DT1_VW Aud ON Sam.SAM_ID = Aud.SAM_ID" StrSQL = StrSQL & "Where Aud.DEFECT_SEVERITY = 'Major' AND Aud.AUD_RESULT = 'Defect' And NOT (Aud.AUDIT_OUTCOME = 'SPFR Withdrawn' and Aud.AUDIT_OUTCOME = 'Defect/ Observation Cancelled' and Aud.AUDIT_OUTCOME = 'Rejected by MIMA' and Aud.AUD_RESULT = 'Fixed' and Aud.AUDIT_OUTCOME = 'SPFR response accepted') and Aud.AUDIT_COMPLETION IS NOT NULL, Aud.AUDIT_COMPLETION IS NOT NULL, " StrSQL = StrSQL & "Group by Sam.SAM_ID;" When I run above code I get an error as - FROM Keyword not found where expected
Use line continuation (_) and double up your quotes to escape them: strSQL = "select a, b as ""My Field"" from " & _ "tableZ where a = 'blah' and " & _ "b ='blah' "
You don't have space beetween from count(Aud.AUDIT_COMPLETION)From CMS.CMS_SAM_ALL_DATA Sam left but take Tim Wiliams advice and split it to new lines. You can also do it like this: Dim strSQL As String strSQL = strSQL & "SELECT " strSQL = strSQL & "1 " strSQL = strSQL & "FROM dual "
Selecting Top 5 records of each group based on date (Access/SQL)
I need to select 5 records from each Startup System ([SU SYS-TO DATE].STARTUP_SYS) based off the most recent or future CURRTARGETCOMPDATE. I believe I will need to add a subquery to my SQL query, but I am having a hard time figuring out how: SELECT Friday_Meeting.ID, [SU SYS-TO DATE].STARTUP_SYS, [SU SYS-TO DATE].[FORECAST DATE], TEAMWORKS_24590_TOPUNCHITEM.PUNCHITEM_KEY, TEAMWORKS_24590_TOPUNCHITEM.ORIGINATOR_NAME, TEAMWORKS_24590_TOPUNCHITEM.TAG_NO, TEAMWORKS_24590_TOPUNCHITEM.ENTRYDATE, TEAMWORKS_24590_TOPUNCHITEM.FIELD_ENGINEER, [ITEM_DESCRIPTION1] & " " & [ITEM_DESCRIPTION2] & " " & [TAG_NO] & " " & " " & [CRECORD] AS Description, TEAMWORKS_24590_TOPUNCHITEM.PRIORITY_CODE, [REMARKS1] & " " & [REMARKS2] AS Remarks, TEAMWORKS_24590_TOPUNCHITEM.RESTRAINTS, TEAMWORKS_24590_TOPUNCHITEM.SUPERINTENDENT, TEAMWORKS_24590_TOPUNCHITEM.PIDISCIPLINE, TEAMWORKS_24590_TOPUNCHITEM.CURRTARGETCOMPDATE, TEAMWORKS_24590_TOPUNCHITEM.ORIGTARGETCOMPDATE, TEAMWORKS_24590_TOPUNCHITEM.CRECORD FROM Friday_Meeting LEFT JOIN ([SU SYS-TO DATE] LEFT JOIN TEAMWORKS_24590_TOPUNCHITEM ON [SU SYS-TO DATE].STARTUP_SYS = TEAMWORKS_24590_TOPUNCHITEM.STARTUP_SYS) ON Friday_Meeting.[SU SYS] = [SU SYS-TO DATE].STARTUP_SYS WHERE ((([SU SYS-TO DATE].STARTUP_SYS)="MVE-L-01" Or ([SU SYS-TO DATE].STARTUP_SYS)="LVE-A-01" Or ([SU SYS-TO DATE].STARTUP_SYS)="GRE-A-01or LVE-B-03" Or ([SU SYS-TO DATE].STARTUP_SYS)="LVE-B-07" Or ([SU SYS-TO DATE].STARTUP_SYS)="CHW-B-01") AND ((TEAMWORKS_24590_TOPUNCHITEM.PRIORITY_CODE)="A") AND ((TEAMWORKS_24590_TOPUNCHITEM.CURRTARGETCOMPDATE)>=[FORECAST DATE]-14) AND ((TEAMWORKS_24590_TOPUNCHITEM.RECORD_CLOSURE) Is Null) AND ((TEAMWORKS_24590_TOPUNCHITEM.DELETED)="N")) ORDER BY Friday_Meeting.ID; I truly appreciate any help and guidance!
Try to use large SQL query
I try to use white large SQL query like this: insert into R7810TEST (select 1111111 , 111111 , 1111111 ,'ORG' ,R401.ZIHUI_BAAL_POLISA ,8990 ,'ID' ,R401.ZIHUI_M_RASHI , R401.M_POLISA ,R440.M_SOCHEN , 201507 ,'ORG' , R401.ZIHUI_BAAL_POLISA ,'RTP' , 'CUR' , '1', '*' ,0 ,10000.00 ,0.00 , 0.00 , 0.00, 0.00, 0.00 , 0.00,0.00, 0.00 , r430.hf_oved_tagmulim_45 ,r430.hf_mavid_tagmulim , r430.hf_mavid_pizuim_mukar , 0.00, r430.hf_mavid_chelef, r430.hf_oved_shonot, r430.hf_mavid_shonot , r430.hf_oved_tagmulim_47 ,'MNG', 0.00 , 0.00 ,0.00 ,0.00 , '*' , to_date ('20150701', 'yyyymmdd'), R401.ZIHUI_BAAL_POLISA, r400.sug_polisa, to_date ('20150701', 'yyyymmdd'),'CHI', '12' , 0, 0 ,'0', to_date ('19000101', 'yyyymmdd') from r400 R400 ,r401 R401 ,r430 r430 ,r440 r440 where r400.m_polisa = r401.m_polisa and r401.m_polisa = r430.m_polisa and r430.m_polisa= r440.m_polisa and r401.tr_rishum in (select max(aa.tr_rishum) from r401 aa where aa.m_polisa = r401.m_polisa) and r430.tr_rishum in (select max(aa.tr_rishum) from r430 aa where aa.m_polisa = r430.m_polisa) and r440.tr_rishum in (select max(aa.tr_rishum) from r440 aa where aa.m_polisa = r440.m_polisa) and r401.status_rashi = 10 --and r401.status_rashi in (30,35) ----and r401.status_rashi in (90) --and r401.status_rashi = 20 and r400.sug_polisa in ('1','3','5','7') and r400.sug_hishtatfut <>0 and r400.sug_hazmada <>0 and r400.m_polisa IN (XXXXX)); And I get an exception from SQL, I use Oracle and vba via Excel, I think this too large string so it's not work and the sql get part of query. What can I do with how can I use this query in vba code?
I can see some possible things at play here. It's hard to tell without the DDL for R7810TEST, but I have 2.5 guesses The last statement in your SQL says r400.m_polisa IN (XXXXX). This does not appear to be strongly typed. Was this a placeholder, or is this the real SQL? If XXXXX is a string, you need quotes. If it's supposed to be a number, well... you know. If it's a field in one of the tables, then I suppose it's okay, but it's a weird construct if that's the case. VBA doesn't do well with large concatenations. You didn't list your VBA code, but I can easily see it puking on a string as large as yours. To get around this, you can break the concatenation up into multiple steps. While your SQL actually appears to have balanced parentheses (with the exception of the fact that the parentheses around the select statement are unnecessary), when you are trying to wrap this in VBA strings, it's not difficult to miss something small. A follow-up on item 2 -- again with VBA concatenation, something as simple as a missing space can derail your best SQL. For example: For example: sql = "select one, two, three" & _ "from foo" Unintentionally becomes: select one, two, threefrom foo Sample for point #2: cmd.CommandText = _ "insert into R7810TEST" & _ "select" & _ " 1111111 , 111111 , 1111111 ,'ORG' ,R401.ZIHUI_BAAL_POLISA ,8990 ,'ID' ," & _ " R401.ZIHUI_M_RASHI , R401.M_POLISA ,R440.M_SOCHEN , 201507 ,'ORG' ," & _ " R401.ZIHUI_BAAL_POLISA ,'RTP' , 'CUR' , '1', '*' ,0 ,10000.00 ," & _ " 0.00 , 0.00 , 0.00, 0.00, 0.00 , 0.00,0.00, 0.00 ," & _ " r430.hf_oved_tagmulim_45 ,r430.hf_mavid_tagmulim ," & _ " r430.hf_mavid_pizuim_mukar , 0.00, r430.hf_mavid_chelef," & _ " r430.hf_oved_shonot, r430.hf_mavid_shonot , r430.hf_oved_tagmulim_47 ," & _ " 'MNG', 0.00 , 0.00 ,0.00 ,0.00 , '*' , to_date ('20150701', 'yyyymmdd')," & _ " R401.ZIHUI_BAAL_POLISA, r400.sug_polisa, to_date ('20150701', 'yyyymmdd')," & _ " 'CHI', '12' , 0, 0 ,'0', to_date ('19000101', 'yyyymmdd') " cmd.CommandText = cmd.CommandText & _ "from r400 R400 ,r401 R401 ,r430 r430 ,r440 r440 " & _ "where r400.m_polisa = r401.m_polisa " & _ "and r401.m_polisa = r430.m_polisa " & _ "and r430.m_polisa= r440.m_polisa " & _ "and r401.tr_rishum in (select max(aa.tr_rishum) " & _ "from r401 aa where aa.m_polisa = r401.m_polisa) " & _ "and r430.tr_rishum in (select max(aa.tr_rishum) " & _ "from r430 aa where aa.m_polisa = r430.m_polisa) " & _ "and r440.tr_rishum in (select max(aa.tr_rishum) " & _ "from r440 aa where aa.m_polisa = r440.m_polisa) " & _ "and r401.status_rashi = 10 " & _ "and r400.sug_polisa in ('1','3','5','7') " & _ "and r400.sug_hishtatfut <>0 " & _ "and r400.sug_hazmada <>0 " & _ "and r400.m_polisa IN ('XXXXX')" When in doubt, make liberal use of whitespace in your SQL. Parting shot -- if this doesn't help, please post the DDL for your tables. It may provide a hint as to what's wrong.
You don't say what the error is, but it looks like your insert...select is malformed. As a matter of good practice, you should specify the columns you are inserting into before your select clause, and remove the parentheses around the select. Something like this: insert into R7810TEST ( column1, column2, ...etc) select 1111111 , 111111 , 1111111 ,'ORG' ...etc
Multiple left join in ADO excel
The below SQL query has a syntax error, how can I solve it? SELECT [CREDITORS$].[Account Code], [CREDITORS$].[Name], Sum([CREDITORS$]![Base Amount]*-1) AS BillAmt," & _ "SUM([SERVICETAX$].[Base Amount]) AS STAMT, Sum(([CREDITORS$]![Base Amount]*-1)-[SERVICETAX$]![Base Amount]) AS TDS " & _ "FROM [CREDITORS$] LEFT JOIN [SERVICETAX$] ON [CREDITORS$].[Transaction Reference]=[SERVICETAX$].[Transaction Reference]" & _ "LEFT JOIN [MASTER$] ON [MASTER$].[Account Code]=[CREDITORS$].[Account Code]" & _ "GROUP BY [CREDITORS$].[Account Code], [CREDITORS$].[Name];
On a dynamic query you need add space in before of LEFT JOIN and GROUP BY, that was missed in the query. So adding the space in end of the line will work. So the below query will work: "SELECT [CREDITORS$].[Account Code], [CREDITORS$].[Name], Sum([CREDITORS$]![Base Amount]*-1) AS BillAmt, " & _ "SUM([SERVICETAX$].[Base Amount]) AS STAMT, Sum(([CREDITORS$]![Base Amount]*-1)-[SERVICETAX$]![Base Amount]) AS TDS " & _ "FROM [CREDITORS$] LEFT JOIN [SERVICETAX$] ON [CREDITORS$].[Transaction Reference]=[SERVICETAX$].[Transaction Reference] " & _ "LEFT JOIN [MASTER$] ON [MASTER$].[Account Code]=[CREDITORS$].[Account Code] " & _ "GROUP BY [CREDITORS$].[Account Code], [CREDITORS$].[Name];" You actual query will return as the below, it won't have proper spacing. SELECT [CREDITORS$].[Account Code], [CREDITORS$].[Name], Sum([CREDITORS$]![Base Amount]*-1) AS BillAmt, SUM([SERVICETAX$].[Base Amount]) AS STAMT, Sum(([CREDITORS$]![Base Amount]*-1)-[SERVICETAX$]![Base Amount]) AS TDS FROM [CREDITORS$] LEFT JOIN [SERVICETAX$] ON [CREDITORS$].[Transaction Reference]=[SERVICETAX$].[Transaction Reference]LEFT JOIN [MASTER$] ON [MASTER$].[Account Code]=[CREDITORS$].[Account Code]GROUP BY [CREDITORS$].[Account Code], [CREDITORS$].[Name];
How do I get dataset from the query?
I have the following SQL query in MS Access / VB.net: Dim sql = "SELECT [Product Name], [Customer Name], [Group Associated], [Customer Address], [Product Price], [Current Stock], [Date Ordered], [Volume Purchased] FROM (torder INNER JOIN tproducts ON tproducts.Product_ID = torder.Product_ID) INNER JOIN tcustomer ON torder.Customer_ID = tcustomer.Customer_ID WHERE torder.[Date Ordered] Between #" & StartOfTimePeriod & "# And #" & Today & "#;" conn.Open() Dim dataAdapter As New OleDbDataAdapter(sql, conn) Dim ds As New DataSet() dataAdapter.Fill(ds, "torder") conn.Close() DataGridView.DataSource = ds DataGridView.DataMember = "torder" When the query is run it doesn't display the full dataset that it should. Instead, the results are almost random. What could be wrong? http://prntscr.com/5eh4ge http://prntscr.com/5eh4ai
Do not stringize your date and time values. Use a parameterized query. Dim sql = "SELECT [Product Name], [Customer Name], [Group Associated], " & _ "[Customer Address], [Product Price], [Current Stock], [Date Ordered], " & _ "[Volume Purchased] " & _ "FROM (torder INNER JOIN tproducts ON tproducts.Product_ID = torder.Product_ID) " & _ "INNER JOIN tcustomer ON torder.Customer_ID = tcustomer.Customer_ID " & _ "WHERE torder.[Date Ordered] >= ? And torder.[Date Ordered] <= ?" Dim dataAdapter As New OleDbDataAdapter(sql, conn) Dim p1 = new OleDbParameter() With { .ParameterName = "#p1", .OleDbType = OleDbType.Date, .Value = StartOfTimePeriod } Dim p1 = new OleDbParameter() With { .ParameterName = "#p2", .OleDbType = OleDbType.Date, .Value = DateTime.Today.AddMinutes(1339) } dataAdapter.SelectCommand.Parameters.Add(p1) dataAdapter.SelectCommand.Parameters.Add(p2) .... In this way the correct interpretation of your DateTime values is a job passed to the database engine together with actual DateTime values (I assume that StartOfTimePeriod is a DateTime variable). Instead, if you convert these values to string, the conversion follow the rules of your locale environment that could produce a string not compatible with the expected format of your database engine (for example the inversion of position of months and days values)