I have two tables domain and domain_meta. What I want to accomplish is to PIVOT table domain_meta (which Im able to do) and join that table with domain. But I cant wrap my head arround how to join this tables with a PIVOT and MAX CASE. Is the simplest way to use some sort of inner select to my domain_meta table? My code looks like this so far:
domain
SELECT
*
FROM
domain
ORDER BY id
domain_meta
SELECT
id,
domain_id,
source,
MAX(CASE WHEN (meta_key = 'domain') THEN meta_value ELSE NULL END) AS domain,
MAX(CASE WHEN (meta_key = 'ip') THEN meta_value ELSE NULL END) AS ip,
MAX(CASE WHEN (meta_key = 'link') THEN meta_value ELSE NULL END) AS link,
MAX(CASE WHEN (meta_key = 'net') THEN meta_value ELSE NULL END) AS net
FROM
domain_meta
GROUP BY id
ORDER BY id
domain
domain_meta
Something like this -
SELECT
d.id,
d.domain,
MAX(CASE WHEN (dm.meta_key = 'domain') THEN dm.meta_value ELSE NULL END) AS domain,
MAX(CASE WHEN (dm.meta_key = 'ip') THEN dm.meta_value ELSE NULL END) AS ip,
MAX(CASE WHEN (dm.meta_key = 'link') THEN dm.meta_value ELSE NULL END) AS link,
MAX(CASE WHEN (dm.meta_key = 'net') THEN dm.meta_value ELSE NULL END) AS net
FROM
domain d
LEFT JOIN domain_meta dm
ON d.id = dm.domain_id
GROUP BY d.id
Related
I am trying to select data from a wordpress usermeta table of some members.
I have managed to use this SQL and this works by listing all the users
SELECT
wpdg_usermeta.user_id,
MAX(CASE when wpdg_usermeta.meta_key = 'firm_name' THEN wpdg_usermeta.meta_value ELSE NULL END) AS firm_name,
MAX(CASE when wpdg_usermeta.meta_key = 'member_name' THEN wpdg_usermeta.meta_value ELSE NULL END) AS member_name,
MAX(CASE when wpdg_usermeta.meta_key = 'firm_address' THEN wpdg_usermeta.meta_value ELSE NULL END) AS firm_address,
MAX(CASE when wpdg_usermeta.meta_key = 'firm_city' THEN wpdg_usermeta.meta_value ELSE NULL END) AS firm_city,
MAX(CASE when wpdg_usermeta.meta_key = 'member_email' THEN wpdg_usermeta.meta_value ELSE NULL END) AS member_email,
MAX(CASE when wpdg_usermeta.meta_key = 'member_directory' THEN wpdg_usermeta.meta_value ELSE NULL END) AS member_directory
FROM wpdg_usermeta
GROUP BY wpdg_usermeta.user_id
However I want to filter only those members that agreed to appear in the member directory, so I tried to add this
WHERE
wpdg_usermeta.meta_key = 'member_directory' AND wpdg_usermeta.meta_value IN ('Yes')
However, then I get 2 rows which do have member_directory = Yes, which is correct, but all the other fields in the results except user_id are Null
also tried
wpdg_usermeta.meta_key = 'member_directory' AND wpdg_usermeta.meta_value = 'Yes'
Same result
Sorry, haven't tried selecting from a value pair table before and I'm a bit stumped
You can use a having clause instead:
having member_directory is not null
I'm not too experienced with SQL, so not sure if a similar question has already been answered (I'm sure there is, but I'm way out of my depth here). I used to be great at SQL many years back, but it's been so long that mind is foggy.
I'm migrating customers from one CMS to a different CMS.
Site A = Old CMS
Site B = New CMS
In Site A's database, we have a table "p20kx_users" which contains the basic info - unique ID, username and email.
We then have a table "p20kx_virtuemart_userinfos" which contains in essence, two sets of data; shipping addresses and billing addresses.
In the "p20kx_virtuemart_userinfos" table, some users have just one row (the billing address), however most users have two rows (shipping AND billing addresses). There is a column in the "p20kx_virtuemart_userinfos" table named "address_type" that determines whether the row is Billing (BT) or Shipping (ST). There are also instances where users have neither, so that row shows up as NULL.
I've written a basic query that appears to retrieve all of the details I need, however I'm looking for a way so that the shipping and billing addresses appear on one row, in separate columns, so that I can export the results of the query directly to CSV and import to Site B.
Here's my query (I'm running this via PHPMyAdmin on a MariaDB 10.2 DB):
SELECT
p20kx_users.id,
p20kx_users.name,
p20kx_users.username,
p20kx_users.email,
p20kx_virtuemart_userinfos.address_type,
p20kx_virtuemart_userinfos.company,
p20kx_virtuemart_userinfos.title,
p20kx_virtuemart_userinfos.first_name,
p20kx_virtuemart_userinfos.last_name,
p20kx_virtuemart_userinfos.phone_1,
p20kx_virtuemart_userinfos.phone_2,
p20kx_virtuemart_userinfos.address_1,
p20kx_virtuemart_userinfos.address_2,
p20kx_virtuemart_userinfos.city,
p20kx_virtuemart_userinfos.zip
FROM p20kx_users
LEFT JOIN p20kx_virtuemart_userinfos
ON p20kx_users.id=p20kx_virtuemart_userinfos.virtuemart_user_id
I've had a good old google, and attempted the below which returns one result with mixed up details from other users.
SELECT
p20kx_users.id,
p20kx_users.name,
p20kx_users.username,
p20kx_users.email,
MAX(CASE WHEN p20kx_virtuemart_userinfos.address_type = 'BT' THEN p20kx_virtuemart_userinfos.company ELSE NULL END) Billing_Company,
MAX(CASE WHEN p20kx_virtuemart_userinfos.address_type = 'BT' THEN p20kx_virtuemart_userinfos.title ELSE NULL END) Billing_Title,
MAX(CASE WHEN p20kx_virtuemart_userinfos.address_type = 'BT' THEN p20kx_virtuemart_userinfos.first_name ELSE NULL END) Billing_Fname,
MAX(CASE WHEN p20kx_virtuemart_userinfos.address_type = 'BT' THEN p20kx_virtuemart_userinfos.last_name ELSE NULL END) Billing_LName,
MAX(CASE WHEN p20kx_virtuemart_userinfos.address_type = 'BT' THEN p20kx_virtuemart_userinfos.phone_1 ELSE NULL END) Billing_Phone1,
MAX(CASE WHEN p20kx_virtuemart_userinfos.address_type = 'BT' THEN p20kx_virtuemart_userinfos.phone_2 ELSE NULL END) Billing_Phone2,
MAX(CASE WHEN p20kx_virtuemart_userinfos.address_type = 'BT' THEN p20kx_virtuemart_userinfos.address_1 ELSE NULL END) Billing_Add1,
MAX(CASE WHEN p20kx_virtuemart_userinfos.address_type = 'BT' THEN p20kx_virtuemart_userinfos.address_2 ELSE NULL END) Billing_Add2,
MAX(CASE WHEN p20kx_virtuemart_userinfos.address_type = 'BT' THEN p20kx_virtuemart_userinfos.city ELSE NULL END) Billing_City,
MAX(CASE WHEN p20kx_virtuemart_userinfos.address_type = 'BT' THEN p20kx_virtuemart_userinfos.zip ELSE NULL END) Billing_Zip,
MAX(CASE WHEN p20kx_virtuemart_userinfos.address_type = 'ST' THEN p20kx_virtuemart_userinfos.company ELSE NULL END) Shipping_Company,
MAX(CASE WHEN p20kx_virtuemart_userinfos.address_type = 'ST' THEN p20kx_virtuemart_userinfos.title ELSE NULL END) Shipping_Title,
MAX(CASE WHEN p20kx_virtuemart_userinfos.address_type = 'ST' THEN p20kx_virtuemart_userinfos.first_name ELSE NULL END) Shipping_Fname,
MAX(CASE WHEN p20kx_virtuemart_userinfos.address_type = 'ST' THEN p20kx_virtuemart_userinfos.last_name ELSE NULL END) Shipping_LName,
MAX(CASE WHEN p20kx_virtuemart_userinfos.address_type = 'ST' THEN p20kx_virtuemart_userinfos.phone_1 ELSE NULL END) Shipping_Phone1,
MAX(CASE WHEN p20kx_virtuemart_userinfos.address_type = 'ST' THEN p20kx_virtuemart_userinfos.phone_2 ELSE NULL END) Shipping_Phone2,
MAX(CASE WHEN p20kx_virtuemart_userinfos.address_type = 'ST' THEN p20kx_virtuemart_userinfos.address_1 ELSE NULL END) Shipping_Add1,
MAX(CASE WHEN p20kx_virtuemart_userinfos.address_type = 'ST' THEN p20kx_virtuemart_userinfos.address_2 ELSE NULL END) Shipping_Add2,
MAX(CASE WHEN p20kx_virtuemart_userinfos.address_type = 'ST' THEN p20kx_virtuemart_userinfos.city ELSE NULL END) Shipping_City,
MAX(CASE WHEN p20kx_virtuemart_userinfos.address_type = 'ST' THEN p20kx_virtuemart_userinfos.zip ELSE NULL END) Shipping_Zip
FROM p20kx_users
LEFT JOIN p20kx_virtuemart_userinfos
ON p20kx_users.id=p20kx_virtuemart_userinfos.virtuemart_user_id
Can someone tell me where I am going wrong, and how I should address this - I've spent half a day looking at this. The longer I look, the more confused I get.
Your query is close. You need a GROUP BY:
SELECT u.id, u.name, u.username, u.email,
MAX(CASE WHEN ui.address_type = 'BT' THEN ui.company END) as Billing_Company,
. . .
FROM p20kx_users u LEFT JOIN
p20kx_virtuemart_userinfos ui
ON u.id = ui.virtuemart_user_id
GROUP BY u.id, u.name, u.username, u.email;
I also introduced table aliases, so the query is easier to write and to read. And I removed the ELSE NULL, because that is redundant.
I have a table I need to pivot that contains a value I need to join with a field in another table. I'm trying to determine if I can do this in one step, or if I need to pivot the first table and then join them together. GROUP_ID is in field_name in the redcap_data table and needs to be joined with group_id in redcap_data_access_groups.
CREATE VIEW vwGlobalHealthInfants AS
SELECT rd.record as record_id,
MAX(CASE WHEN rd.field_name = '__GROUP_ID__' THEN rd.value ELSE NULL END) as GroupId,
g.group_name as hospno,
MAX(CASE WHEN rd.field_name = 'admission_temperature' THEN rd.value ELSE NULL END) as adtemp,
MAX(CASE WHEN rd.field_name = 'antenatal_care' THEN rd.value ELSE NULL END) as antecare,
MAX(CASE WHEN rd.field_name = 'anti_hypertensive' THEN rd.value ELSE NULL END) as antihyper,
MAX(CASE WHEN rd.field_name = 'anticonvulsants' THEN rd.value ELSE NULL END) as anticonvul
FROM (
redcapVON.redcap_data rd
JOIN redcapVON.redcap_data_access_groups g ON ( (
GroupId = g.group_id
) )
)
WHERE (
rd.project_id = 12
)
GROUP BY rd.record
I don't know if it's possible to get it to recognize the GroupID field in the join before the pivot.
CTE would solve it. We just don't have the most updated version of mysql to run it. 2 steps it is.
I am working on a query and want to group the rows and return groupped data but my query is not working as expected.
my query-
select item, branch, packunit,packlevel,dealqty,PromotionFlag,PromotionID, PromotionEndDate, cnc,delivery, volumedeal, standard_price_scheme,
deliv_price_scheme
from
(
SELECT
`item` AS `item`,
`branch` AS `branch`,
`PackUnit` AS `PackUnit`,
`PackLevel` AS `PackLevel`,
`DealQty` AS `DealQty`,
`PromotionFlag` AS `PromotionFlag`,
`PromotionID` AS `PromotionID`,
`PromotionEndDate` AS `PromotionEndDate`,
SUM(`cnc`) AS `cnc`,
SUM(`delivery`) AS `delivery`,
SUM(`volumedeal`) AS `volumedeal`,
`standard_price_scheme` AS `standard_price_scheme`,
`deliv_price_scheme` AS `deliv_price_scheme`
FROM
(
SELECT DISTINCT
`Pricing_Today`.`item` AS `item`,
`Pricing_Today`.`branch` AS `branch`,
`Pricing_Today`.`price_scheme` AS `price_scheme`,
`Pricing_Today`.`PackUnit` AS `PackUnit`,
`Pricing_Today`.`PackLevel` AS `PackLevel`,
`Pricing_Today`.`DealQty` AS `DealQty`,
`Pricing_Today`.`PromotionFlag` AS `PromotionFlag`,
`Pricing_Today`.`PromotionID` AS `PromotionID`,
`Pricing_Today`.`PromotionEndDate` AS `PromotionEndDate`,
(CASE
WHEN (`Pricing_Today`.`PriceType` = 'C&C') THEN `Pricing_Today`.`Sell`
END) AS `cnc`,
(CASE
WHEN (`Pricing_Today`.`PriceType` = 'Delivery') THEN `Pricing_Today`.`Sell`
END) AS `delivery`,
(CASE
WHEN (`Pricing_Today`.`PriceType` = 'Volume Deal') THEN `Pricing_Today`.`Sell`
END) AS `volumedeal`,
(CASE
WHEN (`Pricing_Today`.`PriceType` = 'C&C') THEN `Pricing_Today`.`price_scheme`
END) AS `standard_price_scheme`,
(CASE
WHEN
((`Pricing_Today`.`PriceType` = 'Delivery')
OR (`Pricing_Today`.`PriceType` = 'Volume Deal'))
THEN
`Pricing_Today`.`price_scheme`
END) AS `deliv_price_scheme`
FROM
`Pricing_Today`
where item = 78867
and branch = 0
GROUP BY `Pricing_Today`.`item` , `Pricing_Today`.`PackUnit` , `Pricing_Today`.`PriceType`,`standard_price_scheme`,`deliv_price_scheme`
) as a
GROUP BY branch,`item` , `PackUnit`,`standard_price_scheme`,`deliv_price_scheme`
) as a
-- group by item, packunit
which returns -
BUT, when I group by item, pack I get this -
for cnc its showing null values. How do I eliminate null values and get the numbers?
Thanks in advance
You need to take aggregates of the CASE expressions:
SELECT
p.item,
p.branch,
p.price_scheme,
p.PackUnit,
p.PackLevel,
p.DealQty,
p.PromotionFlag,
p.PromotionID,
p.PromotionEndDate,
MAX(CASE WHEN p.PriceType = 'C&C' THEN p.Sell END) AS cnc,
MAX(CASE WHEN p.PriceType = 'Delivery' THEN p.Sell END) AS delivery,
MAX(CASE WHEN p.PriceType = 'Volume Deal' THEN p.Sell END) AS volumedeal,
MAX(CASE WHEN p.PriceType = 'C&C' THEN p.price_scheme END) AS standard_price_scheme,
MAX(CASE WHEN p.PriceType = 'Delivery' OR p.PriceType = 'Volume Deal'
THEN p.price_scheme END) AS deliv_price_scheme
FROM
Pricing_Today p
WHERE
item = 78867 AND branch = 0
GROUP BY
p.item,
p.branch,
p.price_scheme,
p.PackUnit,
p.PackLevel,
p.DealQty,
p.PromotionFlag,
p.PromotionID,
p.PromotionEndDate;
This is just a standard pivot query. The idea behind taking the MAX of a CASE expression is that if a given group of records has a single non NULL value, then MAX would correctly extract it. This works because MAX ignores NULL values.
Note that I removed the backticks from your query, none of which were necessary. I try to avoid using backticks unless they are really needed, because it makes the query harder to read.
I need some help.. I need to output the value of DisplayName if the value of fullname return a null or empty as FullName.
something like
if(u.first_name && u.last_name are not empty)
output CONCAT(u.first_name,' ',u.last_name)
else
output max(case when t.field_id = 1 then t.value else '' end)
but how? also my query took 3.45 seconds :(..
SELECT * FROM(
SELECT
t.user_id,
##this code
max(case when t.field_id = 1 then t.value else '' end) DisplayName,
CONCAT(u.first_name,' ',u.last_name) FullName,
max(case when t.field_id = 270 then t.value else '' end) Gender,
max(case when t.field_id = 274 then t.value else '' end) Birthday,
max(case when t.field_id = 274 then FLOOR(DATEDIFF(CURRENT_DATE, STR_TO_DATE(t.value,'%m/%d/%Y'))/365) else '' end) Age,
max(case when t.field_id = 275 then t.value else '' end) Phone,
max(case when t.field_id = 286 then t.value else '' end) Email,
max(case when t.field_id = 71 then t.value else '' end) Occupation,
max(case when t.field_id = 73 then t.value else '' end) CurrentCountry,
max(case when t.field_id = 24 then t.value else '' end) Region,
max(case when t.field_id = 355 then t.value else '' end) Province,
max(case when t.field_id = 354 then t.value else '' end) City
FROM wp_bp_xprofile_data t
LEFT JOIN (
SELECT
user_id id,
max(case when meta_key = 'first_name' then meta_value end) first_name,
max(case when meta_key = 'last_name' then meta_value end) last_name
from wp_usermeta
GRoup by user_id
)as u
ON u.id = t.user_id
GROUP BY user_id
)temp
WHERE 1 = 1
##Some Condition to be added
LIMIT 0 , 30
I would change to a multiple join to same table repeatedly to get each field on its own criteria
as a possibility of performance. Start the profile data one for say the field_id = 1 as the basis,
then join all the rest on that same key, then no grouping by or max/case when on every tested row.
IF some fields may NOT exists, just have as LEFT-JOIN to the re-used table with alternate alias.
I would ensure you have a compound/covering index on your profile table with the following key
( user_id, field_id, value )
Then do the following query. The interesting thing here, is it basically opens the same table for each "component" part of the user_ID and gets them all lined up, but runs the query based only on a single instance of records associated with field_id = 1 in the where clause. All the other instances are in their respective aliases joined ON clause. So now, I just run through the records for field_id = 1, and if found on the other tables, grabs the value, otherwise blank (via coalesce). Similarly joined to the user meta-data table to get the first/last name parts.
if this works for you, I would be interested in knowing the performance one-way or the other. The benefit is that it doesn't have to query EVERYONE up front (the inner-most query) to then run the main query of all records, apply the group by, then stop at your 30 limit clause. It just gets up to the first 30 where field_id = 1 and its done. Now, if the population of "field_id = 1" is a low populated field, and you want to swap with another, so be it, you know your data better than us.
SELECT
DN.User_ID,
case when fn.user_id is null and ln.user_id is null then DN.value
when fn.user_id is null then ln.meta_value
when ln.user_id is null then fn.meta_value
else concat( fn.meta_value, ' ', ln.meta_value ) end as FinalName,
DN.Value DisplayName,
CONCAT( COALESCE( fn.meta_value, '' ),' ',COALESCE( ln.meta_value, '' )) FullName,
COALESCE( G.Value, '' ) Gender,
COALESCE( BD.Value, '' ) Birthday,
COALESCE( FLOOR( DATEDIFF( CURRENT_DATE, STR_TO_DATE( BD.value,'%m/%d/%Y'))/365), '' ) Age,
COALESCE( P.Value, '' ) Phone,
COALESCE( E.Value, '' ) Email,
COALESCE( O.Value, '' ) Occupation,
COALESCE( CC.Value, '' ) CurrentCountry,
COALESCE( R.Value, '' ) Region,
COALESCE( PR.Value, '' ) Province,
COALESCE( C.Value, '' ) City
from
wp_bp_xprofile_data DN
LEFT JOIN wp_bp_xprofile_data G
ON DN.User_ID = G.User_ID AND G.field_id = 270
LEFT JOIN wp_bp_xprofile_data BD
ON DN.User_ID = BD.User_ID AND BD.field_id = 274
LEFT JOIN wp_bp_xprofile_data P
ON DN.User_ID = P.User_ID AND P.field_id = 275
LEFT JOIN wp_bp_xprofile_data E
ON DN.User_ID = E.User_ID AND E.field_id = 286
LEFT JOIN wp_bp_xprofile_data O
ON DN.User_ID = O.User_ID AND O.field_id = 71
LEFT JOIN wp_bp_xprofile_data CC
ON DN.User_ID = CC.User_ID AND CC.field_id = 73
LEFT JOIN wp_bp_xprofile_data R
ON DN.User_ID = R.User_ID AND R.field_id = 24
LEFT JOIN wp_bp_xprofile_data PR
ON DN.User_ID = PR.User_ID AND PR.field_id = 355
LEFT JOIN wp_bp_xprofile_data C
ON DN.User_ID = C.User_ID AND C.field_id = 354
LEFT JOIN wp_usermeta FN
ON DN.User_ID = FN.User_ID AND FN.meta_key = 'first_name'
LEFT JOIN wp_usermeta LN
ON DN.User_ID = LN.User_ID AND LN.meta_key = 'last_name'
where
DN.field_id = 1
AND O.Value like '%Programmer%'
LIMIT
0, 30
I modified to add a FINAL Name by use of a case/when.
if both the alias FN/LN (first name / last name) are BOTH null, then get the DN (display name) value. Otherwise we have AT LEAST ONE or BOTH parts of the first/last name field. If the first name was null, just return the last name. If the last name was null, return the first name, otherwise BOTH first and last names were available, and return the concatinated version of the name.
As for applying extra WHERE clauses. As soon as you add a "required" criteria to the where clause against any of the left-join aliases, it will in-effect turn them into INNER JOINs and only include those that HAVE that component AND that component has the value you want... So, per your comment I would just add...
where
DN.field_id = 1
AND BD.User_ID IS NOT NULL
AND STR_TO_DATE( BD.value,'%m/%d/%Y') BETWEEN '2009-01-01' and '2012-01-01'
You could use your datediff for age and do that between 1 and 2, but just a preference to pick a date range of interest..
For your additional comment about Programmer filtering, the way you had it was trying to test ALL the joined aliases looking for programmer, but at the same time, all of them required because you wanted them all as NOT NULL. If you want a field required, but no other specific criteria, such as ALWAYS must have email address, then just change
LEFT JOIN wp_bp_xprofile_data E
to
JOIN wp_bp_xprofile_data E
if you want to make sure the field is considered only if it has a value other than space (such as your last/first name)
LEFT JOIN wp_usermeta FN
ON DN.User_ID = FN.User_ID
AND FN.meta_key = 'first_name'
AND LENGTH( TRIM( COALESCE( FN.meta_Value, '' ))) > 0
This will make sure the record is only considered when it actually has a non-empty string value.
Back to your consideration of "%Programmer%'... why would you ever expect a city, Province, region, etc to have a value of programmer. You should only require the Occupation instance to have the value. Also, because you had an OR, it was doing ALL the rows. You would need to have wrapped the conditions something like
where
DN.field_id = 1
AND ( O.Value like '%Programmer%'
OR E.Value like '%#gmail.com'
OR C.Value like 'Some City' )
Notice I wrapped my ADDITIONAL criteria OR components. This way it still only starts with the Field_ID = 1 entries, but based on the JOIN adds the extra criteria, and in this case, DN.field_id = 1 always, but ANY ONE (or more) of the OR conditions must also be true... The Occupation alias has a value like programmer, email alias has a value of gmail, the city alias like 'Some City'.
Hopefully this clarifies how to implement... Also, make note, that using any LIKE qualifier that has '%' leading the string needs to compare through the whole string and will slow the query down some, but since the join is based on the user ID and the field_ID value, that should still keep it fast (relatively).
Try this:
SELECT if( trim(CONCAT(first_name,' ', lastName))='',
DisplayName,
CONCAT(first_name,' ', lastName) ) as Name
...rest of the fieds on TA subquery
FROM (
(SELECT
t.user_id,
max(case when t.field_id = 1 then t.value else '' end) DisplayName,
max(case when t.field_id = 270 then t.value else '' end) Gender,
max(case when t.field_id = 274 then t.value else '' end) Birthday,
max(case when t.field_id = 274
then FLOOR( DATEDIFF( CURRENT_DATE,
STR_TO_DATE( t.value, '%m/%d/%Y')) / 365)
else '' end) Age,
max(case when t.field_id = 275 then t.value else '' end) Phone,
max(case when t.field_id = 286 then t.value else '' end) Email,
max(case when t.field_id = 71 then t.value else '' end) Occupation,
max(case when t.field_id = 73 then t.value else '' end) CurrentCountry,
max(case when t.field_id = 24 then t.value else '' end) Region,
max(case when t.field_id = 355 then t.value else '' end) Province,
max(case when t.field_id = 354 then t.value else '' end) City
FROM wp_bp_xprofile_data t
GROUP BY user_id
) TA
LEFT JOIN
( SELECT user_id id,
max(case when meta_key = 'first_name'
then meta_value end) first_name,
max(case when meta_key = 'last_name'
then meta_value end) last_name
from wp_usermeta
GRoup by user_id ) as U on (ta.user_id = U.id)
)temp
WHERE 1 = 1
##Some Condition to be added
LIMIT 0 , 30