all the varchar columns in all the tables from sybase - sybase-ase

Does anybody know how can we get the list of all the columns in all the tables which are varchar?
i need it in below format:
<column_name> <table_name>
i tried this:
select o.name [TableName], c.name [ColumnName] from sysobjects o
inner join syscolumns c on c.id = o.id inner join systypes t
on t.usertype = c.usertype where o.type = 'U' and o.name in ("MYTABLE")
but the above gives the list of all columns.
if i can find a way then i can put all the table names inside last braces.

You can use where systypes.name = 'varchar', or systypes.type = 39, or syscolumn.type = 39 to narrow your query to just varchar columns.
select o.name, c.name from sysobjects o, syscolumns c
where o.id = c.id
and c.type = 39
and o.type = "U"

Related

Error in on clause comparison - Big Query

The following big query code gives the following error.
select
selected_date date,
pp.name property,
bb.bookings bb,
av.available vailable,
from
(SELECT DATE(DATE_ADD(TIMESTAMP("2017-10-01"), pos - 1, "DAY")) AS selected_date
FROM (
SELECT ROW_NUMBER() OVER() AS pos, *
FROM (FLATTEN((
SELECT SPLIT(RPAD('', 1 + DATEDIFF(TIMESTAMP(CURRENT_DATE()), TIMESTAMP("2017-10-01")), '.'),'') AS h
FROM (SELECT NULL)),h
)))) v
cross join
(select p.name name from [roomsproperties.properties] p where p.name not like '%test%' group by name) as pp
left join
(select sum(b.rooms) bookings,
p.name property,
b.checkin checkin,
b.checkout checkout
from [bookings.bookings] b
left join [roomsproperties.rooms] r on r.id = b.room_id
left join [roomsproperties.properties] p on p.id = r.property_id
where p.name not like '%test%'
and b.status not in('Rejected', 'Cancelled - By customer', 'OTP Not Varified')
group by property,checkin,checkout
) as bb on pp.name = bb.property and (v.selected_date between bb.checkin and bb.checkout)
left join
(select sum(r.quantity) available,
p.name property,
date(r.created_at) date
from [roomsproperties.rooms] r
left join [roomsproperties.properties] p on p.id = r.property_id
group by property, date
) av on pp.name = av.property and v.selected_date >= av.date
The error is,
Error: ON clause must be AND of = comparisons of one field name from each table, with all field names prefixed with table name. Consider using Standard SQL
Can any one help
You should try:
(select ...) as bb on pp.name = bb.property
WHERE v.selected_date between bb.checkin and bb.checkou
and:
(select ...) as av on pp.name = av.property
WHERE v.selected_date >= av.date

SELECT product variants SQL

This is my query without variant options
SELECT p.*, pd.`name` AS `product_name`
FROM `product` AS `p`
LEFT JOIN `product_description` AS `pd` ON p.`id` = pd.`product_id`
LEFT JOIN `product_to_variant` AS `pv` ON p.`id` = pv.`product_id`
WHERE p.`status` = 0
GROUP BY p.`id`
ORDER BY p.`id` DESC;
SQLFiddle: http://sqlfiddle.com/#!9/8955b/5
and the follwing query has variant options but it doesn't work
SELECT p.*, pd.`name` AS `product_name`
FROM `product` AS `p`
LEFT JOIN `product_description` AS `pd` ON p.`id` = pd.`product_id`
LEFT JOIN `product_to_variant` AS `pv` ON p.`id` = pv.`product_id`
WHERE p.`status` = 0
AND (pv.`feature_id` = 2 AND pv.`variant_id` = 6)
AND (pv.`feature_id` = 3 AND pv.`variant_id` = 11)
GROUP BY p.`id`
ORDER BY p.`id` DESC;
and I also trying to query but there is no output
SELECT pv.* FROM `product_to_variant` AS `pv`
WHERE (pv.`feature_id` = 2 AND pv.`variant_id` = 2)
AND (pv.`feature_id` = 3 AND pv.`variant_id` = 11)
Do you have any other idea how to receive the products: 14, 15 by specific variant_id 6 AND 11 http://prntscr.com/ect2oh
Here is one method to do what you want:
SELECT p.*, pd.name AS product_name
FROM product p LEFT JOIN
product_description pd
ON p.id = pd.product_id JOIN
product_to_variant pv
ON p.id = pv.product_id
WHERE p.status = 0 AND
((pv.feature_id = 2 AND pv.variant_id = 6) OR
(pv.feature_id = 3 AND pv.variant_id = 11)
)
GROUP BY p.id
HAVING COUNT(DISTINCT feature_id) = 2
ORDER BY p.id DESC;
Notes:
No row can meet your original conditions. Because a column cannot have two values at the same time. Hence the OR rather than AND.
The HAVING clause checks that both values match.
There is no need for a LEFT JOIN to pv, because you are checking values from that table in the WHERE clause -- there have to be matches.
The LEFT JOIN to pa is probably also unnecessary.
Using backticks everywhere just makes the query harder to write and to read.
Maybe like this:
SELECT pv.* FROM `product_to_variant` AS `pv`
WHERE (pv.`feature_id` = 2 AND pv.`variant_id` = 2)
OR (pv.`feature_id` = 3 AND pv.`variant_id` = 11)
Instead of AND, which says, you need both feature_id and variant_id in the results, use OR because it takes both.

How to convert SQL Query for Access 2007?

Here is my query. I need it to be formatted so that it can run in Access 2007.
SELECT DISTINCT(meters.meterNo), readings.momentaryIntCount,
readings.readingDate,
ServiceLocations.servLoc
FROM readings
inner join EndPoints on readings.endPointId = endpoints.endPointId
join Meters on endpoints.meterId = meters.meterId
join spus on endpoints.spuid = spus.spuid
join ServiceLocations on meters.serviceLocationId = ServiceLocations.serviceLocationId
left join firmware on endpoints.firmwareId = firmware.firmwareId
left join virtualgroupassoc v on EndPoints.endpointid = v.objectid
left join groups g on v.groupid = g.groupid and g.grouptypeid in (4, 24) and g.active = 1
WHERE readings.readingDate = '01-01-2015'
and firmware.version >= '18'
and quality = 0
and g.name is null
ORDER BY meterNo
Joining multiple tables in Access is a bit of a bother. You have to nest the joins.
For example if you want to do this in SQL:
SELECT
a.columna
, b.columnb
, c.columnc
FROM tablea AS a
LEFT JOIN tableb AS b
ON a.id = b.id
LEFT JOIN tablec AS c
ON a.id = c.id
You'll need to do this in Access:
SELECT
a.columna
, b.columnb
, c.columnc
FROM (
(tablea AS a)
LEFT JOIN tableb AS b ON a.id = b.id)
LEFT JOIN tablec AS c ON a.id = c.id
So for your query:
SELECT
DISTINCT(meters.meterNo)
, readings.momentaryIntCount
, readings.readingDate
, ServiceLocations.servLoc
FROM ((((((
(readings)
INNER JOIN EndPoints
ON readings.endPointId = EndPoints.endPointId)
JOIN Meters
ON EndPoints.meterId = Meters.meterId)
JOIN spus
ON EndPoints.spuid = spus.spuid)
JOIN ServiceLocations
ON meters.serviceLocationId = ServiceLocations.serviceLocationId)
LEFT JOIN firmware
ON EndPoints.firmwareId = firmware.firmwareId)
LEFT JOIN virtualgroupassoc v
ON EndPoints.endpointid = v.objectid)
LEFT JOIN groups g
ON v.groupid = g.groupid AND g.grouptypeid in (4, 24) AND g.active = 1
WHERE readings.readingDate = '01-01-2015'
and firmware.version >= '18'
and quality = 0
and g.name is null
ORDER BY meterNo
Resource:
http://nm1m.blogspot.com/2007/10/multiple-left-joins-in-ms-access.html

How to edit this very complicated Joomla query that kills the server

I have to edit the query bellow on a Joomla site. This query is so slow and hard and it very often kills the server and I get errors Gateway timeout.. Please help me to edit it if possible to avoid so many Left Joins because I think the make it so "heavy".. The query is:
SELECT
o.*, o.price AS value, c.name AS catname, g.title AS groupname, u.name AS editor,
u.id AS userid, u.block as blockuser, t.name as typename, t.id as typeid,
a.name as agentname, a.id as agentid, image.path as imagepath,
image.fname as imagefilename, image.type as imagetype, l.title AS language_title,
l.image AS langImage, u.name AS editor
FROM (mytable_estateagent AS o, mytable_estateagent_categories AS c )
LEFT JOIN (
SELECT * FROM mytable_estateagent_images AS img
WHERE 1 AND (img.type = '.jpg' OR img.type = '.gif' OR img.type = '.png')
AND ( img.ordering= 1 or img.ordering= 0 ) AND img.published = 1
GROUP BY img.objid
)
as image ON (image.objid = o.id)
LEFT JOIN mytable_estateagent_type AS t ON (o.type = t.id)
LEFT JOIN mytable_languages AS l ON l.lang_code = o.language
LEFT JOIN mytable_estateagent_agents AS a ON (o.agent = a.id)
LEFT JOIN mytable_viewlevels AS g ON (g.id = o.access)
LEFT JOIN mytable_users AS u ON (u.id = o.created_by)
LEFT JOIN mytable_users AS uc ON uc.id = o.checked_out
WHERE 1 AND (c.id = o.cat) ORDER BY id ASC
See comments above, but note also that this bit (edited)...
SELECT o.*
...
LEFT JOIN
( SELECT DISTINCT img.objid
FROM mytable_estateagent_images img
WHERE img.type IN('.jpg','.gif','.png')
AND img.ordering IN(1,0)
AND img.published = 1
) image
ON image.objid = o.id
... can be (further) rewritten as follows...
SELECT DISTINCT o.*
...
LEFT
JOIN mytable_estateagent_images image
ON image.objid
AND image.type IN('.jpg','.gif','.png')
AND image.ordering IN(1,0)
AND image.published = 1
...

mysql if statements how to

How do i code this in mysql?
if tbl_comments.to is a number then join with tbl_users
if tbl_comments.to is a player key then join with table persons and display names
if tbl_comments.to is a team key then join with table teams and display names
if tbl_comments.to is a league key then join with tbl_sports
The practice of having a column that references one of several possible tables is called polymorphic associations. It's a hack that breaks rules of relational database design.
SQL does not support conditional joins to different tables depending on the value you find in a given row. All tables must be known at the time the database parses the query.
You could join to all those tables with outer joins, because exactly one of those tables should contain a match:
SELECT c.*, COALESCE(u.name, p.name, t.name, s.name) AS name
FROM tbl_comments AS c
LEFT OUTER JOIN tbl_users u ON c.to = u.id AND c.type = 'number'
LEFT OUTER JOIN tbl_persons p ON c.to = p.id AND c.type = 'player'
LEFT OUTER JOIN tbl_teams t ON c.to = t.id AND c.type = 'team'
LEFT OUTER JOIN tbl_sports s ON c.to = s.id AND c.type = 'league';
Or you could join to one of those tables but restrict to rows you know match:
SELECT c.*, u.name
FROM tbl_comments AS c
INNER JOIN tbl_users u ON c.to = u.id
WHERE c.type = 'number'
UNION ALL
SELECT c.*, p.name
FROM tbl_comments AS c
INNER JOIN tbl_persons u ON c.to = p.id
WHERE c.type = 'player'
UNION ALL
SELECT c.*, t.name
FROM tbl_comments AS c
INNER JOIN tbl_teams t ON c.to = t.id
WHERE c.type = 'team'
UNION ALL
SELECT c.*, s.name
FROM tbl_comments AS c
INNER JOIN tbl_sports s ON c.to = s.id
WHERE c.type = 'league'
How can you tell what tbl_comments.to is, do you have another column to save the type?
Or you need to join all the tables.
SELECT * FROM tbl_comments c
LEFT JION ON tbl_users u ON c.to = u.id
LEFT JION ON tbl_persons p ON c.to = p.id
LEFT JION ON tbl_teams t ON c.to = t.id
LEFT JION ON tbl_sports s ON c.to = s.id

Resources