我有一张桌子作为付款
ser. paymentdate amount
1. 12 mar 2007 5000
1. 14 mar 2007 5000
2. 6 dec 2007 4000
3. 2 mar 2008 6000
4. 5 nov 2008 2000
我想按月从表组中选择数据,例如 2007 年的记录
month amount
mar 10000
dec 4000
我可以通过此查询获取数据日期,但不能获取月份数据。
请试试:
SELECT
DATENAME(m, paymentdate) [Month],
SUM(Amount) AS Amount
FROM
Payment
WHERE
YEAR(paymentdate)=2007
GROUP BY DATENAME(m, paymentdate), MONTH(paymentdate)
ORDER BY MONTH(paymentdate)
SQL LIKE 子句用于使用通配符将值与相似值进行比较。有两个通配符与 LIKE 运算符一起使用:
The percent sign (%)
The underscore (_)
百分号表示零个、一个或多个字符。下划线表示单个数字或字符。这些符号可以组合使用。
WHERE SALARY LIKE '%200%'
查找在任何位置具有 200 的任何值
因此,根据您的情况,取一些包含您要选择的月份的变量,然后输入类似的语句,
WHERE SALARYDATE LIKE '%mar%'
或其他一些适合您的选择
WHERE SALARY LIKE '200%' Finds any values that start with 200
WHERE SALARY LIKE '%200%' Finds any values that have 200 in any position
WHERE SALARY LIKE '_00%' Finds any values that have 00 in the second and third positions
WHERE SALARY LIKE '2_%_%' Finds any values that start with 2 and are at least 3 characters in length
WHERE SALARY LIKE '%2' Finds any values that end with 2
WHERE SALARY LIKE '_2%3' Finds any values that have a 2 in the second position and end with a 3
WHERE SALARY LIKE '2___3' Finds any values in a five-digit number that start with 2 and end with 3