Skip to content

Tag: sql

SQL query for key value table with 1:n relation

I have a table in which I want to store images. Each image has arbitrary properties that I want to store in a key-value table. The table structure looks like this id fk_picture_id key value 1 1 camera iphone 2 1 year 2001 3 1 country Germany 4 2 camera iphone 5 2 year 2020 6 2 country United States

Select distinct very slow

I have a table where I store rows with external ids. Quite often I need to select latest timestamp for given external ids. Now it is a bottleneck for my app Query: Explain: What could I do to make this query faster? Or probably should I use completely different query? UPDATE: Added new query plan as asked @ja…

SQL count by type

I would like to summarize in a table so that I have for each id in each round, the number of tokens “red” and “blue”. What would the SQL query for such table? Here’s the data. The result will display like below: I have been trying the following code: Answer I would suggest this to get to the requested r…

How to get count(percentage) for columns after each groupby item?

I have the following table. Using sqlite DB Item Result A Pass B Pass A Fail B Fail I want to realize the above table as below using some query. Item Total Accept Reject A 2 1(50%) 1(50%) B 2 1(50%) 1(50%) How should I construct this query? Answer You can try PIVOT() if your DBMS supports. Then use CONCAT