I have to following subquery
SELECT
(
SELECT SUM( `total_amount` )
FROM `civicrm_contribution`
WHERE `currency` = 'PCT'
AND `contact_id` = ( SELECT `id`FROM `civicrm_contact`
WHERE `first_name` = ''
UNION ALL
SELECT `contact_id` FROM `civicrm_membership`
WHERE `source` = '')
)
-
(
SELECT SUM(`fee_amount`)
FROM `civicrm_participant`
WHERE `fee_level`='puncte'
AND `contact_id` = (SELECT `id` FROM `civicrm_contact`
WHERE `first_name` =''
UNION ALL
SELECT `contact_id` FROM `civicrm_membership`
WHERE `source` = '')
)As RemainingPoints
and i don't know how to add the result to following queries
SELECT `civicrm_contact`.first_name,`civicrm_membership`.start_date, `civicrm_membership`.end_date, `civicrm_membership`.source FROM `civicrm_contact`, `civicrm_membership` WHERE `civicrm_contact`.id = `civicrm_membership`.contact_id AND `civicrm_membership`.source =''
UNION ALL
SELECT `civicrm_contact`.first_name,`civicrm_membership`.start_date, `civicrm_membership`.end_date, `civicrm_membership`.source FROM `civicrm_contact`, `civicrm_membership` WHERE `civicrm_contact`.id = `civicrm_membership`.contact_id AND `civicrm_contact`.first_name ='test1'
I would like that the "RemainingPoints" to be added at the end of the query
first_name ;start_date; end_date; source; "RemainingPoints"
SELECT *
FROM (SELECT `civicrm_contact`.first_name,`civicrm_membership`.start_date, `civicrm_membership`.end_date, `civicrm_membership`.source FROM `civicrm_contact`, `civicrm_membership` WHERE `civicrm_contact`.id = `civicrm_membership`.contact_id AND `civicrm_membership`.source =''
UNION ALL
SELECT `civicrm_contact`.first_name,`civicrm_membership`.start_date, `civicrm_membership`.end_date, `civicrm_membership`.source FROM `civicrm_contact`, `civicrm_membership` WHERE `civicrm_contact`.id = `civicrm_membership`.contact_id AND `civicrm_contact`.first_name ='test1'
) x
JOIN (SELECT (
SELECT SUM( `total_amount` )
FROM `civicrm_contribution`
WHERE `currency` = 'PCT'
AND `contact_id` = ( SELECT `id`FROM `civicrm_contact`
WHERE `first_name` = ''
UNION ALL
SELECT `contact_id` FROM `civicrm_membership`
WHERE `source` = '')
)
-
(
SELECT SUM(`fee_amount`)
FROM `civicrm_participant`
WHERE `fee_level`='puncte'
AND `contact_id` = (SELECT `id` FROM `civicrm_contact`
WHERE `first_name` =''
UNION ALL
SELECT `contact_id` FROM `civicrm_membership`
WHERE `source` = '')
) As RemainingPoints) y
Related
i was searching on google and in stackoverflow.com how can i write this code clearly and nice but found nothing.
Any idea?
MySQL query:
SELECT COUNT(*) AS `allInvoice`,
(SELECT COUNT(*) FROM `ocr_entity` WHERE `status` = 0) AS `new`,
(SELECT COUNT(*) FROM `ocr_entity` WHERE `status` = 10) AS `notTemplate`,
(SELECT COUNT(*) FROM `ocr_entity` WHERE `status` = 50) AS `withMistake`,
(SELECT COUNT(*) FROM `ocr_entity` WHERE `status` = 100) AS `finished`,
(SELECT COUNT(*) FROM `ocr_entity` WHERE `status` = 200) AS `skipped`
FROM `ocr_entity`;
Use conditional aggregation:
SELECT COUNT(*) AS `allInvoice`,
SUM( `status` = 0 ) AS `new`,
SUM( `status` = 10 ) AS `notTemplate`,
SUM( `status` = 50 ) AS `withMistake`,
SUM( `status` = 100 ) AS `finished`,
SUM( `status` = 200 ) AS `skipped`
FROM `ocr_entity`;
MySQL treats booleans as numbers in a numeric context, with 0 for false and 1 for true (which is why SUM() works).
I hava a Table ,I can use
SELECT `event_id` FROM `event` WHERE 1
to get mulipie rows event_id , and I put event_id in this query
SET #event_id = XXXX;
SELECT * FROM(
(SELECT `event_id`,`author_id`,`latitude`,`longitude`,`type`,`rate`,`description`,`photo_id`, UNIX_TIMESTAMP(`timestamp`) AS `timestamp`
FROM `event`) AS `event`,
(SELECT COUNT(*) AS `thumb_up_count` FROM `thumb_event` WHERE `event_id` = #event_id AND `thumb_state` = 1) AS `thumb_up_count`,
(SELECT COUNT(*) AS `thumb_down_count` FROM `thumb_event` WHERE `event_id` = #event_id AND `thumb_state` = 2) AS `thumb_down_count`,
(SELECT IFNULL((SELECT `thumb_state` FROM `thumb_event` WHERE `event_id` = #event_id AND `user_id` = ?) , 0) AS `thumbed`) AS `thumbed`,
(SELECT COUNT(*) AS `comment_count` FROM `event_comment` WHERE `event_id` = #event_id) AS `comment_count`
) WHERE `event_id` = #event_id;
I can use SET #event_id = 0000; to set event_id , but how I can put all event_id from select in that query
If i understoods, maybe what you need it's an IN clause:
WHERE `event_id` IN (SELECT `event_id` FROM `event` WHERE 1);
I want to be able to grab the two values that I have generated in SUM() from two SELECT queries and minus these values in order to get the result (OutstandingFunds).
These are my two SELECT queries:
Statement (1):
SELECT SUM(Cf.Amount) AS ClearedFunds
FROM (
SELECT Amount FROM PAYMENT1 WHERE `Status` = "Cleared"
UNION ALL
SELECT Amount FROM PAYMENT2 WHERE `Status` = "Cleared"
UNION ALL
SELECT Amount FROM PAYMENT3 WHERE `Status` = "Cleared") AS Cf;
Statement (2):
SELECT SUM(Price) AS TotalSales
FROM PROPERTY
WHERE Status = “Sold”;
Thank you for your time
If you don't need to show the separate subtotal for total sales and cleared funds, you can do it like this:
SELECT SUM(Total.`Price`) AS ClearedFunds
FROM (
SELECT `Price` FROM PROPERTY WHERE `Status` = 'Sold'
UNION ALL
SELECT (`Amount` * -1) AS `Price` FROM PAYMENT1 WHERE `Status` = 'Cleared'
UNION ALL
SELECT (`Amount` * -1) AS `Price` FROM PAYMENT2 WHERE `Status` = 'Cleared'
UNION ALL
SELECT (`Amount` * -1) AS `Price` FROM PAYMENT3 WHERE `Status` = 'Cleared'
) AS Total;
I am assuming you are wanting to subtract cleared funds from total sales here.
You were almost there... here's the working SQL:
SELECT (SELECT SUM(Cf.Amount) AS ClearedFunds
FROM (
SELECT Amount FROM PAYMENT1 WHERE `Status` = "Cleared"
UNION ALL
SELECT Amount FROM PAYMENT2 WHERE `Status` = "Cleared"
UNION ALL
SELECT Amount FROM PAYMENT3 WHERE `Status` = "Cleared") as Cf)
- (SELECT SUM(Price) AS TotalSales
FROM PROPERTY
WHERE Status = "Sold") as Result;
Here's the SQL Fiddle so that you can play with test data: http://sqlfiddle.com/#!2/18677/11
You can use SELECT ... INTO ..., in your case:
SELECT SUM(Cf.Amount) AS ClearedFunds
FROM (
SELECT Amount FROM PAYMENT1 WHERE `Status` = "Cleared"
UNION ALL
SELECT Amount FROM PAYMENT2 WHERE `Status` = "Cleared"
UNION ALL
SELECT Amount FROM PAYMENT3 WHERE `Status` = "Cleared") INTO #cf;
SELECT SUM(Price) AS TotalSales
FROM PROPERTY
WHERE Status = “Sold” INTO #ts;
Then you can substract or use them in any other query, like this:
SELECT #ts - #cf;
Are you looking for this?
SELECT C.ClearedFunds - P.TotalSales
FROM (
SELECT SUM(Cf.Amount) AS ClearedFunds
FROM (
SELECT Amount FROM PAYMENT1 WHERE `Status` = "Cleared"
UNION ALL
SELECT Amount FROM PAYMENT2 WHERE `Status` = "Cleared"
UNION ALL
SELECT Amount FROM PAYMENT3 WHERE `Status` = "Cleared") AS Cf) C,
(SELECT SUM(Price) AS TotalSales
FROM PROPERTY
WHERE Status = “Sold”) P
;
I need to get payment sum for all period and for current month. There are two tables: category and transactions.
transactions:
id int(11)
category varchar(32)
dttm_added datetime
minus float
minus_currency varchar(32)
categories:
id int(11)
key varchar(32)
name varchar(50)
type varchar(1)
Here is my query:
select `key`, `id`, `name`, minus_month,month_cur
from `categories` as ct
left join (
select `category` as tr_ct_m, date_format(`dttm_trans`, '%Y%m') as dat, sum(`minus`) as minus_month, `minus_currency` as month_cur from `transactions` where dat = date_format(now(), '%Y%m')
) as tr_m on tr_m.tr_ct_m = ct.key
where `type` = '-'
I need to check clause in select before joining, because after sum of minus_month all dates are turning to Null.
Help plz
not sure if i understand you correctly,please try below:
select `ct`.`key`, `ct`.`id`, `ct`.`name`, tr_m.minus_month,tr_m.month_cur from
( select * from `categories` where `type` = '-') as ct
left join
( select `category` as tr_ct_m, date_format(`dttm_trans`, '%Y%m') as dat,
sum(`minus`) as minus_month, `minus_currency` as month_cur from `transactions`
where dat = date_format(now(), '%Y%m')
) as tr_m on tr_m.tr_ct_m = ct.key
I want to select all rows where WHERE (uid = {$uid} OR uid = **HERE** ) where **HERE** is the cids retreived from query 2 below.
Query 1:
SELECT * FROM `t_activities`
WHERE (`uid` = {$uid} OR `uid` = **HERE** )
AND `del` = 0
GROUP BY `fid`
ORDER BY `time` DESC
LIMIT 10
And Query 2:
SELECT `cid` FROM `t_con` WHERE `uid` = {$uid} AND `flag` = 1
SELECT * FROM `t_activities`
WHERE (`uid` = {$uid} OR `uid` in (SELECT `cid`
FROM `t_con`
WHERE `uid` = {$uid} AND `flag` = 1))
AND `del` = 0
GROUP BY `fid`
ORDER BY `time` DESC
LIMIT 10
You can do this as a join as well:
SELECT *
FROM `t_activities` ta left outer join
(SELECT `cid`
FROM `t_con`
WHERE `uid` = {$uid} AND `flag` = 1)
) tc
on ta = tc.cid
WHERE (`uid` = {$uid} OR tc.`uid` is not null) AND `del` = 0
GROUP BY `fid`
ORDER BY `time` DESC
LIMIT 10
By the way, as a SQL statement the "GROUP BY fid" looks very strange. This is allowed in mysql, but I think it is a bad practice. It is much better to be explicit about what you are doing:
SELECT fid, min(<field1>) as Field1, . . .
This helps prevent mistakes when you go back to the query or try to modify it.