System:
You are an expert in writing database language. You can write a corresponding database language query to answer the user's question based on the table structure described by the table creation statement.
##Table Structure
CREATE TABLE transportation.bookings ( booking_id.text, passenger_id.text, trip_id.text, seat_number.text, booking_time.timestamp, status.text, fare.numeric, payment_transaction_id.text, special_requests.text )

[User] List the number of bookings for each day.

[Assistant]
SELECT DATE(booking_time) AS booking_date, COUNT(*) AS booking_count
FROM transportation.bookings
GROUP BY DATE(booking_time);

[User] Now only include days where booking_count > 10.

[Assistant]
SELECT DATE(booking_time) AS booking_date, COUNT(*) AS booking_count
FROM transportation.bookings
GROUP BY DATE(booking_time)
HAVING COUNT(*) > 10;

[User] Also include the total fare collected for those days.

[Assistant]
SELECT DATE(booking_time) AS booking_date,
       COUNT(*) AS booking_count,
       SUM(fare) AS total_fare
FROM transportation.bookings
GROUP BY DATE(booking_time)
HAVING COUNT(*) > 10;

[User] Find all pairs of bookings that share the same payment_transaction_id.

[Assistant]
SELECT b1.booking_id AS booking_id_1, b2.booking_id AS booking_id_2, b1.payment_transaction_id
FROM transportation.bookings b1
JOIN transportation.bookings b2
  ON b1.payment_transaction_id = b2.payment_transaction_id
 AND b1.booking_id < b2.booking_id;

[User] Now limit the results to pairs where both bookings have status 'Completed'.

[Assistant]
SELECT b1.booking_id AS booking_id_1, b2.booking_id AS booking_id_2, b1.payment_transaction_id
FROM transportation.bookings b1
JOIN transportation.bookings b2
  ON b1.payment_transaction_id = b2.payment_transaction_id
 AND b1.booking_id < b2.booking_id
WHERE b1.status = 'Completed'
  AND b2.status = 'Completed';

[User] What are the average and maximum fare amounts for each booking status?

[Assistant]
SELECT status, AVG(fare) AS avg_fare, MAX(fare) AS max_fare
FROM transportation.bookings
GROUP BY status;

[User] Only include statuses where the average fare is greater than 50.

[Assistant]
SELECT status, AVG(fare) AS avg_fare, MAX(fare) AS max_fare
FROM transportation.bookings
GROUP BY status
HAVING AVG(fare) > 50;

[User] List pairs of bookings made for the same trip, showing booking IDs and their booking_time.

[Assistant]
SELECT b1.booking_id AS booking1,
       b2.booking_id AS booking2,
       b1.trip_id,
       b1.booking_time AS time1,
       b2.booking_time AS time2
FROM transportation.bookings b1
JOIN transportation.bookings b2
  ON b1.trip_id = b2.trip_id
  AND b1.booking_id < b2.booking_id;

[User] Actually, only include pairs where the time difference between bookings is less than 2 hours.

[Assistant]
SELECT b1.booking_id AS booking1,
       b2.booking_id AS booking2,
       b1.trip_id,
       b1.booking_time AS time1,
       b2.booking_time AS time2
FROM transportation.bookings b1
JOIN transportation.bookings b2
  ON b1.trip_id = b2.trip_id
  AND b1.booking_id < b2.booking_id
WHERE ABS(EXTRACT(EPOCH FROM (b2.booking_time - b1.booking_time))) < 2 * 3600;

[User] Also calculate the difference in minutes between the two booking_time values as diff_minutes.

[Assistant]
SELECT b1.booking_id AS booking1,
       b2.booking_id AS booking2,
       b1.trip_id,
       b1.booking_time AS time1,
       b2.booking_time AS time2,
       ROUND(ABS(EXTRACT(EPOCH FROM (b2.booking_time - b1.booking_time))) / 60, 2) AS diff_minutes
FROM transportation.bookings b1
JOIN transportation.bookings b2
  ON b1.trip_id = b2.trip_id
  AND b1.booking_id < b2.booking_id
WHERE ABS(EXTRACT(EPOCH FROM (b2.booking_time - b1.booking_time))) < 2 * 3600;

[User] List each booking with its previous booking time for the same passenger.

[Assistant]
SELECT b.booking_id,
       b.passenger_id,
       b.booking_time,
       LAG(b.booking_time) OVER (PARTITION BY b.passenger_id ORDER BY b.booking_time) AS previous_booking_time
FROM transportation.bookings b;

[User] I only want records where the gap between bookings is more than 7 days.

[Assistant]
SELECT booking_id,
       passenger_id,
       booking_time,
       previous_booking_time
FROM (
  SELECT booking_id,
         passenger_id,
         booking_time,
         LAG(booking_time) OVER (PARTITION BY passenger_id ORDER BY booking_time) AS previous_booking_time
  FROM transportation.bookings
) t
WHERE previous_booking_time IS NOT NULL
  AND booking_time - previous_booking_time > INTERVAL '7 days';

[User] Also include the total number of bookings for each passenger.

[Assistant]
SELECT booking_id,
       passenger_id,
       booking_time,
       previous_booking_time,
       COUNT(*) OVER (PARTITION BY passenger_id) AS total_bookings
FROM (
  SELECT booking_id,
         passenger_id,
         booking_time,
         LAG(booking_time) OVER (PARTITION BY passenger_id ORDER BY booking_time) AS previous_booking_time
  FROM transportation.bookings
) t
WHERE previous_booking_time IS NOT NULL
  AND booking_time - previous_booking_time > INTERVAL '7 days';

[User] List all unique pairs of passenger_id and trip_id where a passenger booked more than one seat on the same trip. For each pair, show the booking_ids involved.

[Assistant]
SELECT b1.passenger_id, b1.trip_id, b1.booking_id AS booking_id_1, b2.booking_id AS booking_id_2
FROM transportation.bookings b1
JOIN transportation.bookings b2
  ON b1.passenger_id = b2.passenger_id
  AND b1.trip_id = b2.trip_id
  AND b1.booking_id < b2.booking_id
GROUP BY b1.passenger_id, b1.trip_id, b1.booking_id, b2.booking_id;

[User] Can you please add the fare for each booking in the result, and only show pairs where both fares are greater than 75?

[Assistant]
SELECT b1.passenger_id, b1.trip_id, b1.booking_id AS booking_id_1, b1.fare AS fare_1, b2.booking_id AS booking_id_2, b2.fare AS fare_2
FROM transportation.bookings b1
JOIN transportation.bookings b2
  ON b1.passenger_id = b2.passenger_id
  AND b1.trip_id = b2.trip_id
  AND b1.booking_id < b2.booking_id
WHERE b1.fare > 75 AND b2.fare > 75
GROUP BY b1.passenger_id, b1.trip_id, b1.booking_id, b2.booking_id, b1.fare, b2.fare;

[User] Show the total number of bookings made for each seat_number. Include only seat numbers which have more than one booking.

[Assistant]
SELECT seat_number, COUNT(*) AS booking_count
FROM transportation.bookings
WHERE seat_number IS NOT NULL
GROUP BY seat_number
HAVING COUNT(*) > 1;

[User] Please also display the earliest and latest booking_time for each of these seat numbers.

[Assistant]
SELECT seat_number, COUNT(*) AS booking_count, MIN(booking_time) AS earliest_booking, MAX(booking_time) AS latest_booking
FROM transportation.bookings
WHERE seat_number IS NOT NULL
GROUP BY seat_number
HAVING COUNT(*) > 1;


System:
You are an expert in writing database language. You can write a corresponding database language query to answer the user's question based on the table structure described by the table creation statement.
##Table Structure
CREATE TABLE project_fundraising_record ( id character varying(32) NOT NULL, project_id character varying(32) NOT NULL, project_name character varying(128) NOT NULL, rounds_name character varying(32), investment_value numeric(38,14), investment_date character varying(128), introduction character varying(128), valuation numeric(38,14) )
CREATE TABLE narrative_info ( narrative character varying(512) NOT NULL, top_3_coins text, price_change_percentage_1h double precision, price_change_percentage_24h double precision, price_change_percentage_7d double precision, market_cap double precision, volume_24h double precision, trend_rank bigint, token_num bigint )
CREATE TABLE token_dexpool_info ( pair_address character varying(512) NOT NULL, token_name character varying(100) NOT NULL, token_symbol character varying(100) NOT NULL, chain text, price double precision, price_change_percentage_1h double precision, price_change_percentage_6h double precision, price_change_percentage_24h double precision, makers_24h double precision, volume_24h double precision, liquidity double precision, market_cap double precision, trend_rank bigint )
CREATE TABLE cex_token_balance ( snapshot_time character varying(32) NOT NULL, chain character varying(128) NOT NULL, address character varying(128) NOT NULL, exchange character varying(512) NOT NULL, token_address character varying(128) NOT NULL, token_symbol character varying(512), balance numeric(38,10), usd_value numeric(38,14), price double precision )
CREATE TABLE smart_money_token_performance ( token_address character varying(100) NOT NULL, token_symbol character varying(100), pnl numeric(38,2), net_flow numeric(38,2), avg_cost numeric(38,16), price double precision, realized_ratio numeric(10,2), roi numeric(10,2), chain character varying(100) )

[User] Which projects had fundraising rounds named 'Series A' in 2022, and what are their token symbols and current token prices from DEX pools?

[Assistant]
SELECT p.project_name, p.rounds_name, d.token_symbol, d.price
FROM project_fundraising_record p
JOIN token_dexpool_info d ON LOWER(p.project_name) = LOWER(d.token_name)
WHERE p.rounds_name = 'Series A' AND p.investment_date LIKE '2022%';

[User] Can you also include the chain of each token and order the results by token price descending?

[Assistant]
SELECT p.project_name, p.rounds_name, d.token_symbol, d.chain, d.price
FROM project_fundraising_record p
JOIN token_dexpool_info d ON LOWER(p.project_name) = LOWER(d.token_name)
WHERE p.rounds_name = 'Series A' AND p.investment_date LIKE '2022%'
ORDER BY d.price DESC;

[User] I only want to see projects where the fundraising amount exceeded $10,000,000.

[Assistant]
SELECT p.project_name, p.rounds_name, d.token_symbol, d.chain, d.price
FROM project_fundraising_record p
JOIN token_dexpool_info d ON LOWER(p.project_name) = LOWER(d.token_name)
WHERE p.rounds_name = 'Series A' AND p.investment_date LIKE '2022%'
  AND p.investment_value > 10000000
ORDER BY d.price DESC;

[User] I got empty results, maybe the join with project_name and token_name is too strict. Can you try joining where either project_name equals token_name or project_name is contained in token_name, in a case-insensitive way?

[Assistant]
SELECT p.project_name, p.rounds_name, d.token_symbol, d.chain, d.price
FROM project_fundraising_record p
JOIN token_dexpool_info d
  ON LOWER(p.project_name) = LOWER(d.token_name)
    OR LOWER(d.token_name) LIKE CONCAT('%', LOWER(p.project_name), '%')
WHERE p.rounds_name = 'Series A' AND p.investment_date LIKE '2022%'
  AND p.investment_value > 10000000
ORDER BY d.price DESC;

[User] For each narrative, show its name and the total market cap of all related tokens based on DEX pool data.

[Assistant]
SELECT n.narrative, SUM(d.market_cap) AS total_market_cap
FROM narrative_info n
JOIN token_dexpool_info d ON n.top_3_coins LIKE CONCAT('%', d.token_symbol, '%')
GROUP BY n.narrative;

[User] Could you also include the number of tokens per narrative as a column in the result?

[Assistant]
SELECT n.narrative, COUNT(DISTINCT d.token_symbol) AS token_count, SUM(d.market_cap) AS total_market_cap
FROM narrative_info n
JOIN token_dexpool_info d ON n.top_3_coins LIKE CONCAT('%', d.token_symbol, '%')
GROUP BY n.narrative;

[User] I want to filter out narratives where the total market cap is less than 100 million USD.

[Assistant]
SELECT n.narrative, COUNT(DISTINCT d.token_symbol) AS token_count, SUM(d.market_cap) AS total_market_cap
FROM narrative_info n
JOIN token_dexpool_info d ON n.top_3_coins LIKE CONCAT('%', d.token_symbol, '%')
GROUP BY n.narrative
HAVING SUM(d.market_cap) >= 100000000;

[User] List tokens on the 'Polygon' chain held by CEX addresses. Show token_symbol and total USD value held.

[Assistant]
SELECT token_symbol, SUM(usd_value) AS total_usd_value
FROM cex_token_balance
WHERE chain = 'Polygon'
GROUP BY token_symbol;

[User] Include the smart money average price (avg_price) for each token.

[Assistant]
SELECT c.token_symbol, SUM(c.usd_value) AS total_usd_value, AVG(i.avg_price) AS avg_price
FROM cex_token_balance c
LEFT JOIN smart_money_token_inflow i
  ON c.token_symbol = i.token_symbol
  AND c.chain = i.chain
  AND i.type = 1
WHERE c.chain = 'Polygon'
GROUP BY c.token_symbol;

[User] Filter to only include tokens with total USD value above 1000000.

[Assistant]
SELECT c.token_symbol, SUM(c.usd_value) AS total_usd_value, AVG(i.avg_price) AS avg_price
FROM cex_token_balance c
LEFT JOIN smart_money_token_inflow i
  ON c.token_symbol = i.token_symbol
  AND c.chain = i.chain
  AND i.type = 1
WHERE c.chain = 'Polygon'
GROUP BY c.token_symbol
HAVING SUM(c.usd_value) > 1000000;

[User] Order by total USD value descending and limit to top 5 tokens.

[Assistant]
SELECT c.token_symbol, SUM(c.usd_value) AS total_usd_value, AVG(i.avg_price) AS avg_price
FROM cex_token_balance c
LEFT JOIN smart_money_token_inflow i
  ON c.token_symbol = i.token_symbol
  AND c.chain = i.chain
  AND i.type = 1
WHERE c.chain = 'Polygon'
GROUP BY c.token_symbol
HAVING SUM(c.usd_value) > 1000000
ORDER BY total_usd_value DESC
LIMIT 5;

[User] What is the average project valuation for each round name in 2023?

[Assistant]
SELECT rounds_name, AVG(valuation) AS avg_valuation
FROM project_fundraising_record
WHERE investment_date LIKE '2023-%'
GROUP BY rounds_name;

[User] Only include rounds where the average valuation is above 50000000.

[Assistant]
SELECT rounds_name, AVG(valuation) AS avg_valuation
FROM project_fundraising_record
WHERE investment_date LIKE '2023-%'
GROUP BY rounds_name
HAVING AVG(valuation) > 50000000;

[User] List all tokens on the 'Ethereum' chain with DEX 24h volume over 1000000. Show token_symbol, price, and volume_24h.

[Assistant]
SELECT token_symbol, price, volume_24h
FROM token_dexpool_info
WHERE chain = 'Ethereum' AND volume_24h > 1000000;

[User] Also show the total USD inflow from smart money (type=1) for each token.

[Assistant]
SELECT t.token_symbol, t.price, t.volume_24h, COALESCE(SUM(i.usd_value),0) AS total_usd_inflow
FROM token_dexpool_info t
LEFT JOIN smart_money_token_inflow i
  ON t.token_symbol = i.token_symbol
  AND t.chain = i.chain
  AND i.type = 1
WHERE t.chain = 'Ethereum' AND t.volume_24h > 1000000
GROUP BY t.token_symbol, t.price, t.volume_24h;

[User] Filter to only include tokens with total USD inflow greater than 500000.

[Assistant]
SELECT t.token_symbol, t.price, t.volume_24h, COALESCE(SUM(i.usd_value),0) AS total_usd_inflow
FROM token_dexpool_info t
LEFT JOIN smart_money_token_inflow i
  ON t.token_symbol = i.token_symbol
  AND t.chain = i.chain
  AND i.type = 1
WHERE t.chain = 'Ethereum' AND t.volume_24h > 1000000
GROUP BY t.token_symbol, t.price, t.volume_24h
HAVING COALESCE(SUM(i.usd_value),0) > 500000;

[User] Order the results by total USD inflow descending and limit to top 5.

[Assistant]
SELECT t.token_symbol, t.price, t.volume_24h, COALESCE(SUM(i.usd_value),0) AS total_usd_inflow
FROM token_dexpool_info t
LEFT JOIN smart_money_token_inflow i
  ON t.token_symbol = i.token_symbol
  AND t.chain = i.chain
  AND i.type = 1
WHERE t.chain = 'Ethereum' AND t.volume_24h > 1000000
GROUP BY t.token_symbol, t.price, t.volume_24h
HAVING COALESCE(SUM(i.usd_value),0) > 500000
ORDER BY total_usd_inflow DESC
LIMIT 5;

[User] For each fundraising round, can you list the total amount invested and the corresponding number of projects in that round, as well as the highest token market cap associated with any project of that round? Please show the round name, total investment, project count, and max token market cap.

[Assistant]
SELECT r.rounds_name, SUM(r.investment_value) AS total_investment, COUNT(DISTINCT r.project_id) AS project_count, MAX(b.market_cap) AS max_token_market_cap
FROM project_fundraising_record r
LEFT JOIN token_basic_info b ON LOWER(r.project_name) = LOWER(b.token_name)
GROUP BY r.rounds_name;

[User] Actually, can you restrict this to only rounds where the max token market cap is above 100 million? Also, order by total investment descending.

[Assistant]
SELECT rounds_name, total_investment, project_count, max_token_market_cap
FROM (
  SELECT r.rounds_name, SUM(r.investment_value) AS total_investment, COUNT(DISTINCT r.project_id) AS project_count, MAX(b.market_cap) AS max_token_market_cap
  FROM project_fundraising_record r
  LEFT JOIN token_basic_info b ON LOWER(r.project_name) = LOWER(b.token_name)
  GROUP BY r.rounds_name
) t
WHERE max_token_market_cap > 100000000
ORDER BY total_investment DESC;

[User] I got empty results. Could you update the join condition to also allow a match when either the project name is contained in the token name or vice versa, case-insensitively?

[Assistant]
SELECT rounds_name, total_investment, project_count, max_token_market_cap
FROM (
  SELECT r.rounds_name, SUM(r.investment_value) AS total_investment, COUNT(DISTINCT r.project_id) AS project_count, MAX(b.market_cap) AS max_token_market_cap
  FROM project_fundraising_record r
  LEFT JOIN token_basic_info b
    ON LOWER(r.project_name) = LOWER(b.token_name)
    OR LOWER(b.token_name) LIKE CONCAT('%', LOWER(r.project_name), '%')
    OR LOWER(r.project_name) LIKE CONCAT('%', LOWER(b.token_name), '%')
  GROUP BY r.rounds_name
) t
WHERE max_token_market_cap > 100000000
ORDER BY total_investment DESC;

[User] Show me the top 5 projects by total investment value, along with their latest fundraising round name and valuation.

[Assistant] SELECT project_name, rounds_name, valuation, SUM(investment_value) AS total_investment_value FROM project_fundraising_record GROUP BY project_name, rounds_name, valuation ORDER BY total_investment_value DESC LIMIT 5;

[User] Can you also include the investment date for these projects?

[Assistant] SELECT project_name, rounds_name, valuation, investment_date, SUM(investment_value) AS total_investment_value FROM project_fundraising_record GROUP BY project_name, rounds_name, valuation, investment_date ORDER BY total_investment_value DESC LIMIT 5;