I have a table like
Id RefNumber LotNum
---------------------------
1 Ref-1 10
2 Ref-1 11
Lotnumber:
Lot-Id Lot-Name
-------------------
10 Apple
11 Banana
I need my output to look like this:
Ref-1 Apple,Banana
Please help me - how can I achieve this?
On SQL Server 2017 and later, we can use STRING_AGG here:
SELECT
r.RefNumber,
STRING_AGG(l.[Lot-Name]) WITHIN GROUP (ORDER BY l.[Lot-Id]) AS LotNames
FROM Refs r
LEFT JOIN Lotnumber l
ON r.LotNum = l.[Lot-Id]
GROUP BY
r.RefNumber;
Related
I'm a newbie at SQL and Google BigQuery.
I'm trying to run the following query to get a list of names and counts, however I see that I am getting an array error and don't know how to fix it. Any help appreciated.
ERROR MESSAGE:
Cannot access field harmonized on a value with type ARRAY at [5:27]
#standardSQL
-- Applications_Per_Assignee
SELECT assignee_harmonized.name AS Assignee_Name, COUNT(*) AS Number_of_Patent_Apps
FROM (
SELECT ANY_VALUE(assignee.harmonized.name) AS Assignee_Name
FROM `patents-public-data.patents.publications` AS patentsdb
GROUP BY Number_of_Patent_Apps
)
GROUP BY assignee_harmonized.name
ORDER BY Number_of_Patent_Apps DESC;
Below is for BigQuery Standard SQL
#standardSQL
SELECT
ah.name AS Assignee_Name,
COUNT(*) AS Number_of_Patent_Apps
FROM `patents-public-data.patents.publications`,
UNNEST(assignee_harmonized) ah
GROUP BY Assignee_Name
HAVING Number_of_Patent_Apps < 1000
ORDER BY Number_of_Patent_Apps DESC
-- LIMIT 10
with output
Row Assignee_Name Number_of_Patent_Apps
1 SAMSUNG ELECTRONICS CO LTD 600678
2 CANON KK 579731
3 MATSUSHITA ELECTRIC IND CO LTD 560644
4 HITACHI LTD 531286
5 SIEMENS AG 486276
6 MITSUBISHI ELECTRIC CORP 461673
7 IBM 438822
8 SONY CORP 438039
9 FUJITSU LTD 384270
10 NEC CORP 357193
Looks like there are a few things wrong with your query.
assignee is a string, I think you want to look at assignee_harmonized.name
You will want to UNNEST() assignee_harmonized
ANY_VALUE() only selects a random value, which does not sound like what you want
You have a GROUP BY in your inner select, which will not give you the results you want
You don't really need a subquery for this type of query.
#standardSQL
SELECT ah.name AS Assignee_Name, COUNT(*) AS Number_of_Patent_Apps
FROM `patents-public-data.patents.publications` AS patentsdb
LEFT JOIN UNNEST(assignee_harmonized) ah
GROUP BY 1
ORDER BY 2 DESC
I have 2 tables on SQL Server 2008, each one has a single column and the same rows count number:
USERS OPERATION
Name Operation
----------- -----------
John W383
William R823
Karen X933
Peter M954
Alex S744
I need to perform every week a random draw between the 2 tables to get something like the follow and save it into a 3rd. table:
DRAW_RESULT:
Name Operation_Assigned Week_Number
----------------------------------------------
Peter M954 2
William W383 2
John S744 2
Alex X933 2
Karen R823 2
Name Operation_Assigned Week_Number
----------------------------------------------
William R823 3
Alex M954 3
Karen X933 3
John S744 3
Peter W383 3
How can I do this using T-SQL?
If I understood correctly what you're doing, something like this should work:
select name, operation from (
select
row_number() over (order by (select null)) as RN,
name
from
users
) U join (
select
row_number() over (order by newid()) as RN,
operation
from
operation
) O on U.RN = O.RN
Edit: row_number with newid() works, so removed the extra derived table.
Here's also SQL Fiddle to test this.
I have the following tables:
Manufacturer: Model range:
IDManufacturer Manufacturer IDModelRange IDManufacturer ModelRange
1 Mercedes 1 1 Benz
2 Audi 2 1 E-Klasse
3 2 TT
4 2 A4
I would like to query the data from both tables and the result to be like:
IDManufacturer+ModelRange
1 Benz
1 E-Klasse
I tried all joins but I couldn't find the right one. Need some help! Thanks
I'm assuming you are trying to find the Models for a specific Manufacturer.
If you are filtering by the ID you don't need the JOIN as stated in the comments.
If you are filtering by the name you can do it with a INNER JOIN. Here is an example:
Declare
#myManufacturerChoice VARCHAR(100) = 'Mercedes';
SELECT
M.IDManufacturer,
mo.ModelRange Manufacturer
from Manufacturer M
inner join [Model range] mo on
M.IDManufacturer = mo.IDManufacturer
where
M.Manufacturer = #myManufacturerChoice
Also a reference link about joins.
I have two tables,Blog table has a FK BlogTagID column point to BlogTag table:
Blog table:
BlogID BlogTagID BlogTitle
1 2 test1
2 1 test2
3 2 test3
BlogTag table:
BlogTagID BlogTagName
1 JAVA
2 .NET
3 PHP
I would like to get the result:
BlogTagName count
JAVA 1
.NET 2
PHP 0
How to get this?Thank you very much!
try this code
select BlogTagName, count(blogid)
from BlogTag bt
left join Blog b on b.blogtagid = bt.BlogTagID
group by BlogTagName
SQL FIDDLE : http://sqlfiddle.com/#!3/356c5/8/0
You can try this also
SELECT BlogTagName,COUNT(BlogTagID) FROM Blog b JOIN BlogTagID bt WHERE b.BlogTagID=bt.BlogTagID GROUP BY BlogTagID;
Its my first post and I'm really rusty on MSSQL so be gentle :-)
I have a table in which I am trying to use datediff. I think it will be easiest if I post the query and results first
select mh.agetime, mh.whatid
from mailhistory mh
inner join mail m
on mh.mailid=m.myid
where (mh.whatid=17 or mh.whatid=11 or mh.whatid=0) and maincontactid=287816 and mailid=276086
order by agetime
Really, the maincontactid and mailid are currently just in there to limit the results while i make the query.
The results are as follows...
AGETIME WHATID
1899-12-30 00:00:00.000 0
1899-12-30 00:48:10.000 11
1899-12-31 02:16:49.000 17
1899-12-31 06:29:08.000 11
1900-01-18 15:31:40.000 17
1900-02-11 14:56:59.000 11
I am trying to make a third column as the query runs that will make a third column showing the difference in the dates (in days)... between items with a WHATID of 11 and 17... so I'm after results like this:
AGETIME WHATID DIFFERENCE
1899-12-30 00:00:00.000 0 NULL
1899-12-30 00:48:10.000 11 0
1899-12-31 02:16:49.000 17 1
1899-12-31 06:29:08.000 11 0
1900-01-18 15:31:40.000 17 18
1900-02-11 14:56:59.000 11 22
Something like that... So, is there a way to convert my query to do the running datediff like that?
Many thanks in advance!
Chris
If using SQL Server 2005 or above, you can make a CTE and assign row_number to your current resultset. Then you can have a left self join on CTE with Current.Row_Num = Previous.Row_Num -1 and then can get the date difference. Approach will be similar as show in this link:
http://www.kodyaz.com/articles/sql-select-previous-and-next-rows-with-current-row-in-tsql.aspx
For SQL Server 2000
Assuming there is no grouping in result set:
I would probably create a temp table variable instead of CTE with an Identity Filed( Which will act as a rownumber) and then will apply the same logic as described above for 2005. Only major change will be instead of CTE we will use Temp Table variable and instead of rownumber we will use identity in temp table.