SELECT
EMPLOYEE.ID
, EMPLOYEE.NAME
FROM
EMPLOYEE
LEFT OUTER JOIN
APPOINTMENT
ON
EMPLOYEE.ID = APPOINTMENT.EMPLOYEE_ID
GROUP BY
EMPLOYEE.ID
, EMPLOYEE.NAME
, APPOINTMENT.START_TIME
HAVING
(COUNT(APPOINTMENT.EMPLOYEE_ID) <= 3)
AND (APPOINTMENT.START_TIME = CONVERT(TIME, '09:00:00', 114))
SELECT
3TABLE.ID
-- Вычисляем скидку
, CASE
WHEN
TIMES > 2
THEN
'20'
ELSE
'5'
END AS DISCOUNT
FROM
3TABLE
LEFT OUTER JOIN
(
-- Группируем по ИД
-- Суммируем sales
-- Берем более 1500
SELECT
ID
, SUM(SALES) AS SALES
FROM
(
-- Выбираем Samara и Barcelona
-- Исключаем England
-- Группируем (хотя, вот здесь наверное можно не группировать)
SELECT
1TABLE.ID
, SUM(1TABLE.SALES) AS SALES
FROM
1TABLE
INNER JOIN
2TABLE
ON
1TABLE.CITY = 2TABLE.CITY
GROUP BY
1TABLE.ID
, 1TABLE.CITY
, 2TABLE.COUNTRY
, 1TABLE.SALES
HAVING
(1TABLE.CITY IN (N'Samara', N'Barcelona'))
AND (NOT (
2TABLE.COUNTRY = N'England'
))
) AS WRAP_TABLE
GROUP BY
ID
HAVING (SUM(sales) > 1500)
) AS SUM_WRAP_TABLE
ON
SUM_WRAP_TABLE.ID = 3TABLE.ID
SELECT * # - выбрать все *(звездочка)
FROM items # - из таблицы items
WHERE # - где
prise > # - цена prise больше(пишется price правильно)
(
# Этот подзапрос вернет число которое сверяется с prise
SELECT sell_prise # - выбрать sell_prise (sell_priсe правильно)
FROM results # - из таблицы results
WHERE items_id = 1 # - где items_id (в таблице results) = 1
)
AND id = 1; # - и id (в таблице items ) = 1
GROUP_CONCAT(A.ACTID)
SELECT
CASE WHEN P.PRODUCTID != '' THEN P.PRODUCTNAME WHEN L.LICENSEID != '' THEN L.NAME ELSE S.SERVICENAME END AS PRODUCTNAME
, CASE WHEN P.PRODUCTID != '' THEN 'product' WHEN L.LICENSEID != '' THEN 'license' ELSE 'service' END AS TYPE
, SUM(INV.QUANTITY) AS QUANTITY
, SUM(INV.QUANTITY*INV.LISTPRICE*(1-COALESCE(INV.DISCOUNT_PERCENT, 0)/100)-COALESCE(INV.DISCOUNT_AMOUNT, 0)) AS SUBTOTAL
, GROUP_CONCAT(A.ACTID)
FROM
VTIGER_SP_ACT A
INNER JOIN
VTIGER_CRMENTITY E
ON
E.CRMID = A.ACTID
INNER JOIN
VTIGER_INVENTORYPRODUCTREL INV
ON
INV.ID = A.ACTID
LEFT JOIN
VTIGER_PRODUCTS P
ON
P.PRODUCTID = INV.PRODUCTID
LEFT JOIN
VTIGER_SERVICE S
ON
S.SERVICEID = INV.PRODUCTID
LEFT JOIN
VTIGER_LICENSE L
ON
L.LICENSEID = INV.PRODUCTID
WHERE
E.DELETED = FALSE
AND ACTDATE BETWEEN '$from_date' AND '$to_date'
AND A.BRANCH_OR_AGENT IN ($branches)
GROUP BY
1
, 2
SELECT ORDERID, SUM(QUANTITY) AS QUANTITY FROM [ORDERDETAILS] GROUP BY ORDERID
SELECT
TABLE_SUM_QNT.ORDERID
, TABLE_SUM_QNT.QUANTITY
FROM
(
SELECT
ORDERID
, SUM(QUANTITY) AS QUANTITY
FROM
[ORDERDETAILS]
GROUP BY
ORDERID
) AS TABLE_SUM_QNT
WHERE
TABLE_SUM_QNT.QUANTITY > 200
book | rating
---------------
1 | 4
1 | 5
book | rating | gename
----------------------
1 | 4 | g1
1 | 4 | g2
1 | 5 | g1
1 | 5 | g2
book | rating | gename
----------------------
1 | 4.5 | g1, g2, g1, g2
book | rating | gename
----------------------
1 | 4.5 | g1, g2
SELECT
USERS.LOGIN
, NEWS.TITLE
, NEWS.TEXT
FROM
USERS
LEFT OUTER JOIN
COMMENTS
ON
USERS.ID = COMMENTS.USER_ID
RIGHT OUTER JOIN
NEWS
ON
USERS.ID = NEWS.USER_ID
GROUP BY
NEWS.TITLE
, NEWS.TEXT
, USERS.LOGIN
HAVING
(COUNT(COMMENTS.ID) > 1)