Comparing dates within SUM- CASE WHEN - sql

I am working with a table similar to the following one:
PBName InputDate Amount
Michael Brown 07-jan-13 1391000,000 €
Mary Aspas 07-jan-13 -400000,000 €
Thomas Dand 15-feb-13 3000000,000 €
Mary Aspas 31-jul-13 3000000,000 €
Then, I am just trying to get the total amount by PBName when InputDate is previous to a given one:
Dim dd As Date
dd = 15-feb-13
This is the code I am using:
strSQL = "SELECT Rea.PBName, SUM(CASE WHEN Rea.InputDate < dd THEN Rea.AMOUNT / 1000000 ELSE 0 END) As NNARea "
When I try to run the query I get the message "invalid column name".
Could you please help me?
Thanks

This should work for you...
strSQL = "SELECT PBName, SUM(Amount)/1000000 AS NNARea " & _
"FROM Rea " & _
"WHERE InputDate < #" & dd & "# " & _
"GROUP BY PBName;"

Related

Object required when using GetList function for concatenation

OBJECTIVE: To concatenate row values
Record in COMP table
player_id cc_number
1 123
2 152
1 254
2 154
3 256
Result to be
player_id cc_number
1 123, 254
2 152, 154
3 256
CODE:
MYSQL = "Select T.player_id, "
MYSQL = MYSQL & GetList("Select cc_number From COMP As T1 Where T1.player_id = " & [T].[player_id], "", ", ") & " AS NewCCNumber"
MYSQL = MYSQL & "From COMP AS T"
MYSQL = MYSQL & "Group By T.player_id"
ERROR:
i used GetList function but it gives me error Object Required.
Need space in front of FROM and GROUP or after NewCCNumber and T. Otherwise text strings run together. Also, the GetList() function should probably be embedded in string
MYSQL = "Select T.player_id, "
MYSQL = MYSQL & "GetList('Select cc_number From COMP As T1 Where T1.player_id = " & [T].[player_id] & "', '', ', ') As NewCCNumber "
MYSQL = MYSQL & "From COMP As T "
MYSQL = MYSQL & "Group By T.player_id"

MS Access vba query where

Simple Question.
For example, i have a Customer that have 10 Orders and each order include 6-10 Items.
i want to create a vba query that will desplay all the items of a specific customer.
My query is:
x = CustomerNum
Query = "Select OrderNum from CustomerOrderT Where CustomerNum = " & x
Set result = CurrentDb.OpenRecordset(Query)
y = result!OrderNum
'(there is a lot of orders on y)
Query = "Select Product From OrderProducts Where OrderNum = " & y
Set result = CurrentDb.OpenRecordset(Query)
The problem is that i only see the Products of the first order and i cannot see the products of all the orders that i select on the first query.
Need some help to handke this situation.
Thank you very much.
You can just execute a single query for all orders:
x = CustomerNum
Query = " SELECT CustomerOrderT.CustomerNum, " & _
CustomerOrderT.OrderNum, " & _
" OrderProducts.Product " & _
" FROM CustomerOrderT INNER JOIN OrderProducts " & _
ON CustomerOrderT.OrderNum = OrderProducts.OrderNum " & _
" WHERE (((CustomerOrderT.CustomerNum)=" & x & ")) " & _
"ORDER BY CustomerOrderT.OrderNum, " & _
" OrderProducts.Product;"
And then loop over all the records, noting each change in OrderNum
But beware of building SQL like this if you don't control how variable CustomerNum is assigned, as you open yourself to SQL injection attacks.

SQL SERVER 2008 select statement with case

I have a Select statement in my VB6 application....
Here's what it looks like.....
Dim str As string
str = "SELECT CompID, Department from tblCompanies " & _
"Where CompID in (123, 234, 345, 456) " & _
"Order by CompID "
So what I'm tryign to do here is add a CASE WHEN statement to WHERE clause - basically I'm looking to add a string to each Department name, depending on the COMPID. So I need to specify that these are the COMPID's that I want to select, then I want to do soemthing like
Case when CompID = 123 Then ----ADD "GC" to that Department Name
I guess I need to do this before I open my recordset with
rs.open str, g_CN, adOpenStatic
Because once it's open it seems to be giving me errors when I try to edit it.
All in all, if my recordset looks like this...
Accounting
Finance
IT
R&D
I'm trying to make it look like
"GC" - Accounting
"GC" - Finance
"BP" - IT
"DC" - R&D
change it to,
Dim str As string
str = "SELECT CompID, CASE WHEN CompID IN (123,234) THEN 'GC' " & _
" WHEN CompID = 345 THEN 'IT' " & _
" WHEN CompID = 456 THEN 'DC' " & _
" ELSE '' END + ' - ' + Department AS Department from tblCompanies " & _
"Where CompID in (123, 234, 345, 456) " & _
"Order by CompID "
Try this:
str = "SELECT CompID, CASE WHEN CompID = 123 THEN '""GC"" - ' ELSE '' END + Department As Department from tblCompanies " & _
"Where CompID in (123, 234, 345, 456) " & _
"Order by CompID "

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

Confusing output using DateInterval.day

I confused why my code is not running the right output consistently, This is computation for date format. And im using military time format
Left digit character represents hours
decimal represents minutes
And last is the dateformat
Sample run with 2 digit day format dd:
Input 1 is: 3.56
Input 2 is: 21.32 03/13/2014
Output is: 1.28 03/14/2014
Sample run with 1 digit day format d. Which is my problem when the day is single digits wrong output:
Input 1 is: 3.56
Input 2 is: 21.32 03/07/2014
Output is: 1.33 03/8/2014
This is My code:
'if minutes reach 60
If Val(TextBox7.Text.Trim.Split(".")(1)) >= 60 Then
TextBox7.Text = Val(TextBox7.Text.Trim.Split(".")(0)) + 1 & "." & Val (TextBox7.Text.Trim.Split(".")(1) - 60) & " " & Format(Now, "MM/dd/yyyy")
Else
TextBox7.Text = Format(Val(TextBox6.Text) + Val(Strings.Left(time.Text.Trim, 5)), "##.00") & Strings.Right(time.Text.Trim, 11)
End If
'This is for hours convert which if the minutes reach 60
'Problem here
Dim xDate As Date = Format(CDate(Strings.Right(time.Text.Trim, 11)), "MM/dd/yyyy")
If Val(TextBox7.Text.Trim.Split(".")(0)) >= 24 Then
TextBox7.Text = Val(TextBox7.Text.Trim.Split(".")(0)) - 24 & "." & Val(TextBox7.Text.Trim.Split(".")(1)) & " " & DateAdd(DateInterval.Day, 1, xDate)
End If
You could use the .NET formats:
Console.WriteLine( date.ToString("h:mm dd/MM/yyyy"));
See on MSDN for more details about available formats