Jorgen
asked on
How to change my SQL Statement in PowerQuery
Hi Experts
I have a SQL statement that I can import to a Power Query
I received it from the supplier of our hotel system, but they are not very helpful apart from giving me the SQL code.
at the moment I can change the code in line 2 and I am able to see the specific date in Power Query.
I want to be able to import all days after a specific date, and afterwards I want my datamodel to be updated with new months
I am well aware, that the information might not be of the best quality, but hopefully you can guide me anyway
SELECT
'01.06.2015' as 'DateStamp',
isnull(Accounts.ExportAcco untNumber, Accounts.AccountNumber) as 'Account Number',
Accounts.AccountText as 'Account Text',
SUM(Transactions.PriceOver write * (CASE Transactions.Debit WHEN 1 THEN 1 ELSE -1 END)) as 'PriceOverWrite Without Vat',
SUM(Transactions.PriceOver write * (CASE Transactions.Debit WHEN 1 THEN 1 ELSE -1 END)* CASE WHEN Accounts.AccountType IN (4,5,6) THEN 0 ELSE (VatValue/100) END) as 'PriceOverWrite Vat',
0 as 'Reserved (AccountType)',
isnull(Accounts.ExportDepa rtmentNumb er, 110) as 'Export Department Number',
(case when isnull(Accounts.ExportAcco untNumber, Accounts.AccountNumber) = 20210 then 5 else
case when isnull(Accounts.ExportAcco untNumber, Accounts.AccountNumber) = 20215 then 6 else
case when isnull(Accounts.ExportAcco untNumber, Accounts.AccountNumber) = 20222 then 7 else
case when isnull(Accounts.ExportAcco untNumber, Accounts.AccountNumber) = 20248 then 5 else
case when isnull(Accounts.ExportAcco untNumber, Accounts.AccountNumber) = 20258 then 4 else null
end
end
end
end
end)
as 'Account alias',
Customer.HotelNumber as 'Hotel Number',
CASE WHEN (Reservations.Project IS NULL) THEN IsNull(Accounts.ExportProj ectCodeNum ber, 0) ELSE IsNUll(Reservations.Refere nceNumber, 0) END as 'ExportProjectcodeNumber',
Departments.Departmentnumb er as 'Active Department Number',
99999 as 'Reserved (99999)'
FROM Accounts (NOLOCK)
FULL OUTER JOIN TurnOverGroups WITH (NOLOCK) ON Accounts.TurnOverGroupNumb er = TurnOverGroups.NUMBER
FULL OUTER JOIN AccountGroups WITH (NOLOCK) ON Accounts.AccountGroupNumbe r = AccountGroups.AccountGroup Number
FULL OUTER JOIN Transactions WITH (NOLOCK) ON Accounts.AccountNumber = Transactions.AccountNumber
INNER JOIN Folios WITH (NOLOCK) ON Folios.FolioNumber = Transactions.FolioNumber
LEFT JOIN Reservations WITH (NOLOCK) ON (Reservations.ReferenceNum ber = Folios.ReferenceNumber) AND (Reservations.GuestNumber = Folios.GuestNumber)
LEFT OUTER JOIN Departments WITH (NOLOCK) ON Departments.PK_Department = Transactions.FK_Department Number
INNER JOIN Customer WITH (NOLOCK) ON Customer.CustomerNumber = Transactions.HotelCustomer Number
INNER JOIN Ware WITH (NOLOCK) ON Ware.WareNumber = Transactions.WareNumber
INNER JOIN WareGroups WITH (NOLOCK) ON WareGroups.WareGroupNumber = Ware.WareGroupNumber
WHERE
convert(varchar,Transactio ns.DateSta mp,112) > '20141125' AND
Customer.HotelNumber in (0)
GROUP BY
CASE WHEN (Reservations.Project IS NULL) THEN IsNull(Accounts.ExportProj ectCodeNum ber, 0) ELSE IsNUll(Reservations.Refere nceNumber, 0) END
,
(case when isnull(Accounts.ExportAcco untNumber, Accounts.AccountNumber) = 20210 then 5 else
case when isnull(Accounts.ExportAcco untNumber, Accounts.AccountNumber) = 20215 then 6 else
case when isnull(Accounts.ExportAcco untNumber, Accounts.AccountNumber) = 20222 then 7 else
case when isnull(Accounts.ExportAcco untNumber, Accounts.AccountNumber) = 20248 then 5 else
case when isnull(Accounts.ExportAcco untNumber, Accounts.AccountNumber) = 20258 then 4 else null
end
end
end
end
end)
,
Accounts.AccountNumber,
Accounts.AccountText,
Accounts.ExportAccountNumb er,
Accounts.ExportDepartmentN umber,
Customer.HotelNumber,
Departments.Departmentnumb er
HAVING
SUM(Transactions.PriceOver write * (CASE Transactions.Debit WHEN 1 THEN 1 ELSE -1 END)) <> 0
ORDER BY
isnull(Accounts.ExportAcco untNumber, Accounts.AccountNumber) ,
isnull(Customer.HotelNumbe r, -1)
I have a SQL statement that I can import to a Power Query
I received it from the supplier of our hotel system, but they are not very helpful apart from giving me the SQL code.
at the moment I can change the code in line 2 and I am able to see the specific date in Power Query.
I want to be able to import all days after a specific date, and afterwards I want my datamodel to be updated with new months
I am well aware, that the information might not be of the best quality, but hopefully you can guide me anyway
SELECT
'01.06.2015' as 'DateStamp',
isnull(Accounts.ExportAcco
Accounts.AccountText as 'Account Text',
SUM(Transactions.PriceOver
SUM(Transactions.PriceOver
0 as 'Reserved (AccountType)',
isnull(Accounts.ExportDepa
(case when isnull(Accounts.ExportAcco
case when isnull(Accounts.ExportAcco
case when isnull(Accounts.ExportAcco
case when isnull(Accounts.ExportAcco
case when isnull(Accounts.ExportAcco
end
end
end
end
end)
as 'Account alias',
Customer.HotelNumber as 'Hotel Number',
CASE WHEN (Reservations.Project IS NULL) THEN IsNull(Accounts.ExportProj
Departments.Departmentnumb
99999 as 'Reserved (99999)'
FROM Accounts (NOLOCK)
FULL OUTER JOIN TurnOverGroups WITH (NOLOCK) ON Accounts.TurnOverGroupNumb
FULL OUTER JOIN AccountGroups WITH (NOLOCK) ON Accounts.AccountGroupNumbe
FULL OUTER JOIN Transactions WITH (NOLOCK) ON Accounts.AccountNumber = Transactions.AccountNumber
INNER JOIN Folios WITH (NOLOCK) ON Folios.FolioNumber = Transactions.FolioNumber
LEFT JOIN Reservations WITH (NOLOCK) ON (Reservations.ReferenceNum
LEFT OUTER JOIN Departments WITH (NOLOCK) ON Departments.PK_Department = Transactions.FK_Department
INNER JOIN Customer WITH (NOLOCK) ON Customer.CustomerNumber = Transactions.HotelCustomer
INNER JOIN Ware WITH (NOLOCK) ON Ware.WareNumber = Transactions.WareNumber
INNER JOIN WareGroups WITH (NOLOCK) ON WareGroups.WareGroupNumber
WHERE
convert(varchar,Transactio
Customer.HotelNumber in (0)
GROUP BY
CASE WHEN (Reservations.Project IS NULL) THEN IsNull(Accounts.ExportProj
,
(case when isnull(Accounts.ExportAcco
case when isnull(Accounts.ExportAcco
case when isnull(Accounts.ExportAcco
case when isnull(Accounts.ExportAcco
case when isnull(Accounts.ExportAcco
end
end
end
end
end)
,
Accounts.AccountNumber,
Accounts.AccountText,
Accounts.ExportAccountNumb
Accounts.ExportDepartmentN
Customer.HotelNumber,
Departments.Departmentnumb
HAVING
SUM(Transactions.PriceOver
ORDER BY
isnull(Accounts.ExportAcco
isnull(Customer.HotelNumbe
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
ASKER
It all sounds as wise advice. Unfortunately I will not be with the data before thursday, where I can give it a try.
But can you explain to me, why I have to use the >= and not just the > as I did try. And the other part I would like to understand if I can just replace the datestamp with the transactions.DateStamp to get the date for all the transactions? I would expect so, but as I can read simple SQL statements, a statement of this kind is a Little to complex.
regards
Jørgen