简体   繁体   中英

Can anyone help me to convert Access Query to MS SQL Query

Hi I have a access query, Can any one help me please me out to convert the query to MS SQL 2008 Query.

Struggling with Transform and PIVOT .

Access Query is:

TRANSFORM Count(vwg_vkCustomers.CustomerNo) AS CountOfCustomerNo
SELECT vwg_vkCustomers.ClassID, vwg_vkCustomers.SubjectTypeID, vwg_vkCustomers.TutorCode
FROM vwg_vkCustomers
WHERE (((vwg_vkCustomers.TutorCode)="123456") AND ((vwg_vkCustomers.CustomerAdded)>#6/21/2011#))
GROUP BY vwg_vkCustomers.ClassID, vwg_vkCustomers.SubjectTypeID, vwg_vkCustomers.TutorCode
PIVOT vwg_vkCustomers.Type;

Thanks

I don't know the T-SQL version of IIF, but this is the Access-version of a crosstab query that doesn't use TRANSFORM/PIVOT. Repeat the IIF expression for each column you want:

SELECT vwg_vkCustomers.ClassID, vwg_vkCustomers.SubjectTypeID, vwg_vkCustomers.TutorCode,
Sum(IIF(vwg_vkCustomers.Type="Whatever",1,0) AS TypeWhatever 
FROM vwg_vkCustomers
WHERE (((vwg_vkCustomers.TutorCode)="123456") AND ((vwg_vkCustomers.CustomerAdded)>#6/21/2011#))
GROUP BY vwg_vkCustomers.ClassID, vwg_vkCustomers.SubjectTypeID, vwg_vkCustomers.TutorCode

Combine this with TimD's answer, since I don't wrangle TRANSFORM very often....

For Iif , use CASE :

...
CASE vwg_vkCustomers.Type
WHEN 'Whatever' 1
ELSE 0
END,
...

Note that you use single quotes instead of double.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM