group-by

MySQL - Counting rows and left join problem

断了今生、忘了曾经 提交于 2020-01-01 04:24:05
问题 I have 2 tables, campaigns and campaign_codes: campaigns: id, partner_id, status campaign_codes: id, code, status I want to get a count of all campaign_codes for all campaigns WHERE campaign_codes.status equals 0 OR where there are no campaign_codes records for a campaign. I have the following SQL, but of course the WHERE statement eliminates those campaigns which have no corresponding records in campaign_codes ( i want those campaigns with zero campaign_codes as well) SELECT c.id AS campaign

Grouping by 2 fields in XSL

梦想的初衷 提交于 2020-01-01 03:18:07
问题 I have next xml: <page> <document> <id>1001</id> <cur>USD</cur> <date>01.01.2009</date> <amount>10</amount> </document> <document> <id>1001</id> <cur>USD</cur> <date>02.01.2009</date> <amount>15</amount> </document> <document> <id>1001</id> <cur>JPY</cur> <date>01.01.2009</date> <amount>5</amount> </document> <document> <id>1002</id> <cur>USD</cur> <date>01.01.2009</date> <amount>5</amount> </document> ... </page> And I need to transform it into html. Records should be grouped by id and cur .

SQL frequency distribution query to count ranges with group-by and include 0 counts

走远了吗. 提交于 2019-12-31 14:33:42
问题 Given: table 'thing': age --- 3.4 3.4 10.1 40 45 49 I want to count the number of things for each 10-year range, e.g., age_range | count ----------+------- 0 | 2 10| 1 20| 0 30| 0 40| 3 This query comes close: SELECT FLOOR(age / 10) as age_range, COUNT(*) FROM thing GROUP BY FLOOR(age / 10) ORDER BY FLOOR(age / 10); Output: age_range | count -----------+------- 0 | 1 1 | 2 4 | 3 However, it doesn't show me the ranges which have 0 counts. How can I modify the query so that it also shows the

BigQuery - Group by on arrays

血红的双手。 提交于 2019-12-31 07:03:02
问题 I want to group by on an array. sample query: #standardSQL WITH `project.dataset.table` AS ( SELECT 'compute' description, '[{"key":"application","value":"scaled-server"},{"key":"department","value":"hrd"}]' labels, 0.323316 cost UNION ALL SELECT 'compute' description, '[{"key":"application","value":"scaled-server"},{"key":"department","value":"hrd"}]' labels, 0.342825 cost ) SELECT description, ARRAY( SELECT AS STRUCT JSON_EXTRACT_SCALAR(kv, '$.key') key, JSON_EXTRACT_SCALAR(kv, '$.value')

how to group by, and select from two tables, need two records for each given id

限于喜欢 提交于 2019-12-31 05:39:07
问题 I am new to SQL query. Can you please help me with the following? table 1 QuoteObservations: id value quotePointId asOfTime table 2 QuotePoints: id, quotoType quoteType could be LastPrice, RepoRate, etc I need to select id and value, asOfTime, quoteType, with highest asOfTime from table 1 with quoteType=LastPrice ( =1) or RepoRate (=2) in table 2; I need to return two records, one for last price, one for repo rate for each id, but both last price and repo rate should be with the highest

GROUP BY lname ORDER BY showing wrong results

孤街醉人 提交于 2019-12-31 05:12:06
问题 This query I've written, listing the top 25 in a sales competition, is showing the wrong results. Users aren't showing that should be pretty far up on the list. Any idea what the issue could be? SELECT u.fname, u.lname, SUM(p.point) as points FROM comp_sale s, comp_product p, comp_user u WHERE s.prod_id = p.product_id AND s.sale_id = u.wp_id GROUP BY lname ORDER BY points DESC limit 25 table comp_user: user_id int(11) NO PRI NULL auto_increment fname varchar(255) NO NULL lname varchar(255) NO

SQL Group By / Count: Count Same Values Across Multiple Columns?

 ̄綄美尐妖づ 提交于 2019-12-31 04:49:06
问题 I'm trying to figure out how to write a query that counts values across multiple columns, with the result table having a count in each column for every possible value of any column. Example: Say I have mytable Source data table: P1 P2 P3 ----------- a b a a a a b b b a b b I want a query that counts a's and b's in each column, producing something like: Desired query output: P1 P2 P3 ------------- a | 3 1 2 b | 1 3 2 I know I can do this for a single column easily with a group by : select P1,

Get a substring from a column and perform a groupBy and count

僤鯓⒐⒋嵵緔 提交于 2019-12-31 04:26:09
问题 I have a table that stores data about a large number of files, such as their language, unique ID, file path etc. I want to be able to get the sub-string from the unique ID which gives me the asset type, this is always the first 2 letters of the ID. I then want to group these asset types by language and have a count for how many of each type every language has. So at the end I would ideally like a table that has a language column and then a column for each substring (asset type). I have tried

R language: problems computing “group by” or split with ff package

青春壹個敷衍的年華 提交于 2019-12-31 03:47:04
问题 I am nearly new to R, so sorry if I make some basic questions, but I can not find a solution to this "simple" problem: Having a database (big one, 25 million rows, 14 cols) of patients, I have several rows for each "id", with for example this structure: "id" "birth_date" "treatment" "date_treatment" 123 2002-01-01 2 2011-01-03 123 2002-01-01 3 2011-10-03 124 2002-01-01 6 2009-11-07 124 2002-01-01 NA NA ... ..... ...... ........ 1022 2007-01-01 4 2011-01-06 I have to use ff package to be able

Linq relational table include

落花浮王杯 提交于 2019-12-31 03:44:08
问题 I have 4 relational table; Category Product ProductBrand ProductImage I need linq query included 3 tables group by ProductBrand from CategoryId . I try this; var PBrand = from b in db.ProductBrands join p in db.Products on b.BrandId equals p.BrandId join i in db.ProductImages on p.ProductId equals i.ProductId where b.CategoryId == 5 select b; but Products and ProductImages are null. How I include Products and ProductImages table on Brand table? 回答1: Use Include from b in db.ProductBrands